Swagger-ui: response model only displayed if the response is a `$ref` into the `responses` object

Created on 18 Nov 2015  路  10Comments  路  Source: swagger-api/swagger-ui

It seems that how a response is defined affects whether it's model shows up in swagger-ui. And even when it does show up, the response headers are not shown.

This doesn't seem right.

screen shot 2015-11-18 at 12 26 56 am

Here is the code too:

swagger: '2.0'
info:
  version: 1.0.0
  title: Sample for testing display of reponse models
  description: |
    It seems like display of response models in swagger-ui depends on how they're defined.
    Particularly, whether they're referenced or in line. It should not matter.
basePath: /sample
produces:
  - application/json

paths:
  /test:
    get:
      summary: Example operation
      description: |
        All the different reponses should have an associated model ...
        The models are defined, but are they displayed?
      responses:
        200:
          description: This model is defined in-line, but since it's a 200, it shows up.
          schema:
            type: object
            properties:
              key:
                type: string
              value:
                type: string
        400:
          description: This model is defined with a reference to a definition object
          schema:
            $ref: '#/definitions/Error'
        403:
          $ref: '#/responses/Unauthorized'
        418:
          description: This model is defined in-line.
          schema:
            type: object
            properties:
              tea:
                type: string
                enum:
                  - earl grey
                  - sweet
                  - red rose
                  - green
              temp:
                type: string
                enum:
                  - hot
                  - iced

responses:
  Unauthorized:
    description: Only authorized users may make this request.
    schema:
      $ref: '#/definitions/Error'
    headers:
      WWW-Authenticate:
        type: string

definitions:
  Error:
    required:
      - code
      - message
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string

Most helpful comment

But if I specify object schema inline in responses:

paths:
  /test:
    get:
      summary: Example operation
      description: |
        All the different reponses should have an associated model ...
        The models are defined, but are they displayed?
      responses:
        #...
        500:
          $ref: '#/responses/ServerError'

# ...
responses:
  ServerError:
    description: Server failed to process your request.
    schema:
      type: object
      required: ["status", "message"]
      properties:
        status:
          type: string
          enum: ["error"]
        message:
          type: string

it fails with Object is not a primitive:

2016-08-02_21 42 43_1

Here is full spec for details: https://gist.github.com/rutsky/4f4cda9f6039ae604ae9c31ccc2337e4

All 10 comments

this might be caused by swagger-client issue 518 ...

hi, when can this be fixed? It's very common for a restful resource to just return a primitive type such as Integer.

This issue is still in the current release 2.1.4
Although it seems to be fixed in the demo at http://petstore.swagger.io ?!

Please try master, which is different from release 2.1.4

Looks working in master (225d18e5ae):

2016-08-02_21 34 03_1

But if I specify object schema inline in responses:

paths:
  /test:
    get:
      summary: Example operation
      description: |
        All the different reponses should have an associated model ...
        The models are defined, but are they displayed?
      responses:
        #...
        500:
          $ref: '#/responses/ServerError'

# ...
responses:
  ServerError:
    description: Server failed to process your request.
    schema:
      type: object
      required: ["status", "message"]
      properties:
        status:
          type: string
          enum: ["error"]
        message:
          type: string

it fails with Object is not a primitive:

2016-08-02_21 42 43_1

Here is full spec for details: https://gist.github.com/rutsky/4f4cda9f6039ae604ae9c31ccc2337e4

In case anyone is looking for a quick (and ugly) workaround for the "Object is not a primitive" message for responses with inline schemas mentioned by @rutsky (while this issue is fixed), you can change line 24703 at the very bottom of swagger-ui.js:

from:

if (this.router.api.models.hasOwnProperty(this.model.responseModel)) {

to:

if (_.isUndefined(value) && !_.isUndefined(this.model.schema)) {
                value = new Object();
                value.definition = this.model.schema;
                value.models = this.router.api.models;

                // avoid duplicate inline model names
                var modelName = 'inline_model';
                var name = modelName;
                var done = false; var counter = 0;
                while(!done) {
                    if(typeof this.router.api.models[name] === 'undefined') {
                        done = true;
                        break;
                    }
                    name = modelName + '_' + counter;
                    counter ++;
                }               

                value.name = name;
            }

            if (this.router.api.models.hasOwnProperty(this.model.responseModel) || !_.isUndefined(value)) {

P.S: I tested it with JSON only.

+1

@juan-quiver In swagger-ui v2.2.10 it's the line 25325 and your patch works there (with YAML file). Thanks!

PS. I'm wondering why it it's not in the repo.

This should no longer be an issue with the new UI.

Was this page helpful?
0 / 5 - 0 ratings