如何解决Swagger错误"实例类型(字符串)与任何允许的基本类型都不匹配......"

Zoo*_*oop 6 c# asp.net-web-api swagger swagger-2.0 swashbuckle

背景

我刚刚开始了一个新项目,并希望将Swagger用于我的API文档.我目前在本地运行我的项目,在IIS中托管.

我修改了我的hosts文件,为网站提供了有效的标头.对于这篇文章,让我们说标题是publicapiurl.domain.com.所以,我在主机文件中添加了以下条目:

127.0.0.1   publicapiurl.domain.com
Run Code Online (Sandbox Code Playgroud)

现在,当我输入publicapiurl.domain.com/swagger时,我会得到一个昂首阔步的文档.初始设置似乎很简单,但我的swagger doc右下角有一条红色的'ERROR {...}'消息.错误消息如下:

{"messages":["malformed or unreadable swagger supplied"],"schemaValidationMessages":[{"level":"error","domain":"validation","keyword":"type","message":"instance type (string) does not match any allowed primitive type (allowed: [\"object\"])","schema":{"loadingURI":"#","pointer":""},"instance":{"pointer":""}}]}
Run Code Online (Sandbox Code Playgroud)

我过去曾与Swagger合作过,因此我将提供的链接提供给生成的swagger文档并复制代码.我将代码粘贴到swagger.io/tools编辑器中,看看他们的验证过程可能告诉我什么.我粘贴的代码经过验证,没有任何错误.这是代码:

swagger: '2.0'
info:
  version: v1
  title: Generic.Public.Api
host: publicapiurl.domain.com
schemes:
  - http
paths:
  /api/Values:
    get:
      tags:
        - Values
      operationId: Values_Get
      consumes: []
      produces:
        - application/json
        - text/json
        - application/xml
        - text/xml
      responses:
        '200':
          description: OK
          schema:
            type: array
            items:
              type: string
    post:
      tags:
        - Values
      operationId: Values_PostByvalue
      consumes:
        - application/json
        - text/json
        - application/xml
        - text/xml
        - application/x-www-form-urlencoded
      produces: []
      parameters:
        - name: value
          in: body
          required: true
          schema:
            type: string
      responses:
        '204':
          description: No Content
  '/api/Values/{id}':
    get:
      tags:
        - Values
      operationId: Values_GetByid
      consumes: []
      produces:
        - application/json
        - text/json
        - application/xml
        - text/xml
      parameters:
        - name: id
          in: path
          required: true
          type: integer
          format: int32
      responses:
        '200':
          description: OK
          schema:
            type: string
    put:
      tags:
        - Values
      operationId: Values_PutByidvalue
      consumes:
        - application/json
        - text/json
        - application/xml
        - text/xml
        - application/x-www-form-urlencoded
      produces: []
      parameters:
        - name: id
          in: path
          required: true
          type: integer
          format: int32
        - name: value
          in: body
          required: true
          schema:
            type: string
      responses:
        '204':
          description: No Content
    delete:
      tags:
        - Values
      operationId: Values_DeleteByid
      consumes: []
      produces: []
      parameters:
        - name: id
          in: path
          required: true
          type: integer
          format: int32
      responses:
        '204':
          description: No Content
definitions: {} 
Run Code Online (Sandbox Code Playgroud)

有谁知道上面提到的错误实际意味着什么,或者我怎么能够解决它?

我最好的猜测是它与我修改主机文件和某种类型的CORS问题有关......但我真的很茫然.任何建议表示赞赏!

编辑:

我更简化了控制器并删除了XML响应类型,但我仍然收到在本地IIS上运行的相同错误.swagger定义仍然在swagger在线编辑器内无错误地验证.

我也从Swashbuckle nuget包切换到Swashbuckle.Core但结果是一样的.

这是新的招摇定义:

swagger: '2.0'
info:
  version: v1
  title: Generic Public Api
host: l-publicapi.generic.com
schemes:
  - http
paths:
  /api/values/values:
    get:
      tags:
        - Values
      operationId: Values_Get
      consumes: []
      produces:
        - application/json
        - text/json
      responses:
        '200':
          description: OK
          schema:
            type: array
            items:
              type: string
  '/api/values/values/{id}':
    get:
      tags:
        - Values
      operationId: Values_GetByid
      consumes: []
      produces:
        - application/json
        - text/json
      parameters:
        - name: id
          in: path
          required: true
          type: integer
          format: int32
      responses:
        '200':
          description: OK
          schema:
            type: string
definitions: {}
Run Code Online (Sandbox Code Playgroud)

还有其他建议吗?

Hel*_*len 1

有一个问题,但我不确定这是否就是验证者抱怨的原因。反正:

在 OpenAPI (fka Swagger) 2.0 中,操作不能同时使用表单数据和 JSON/XML。这是因为表单数据是使用in: formData参数描述的,而 JSON/XML 是使用in: body参数描述的,对于同一操作,正文和表单参数不能同时存在。这将在 OpenAPI 3.0(撰写本文时为 RC)中成为可能。