我正在使用 fastify (3.20.1) 和 fastify-swagger(4.8.4) 从我的模式中生成 openapi swagger doc。
但是,生成的 openAPI 3.0.3 swagger 中没有显示我作为 $ref 的 $id 引用。
例如,为了演示,我有以下架构:
le.exports = function (fastify) {
fastify.addSchema({
$id: 'persistence-request-doc',
title: "PersistenceServiceRequest",
description: '(Opeque) Persistence Service document as key-value pair passed as a request.',
type: 'object',
example: '{ key1: value1, key2: value2, ....., key-n: value-n}',
additionalProperties: true,
});
fastify.addSchema({
$id: 'persistence-response-doc',
title: "PersistenceServiceResponse",
type: 'object',
description: 'Persistence Service response document.',
properties: {
'_id': {
description: "Persistence Service response document identifier. This is auto generated and immutable.",
type: 'string',
minLength: 24,
maxLength: 24,
example: '60f51cf96bf74c1eec9be2cc',
},
'document': {
description: '(Opeque) Persistence Service response document.',
$ref: 'persistence-request-doc#',
},
},
});
在生成的 JSON 中,我看到了这个:
"def-3":{
"title":"PersistenceServiceRequest", ## <=== No presense of $id
"description":"(Opeque) Persistence Service document as key-value pair passed as a request.",
"type":"object",
"example":"{ key1: value1, key2: value2, ....., key-n: value-n}",
"additionalProperties":true
},
"def-4":{
"title":"PersistenceServiceResponse",
"type":"object",
"description":"Persistence Service response document.",
"properties":{
"_id":{
"description":"Persistence Service response document identifier. This is auto generated and immutable.",
"type":"string",
"minLength":24,
"maxLength":24,
"example":"60f51cf96bf74c1eec9be2cc"
},
"document":{
"description":"(Opeque) Persistence Service response document.",
"$ref":"persistence-request-doc#" ## Here, undef reference
}
}
},
结果,在我的 swagger-oas UI 中,我收到了这个错误,并且没有任何响应模型出现。
我正在寻找的是一种干净的方式,以便引用在 OAS3 中正常工作,并且我生成的 swagger-oas JSON 是干净的。
任何帮助如何使它正确将不胜感激。
谢谢, 普拉迪普