使用FromForm属性和Array属性生成Swagger JSON

p4rjhz4m  于 2023-10-18  发布在  其他
关注(0)|答案(1)|浏览(189)

我试图为我的服务生成swagger规范,它失败了这个错误。

Error: Swagger schema validation failed.
  Data does not match any schemas from 'oneOf' at #/paths//abc/{id}/createFile/post/parameters/3
    Data does not match any schemas from 'oneOf' at #/paths//abc/{id}/createFile/post/parameters/3
      Missing required property: schema at #/
      Data does not match any schemas from 'oneOf' at #/
        Additional properties not allowed: properties at #/items
        Additional properties not allowed: properties at #/items
        Additional properties not allowed: properties at #/items
        Missing required property: required at #/
    Missing required property: $ref at #/paths//abc/{id}/createFile/post/parameters/3

如果从InputClass中删除AdditionalInput属性,则Swagger规范生成成功。应该采取什么措施来解决此问题?

public class InputClass
{
    public IFormFile File { get; set; }
    public string Name { get; set; }
    public List<CustomKVP> AdditionalInput { get; set; }
}

public class CustomKVP
{
    public string Name { get; set; }
    public string Value { get; set; }
}

[HttpPost]
[Route("abc/{id}/createFile")]
[SwaggerOperation(OperationId = "createFile")]
public async Task<IActionResult> CreateFileAsync([FromRoute] string id, [FromForm] InputClass inputClass)
{
}

此外,如果我将FromForm更改为FromBody,则会创建Swagger Spec。
Swagger JSON,输入对象为FromForm

{
    "swagger": "2.0",
    "info": {
        "title": "Service - v1",
        "description": "Web API service.",
        "version": "v1"
    },
    "paths": {
        "/abc/{id}/createFile": {
            "post": {
                "tags": [
                    "createFile"
                ],
                "summary": "A create file operation",
                "operationId": "createFile",
                "consumes": [
                    "multipart/form-data"
                ],
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "in": "path",
                        "name": "id",
                        "description": "Id",
                        "required": true,
                        "type": "string"
                    },
                    {
                        "in": "formData",
                        "name": "File",
                        "type": "file"
                    },
                    {
                        "in": "formData",
                        "name": "Name",
                        "type": "string"
                    },
                    {
                        "in": "formData",
                        "name": "AdditionalInput",
                        "type": "array",
                        "items": {
                            "$ref": "#/definitions/CustomKVP"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Returns the output model.",
                        "schema": {
                            "$ref": "#/definitions/OutputInfo"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "OutputInfo": {
            "type": "object",
            "properties": {
                "uploadURL": {
                    "type": "string"
                }
            }
        },
        "CustomKVP": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string"
                },
                "value": {
                    "type": "string"
                }
            }
        }
    }
}

在Swagger UI上遇到错误。

Structural error at paths./abc/{id}/createFile.post.parameters.3.items
should NOT have additional properties
additionalProperty: $ref

Swagger JSON,输入对象为FromBody

{
  "swagger": "2.0",
  "info": {
    "title": "Service - v1",
    "description": "Web API service.",
    "version": "v1"
  },
  "paths": {
    "/abc/{id}/createFile": {
      "post": {
        "tags": [
          "createFile"
        ],
        "summary": "A create file operation",
        "operationId": "createFile",
        "consumes": [
          "application/json-patch+json",
          "application/json",
          "text/json",
          "application/*+json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "description": "Id",
            "required": true,
            "type": "string"
          },
          {
            "in": "body",
            "name": "inputClass",
            "description": "Input to create a file",
            "schema": {
              "$ref": "#/definitions/InputClass"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Returns the output model.",
            "schema": {
              "$ref": "#/definitions/OutputInfo"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "InputClass": {
      "type": "object",
      "properties": {
        "file": {
          "format": "binary",
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "additionalInput": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/CustomKVP"
          }
        }
      }
    },
    "OutputInfo": {
      "type": "object",
      "properties": {
        "uploadURL": {
          "type": "string"
        }
      }
    },
    "CustomKVP": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "value": {
          "type": "string"
        }
      }
    }
  }
}
vhmi4jdf

vhmi4jdf1#

我去了Github并阅读了OpenAPI规范模式。swagger json就是在这个模式下得到验证的。
https://github.com/OAI/OpenAPI-Specification/blob/main/schemas/v2.0/schema.json
我理解$ref不是FormData允许的属性。它只允许原始数据库。
模式不允许非原始数据库是有道理的,因为HTML表单没有可以提交非原始数据库的元素。
我更改了API规范。我现在将非原始数据类型作为字符串,并在服务器端代码中将其反序列化为对象。

相关问题