技术与版本:
要求:
“深嵌套引用”与Swagger。最终,我希望有一个主swagger文件,$ref一个用于路径参数/响应定义的外部文件,然后外部文件应该能够在同一个文件中$ref子定义。
迄今为止:
我正在使用JSON.NET架构库来运行我们的程序集,并以json格式创建傲慢的模式。然后从我们的主swagger.json文件中手动引用这些文件。我有两个结果:
我想让结果2开始工作。
例如,如果我有以下两个文件,我希望"$ref":“#/定义/教育”部分能够工作。swaggerSchemas.json输出是我从JSON.NET模式生成器获得的。我尝试过将“定义”从"Person“移到swaggerSchemas.json的根json包装,但这也不起作用。当我说“它不管用”时,我的意思是斯威格不喜欢它。应用程序因Swagger验证错误而死亡。
swagger.json
{
"swagger": "2.0",
"info": {
"version": "0.0.1",
"title": "ASDF"
},
"basePath": "/",
"schemes": [
"http",
"https"
],
"consumes": [
"application/json",
"application/octet-stream"
],
"produces": [
"application/json"
],
"paths": {
"/person": {
"x-swagger-router-controller": "PersonController",
"get": {
"x-function": "find",
"description": "Default Description",
"tags": [
"gen"
],
"responses": {
"200": {
"description": "Success",
"schema": {
"$ref": "swaggerSchemas.json#/Person"
}
},
"default": {
"$ref": "#/responses/ErrorResponse"
}
}
}
}
}
}
swaggerSchemas.json
{
"Person": {
"definitions": {
"education": {
"type": "object",
"properties": {
"highestQualification": {
"type": "string"
},
"extraData": {
"type": [
"string",
"number",
"integer",
"boolean",
"object",
"array"
]
}
},
"required": [
"highestQualification",
"extraData"
]
}
},
"type": "object",
"properties": {
"userId": {
"type": "string"
},
"firstNames": {
"type": "string"
},
"surname": {
"type": "string"
},
"education": {
"$ref": "#/definitions/Education"
}
}
}
}
这种行为,即“深嵌套$ref”可用于Swagger2.0吗?
如果是这样的话,我如何在JSON.NET模式中完成这一任务?
发布于 2018-01-04 12:37:25
swaggerSchemas.json文件的结构看起来无效:
{
"Person": {
"definitions": {
"education": {
"type": "object",
...
}
},
"type": "object",
...
}
}
包含多个架构的文件应该如下所示。根标记名可以是任意的,但是使用definitions
是常见的。
{
"definitions": {
// "Education", not "education".
// The letter case of the schema name must be the same as used in the $ref below.
"Education": {
"type": "object",
...
},
"Person": {
"type": "object",
"properties": {
...,
"education": {
"$ref": "#/definitions/Education"
}
}
}
}
}
另外,在主文件中,更改
"$ref": "swaggerSchemas.json#/Person"
至
"$ref": "swaggerSchemas.json#/definitions/Person"
以在definitions
文件中反映新的节点结构( -> Person
)。
https://stackoverflow.com/questions/47829632
复制相似问题