Zoop
Zoop

Reputation: 882

How to resolve Swagger error "instance type (string) does not match any allowed primitive type..."

Background

I've just started a new project and would like to use Swagger for my API Documentation. I am currently running my project locally, hosted in IIS.

I have modified my hosts file to give the site a valid header. For this post, let's say the header is publicapiurl.domain.com. So, I have added the following entry to my hosts file:

127.0.0.1   publicapiurl.domain.com

Now, when I type publicapiurl.domain.com/swagger I get the swagger docs. The initial setup seemed simple enough but I have a red 'ERROR {...}' message at the bottom right corner of my swagger doc. The error message reads as follows:

{"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":""}}]}

I've worked a bit with Swagger in the past so I took the provided link to the generated swagger doc and copied the code. I pasted the code into the swagger.io/tools editor to see what their validation process might tell me. The code I pasted validated without any errors. Here is the code:

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: {} 

Does anyone know what the aforementioned error I am getting actually means or how I may be able to resolve it?

My best guess is it has something to do with my modification of the hosts file and some type of CORS issue perhaps... but I am seriously at a loss. Any suggestions are appreciated!

EDIT:

I simplified the controller even more and removed the XML response type, but I still receive the same error running on my local IIS. The swagger definition still validates without errors inside of the swagger online editor.

I also switched from the Swashbuckle nuget package to Swashbuckle.Core but the result is the same.

Here is the new swagger definition:

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: {}

Any additional suggestions?

Upvotes: 6

Views: 16342

Answers (2)

jtate
jtate

Reputation: 2696

I was having the same issue, and just like you, my Swagger definition seemed to be valid but the UI would still show the error. I spent a lot of time trying to fix the root issue, but couldn't figure it out. I ended up disabling the validation in the UI, and since you're using SwashBuckle you can add this code to your startup configuration:

GlobalConfiguration.Configuration
    .EnableSwagger(c => {
        // your configuration code here
    })
    .EnableSwaggerUi(c => {
        // other ui configuration here

        c.DisableValidator();
    });

Again, this is not a true fix for your issue, but if you want the ugly red error in your UI to go away, this will do it.

Upvotes: -1

Helen
Helen

Reputation: 97540

There's one issue, but I'm not sure if that's why the validator complains. Anyway:

In OpenAPI (fka Swagger) 2.0, an operation cannot consume both form data and JSON/XML. This is because form data is described using in: formData parameters whereas JSON/XML is described using in: body parameters, and body and form parameters cannot exist together for the same operation. This will be possible in OpenAPI 3.0 (which is RC at the time of writing).

Upvotes: 1

Related Questions