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

2

6

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?

Glissando answered 18/4, 2017 at 12:29 Comment(0)
M
1

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).

Microscopic answered 18/4, 2017 at 21:16 Comment(2)
Thanks for the reply. I will try specifying JSON only and see if that makes any difference.Glissando
I got the same error and solved it this way: open your swagger file in Swagger UI and transform it to Swagger 3.0. Then upload 3.0 file to your hosted project. The error “instance type (string) does not match any allowed primitive type…” should be vanished.Discant
H
-1

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.

Harden answered 16/4, 2019 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.