swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Swagger UI when working with Swagger 2.0 document wrongly interprets 'required' attribute on schema

Open xprzemekw opened this issue 5 years ago • 1 comments

Q&A (please complete the following information)

  • OS: Windows
  • Browser: Chrome, Firefox
  • Version: Chrome:89, Firefox:86
  • Method of installation: Bundled with Swashbuckle.AspNetCore
  • Swagger-UI version: 3.42.0
  • Swagger/OpenAPI version: Swagger 2.0

Content & configuration

Example Swagger/OpenAPI definition:

swagger: '2.0'
info:
  title: Sample API
  description: 'A sample application with Swagger, Swashbuckle, and API versioning.'
  version: '1.0'
paths:
  /api/v1/create:
    post:
      tags:
        - Api
      consumes:
        - application/json
      produces:
        - application/json
      parameters:
        - in: body
          name: body
          schema:
            $ref: '#/definitions/TestRequest'
      responses:
        '200':
          description: Success
          schema:
            $ref: '#/definitions/TestResponse'
definitions:
  TestRequest:
    type: object
    properties:
      test:
        type: string
      testRequestData:
        $ref: '#/definitions/TestRequestData'
  TestRequestData:
    required:
      - testRequestDataItem
    type: object
    properties:
      testRequestDataItem:
        type: string
  TestResponse:
    type: object
    properties:
      status:
        type: string

Swagger-UI configuration options:

SwaggerUI({
  "urls": [
    {
      "url": "/swagger/v1/swagger.json",
      "name": "V1"
    }
  ],
  "deepLinking": false,
  "displayOperationId": false,
  "defaultModelsExpandDepth": 1,
  "defaultModelExpandDepth": 1,
  "defaultModelRendering": "example",
  "displayRequestDuration": false,
  "docExpansion": "list",
  "showExtensions": false,
  "showCommonExtensions": false,
  "supportedSubmitMethods": [ "get", "put", "post", "delete", "options", "head", "patch", "trace" ]
})
N/A

Describe the bug you're encountering

Swagger UI when working with Swagger 2.0 document wrongly interprets 'required' attribute on schema. It wrongly applies it to parent instead of child or misbehaves at all.

To reproduce...

Steps to reproduce the behavior:

  1. Use swagger ui to open spec document
  2. use 'POST ​/api​/v1​/create' to test
  3. Below request does not work when clicking 'Execute', no output on console, no any other user visible message

{ "test": "string", }

Note: testRequestData is not set as required in swagger 2.0 spec yet swagger ui client code for Swagger 2.0 document rejects request

  1. Surprisingly below request works

Swagger UI submits data to server, yet 'testRequestData/testRequestDataItem' is not present

{ "test": "string", "testRequestData": { } }

Expected behaviour

API requests on client side are validated as per Swagger 2.0 specification or not validated at all. NB: For OAS 3.0 document it looks 'required' is not checked, swagger ui POSTs data regardless presence of 'required' elements.

Screenshots

N/A

Additional context or thoughts

N/A

xprzemekw avatar Mar 22 '21 19:03 xprzemekw

This has probably been fixed in one of the latest versions. See at: https://gist.githubusercontent.com/mathis-m/b50c46425045fa936ce1a5df6c11122a/raw/ce3240f57e98e47b3c34aa0aad27cf1253b2dd39/pw0RRtsMxx.txt image

mathis-m avatar Apr 09 '21 10:04 mathis-m