redskap / swagger-brake

Swagger contract checker for breaking API changes
Apache License 2.0
57 stars 16 forks source link

Required fields in nested definitions are being reported as breaking when they are not #54

Closed sumanswaroop closed 2 years ago

sumanswaroop commented 3 years ago

Consider these two spec files: Old

swagger: '2.0'
info:
  description: ABC
  version: 1.0.5
  title: Swagger Petstore
  termsOfService: 'http://swagger.io/terms/'
host: petstore.swagger.io
basePath: /v2
schemes:
  - https
  - http
paths:
  /pet/uploadImage:
    put:
      tags:
        - pet
      summary: Update an existing pet
      description: ''
      operationId: updatePet
      parameters:
        - in: body
          name: body
          description: Pet object that needs to be added to the store
          required: true
          schema:
            $ref: '#/definitions/Pet'
      responses:
        '400':
          description: Invalid ID supplied
definitions:
  Pet:
    type: object
    required:
      - id
    properties:
      id:
        type: integer
        format: int64

Lets add a new field in the body parameter Pet which is optional but a field inside that is required.

swagger: '2.0'
info:
  description: ABC
  version: 1.0.5
  title: Swagger Petstore
  termsOfService: 'http://swagger.io/terms/'
host: petstore.swagger.io
basePath: /v2
schemes:
  - https
  - http
paths:
  /pet/uploadImage:
    put:
      tags:
        - pet
      summary: Update an existing pet
      description: ''
      operationId: updatePet
      parameters:
        - in: body
          name: body
          description: Pet object that needs to be added to the store
          required: true
          schema:
            $ref: '#/definitions/Pet'
      responses:
        '400':
          description: Invalid ID supplied
definitions:
  Pet:
    type: object
    required:
      - id
    properties:
      id:
        type: integer
        format: int64
      breed:
        $ref: '#/definitions/Breed'
  Breed:
    type: object
    required:
      - name
    properties:
      name:
        type: string

This means that if Breed is passed in body then it must have name property. Or else we can send the request without the whole breed object.

This is being reported as breaking change. java -jar /usr/local/lib/swagger-brake-cli-2.2.0.jar --old-api=old.yml --new-api=new.yaml Loading old API from old.yml Loading new API from new.yaml Successfully loaded APIs Starting the check for breaking API changes Check is finished There were breaking API changes R007 Request parameter breed.name is required in PUT /pet/uploadImage Incase there already exist a optional parameter and we add new required fields inside that, then it qualifies as a breaking change.

sumanswaroop commented 3 years ago

Verified that swagger-diff tool doesn't report it as a breaking change.

galovics commented 2 years ago

Hi @sumanswaroop, I'm gonna check it, sorry for the long wait.

galovics commented 2 years ago

@sumanswaroop fixed. With the new 2.3.0 version that will be released pretty soon (might be even today), you can give it a try.