Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WARNING: definition is not used anywhere if a definition is used in composition using allOf. #2304

Open
folago opened this issue Jun 8, 2020 · 1 comment
Labels
discriminator Related to polymorphic types enhancement validate spec Related to github.com/go-openapi/validate

Comments

@folago
Copy link

folago commented Jun 8, 2020

Problem statement

swagger validate returns WARNING: definition [...] is not used anywhere if a definition is used in composition using allOf.

Is this the expected behaviour?
We are getting more strict against warnings in our CI and as far as I understood this is the right way to do things. But then why there is this warning?

Swagger specification

swagger: "2.0"

info:
  title: Sample API
  description: 'API description in Markdown.'
  version: '1.0.0'

host: 'api.example.com'

basePath: '/v1'

schemes:
  - https
    
paths:
  /pets:
    get:
      summary: Returns pets.
      description: Optional extended description in Markdown.
      produces:
        - application/json
      responses:
        200:
          description: OK
          schema:
            type: object
            required: 
              - data
            properties:
              data:
                $ref: '#/definitions/Pet'


definitions:
  Pet:
    type: object
    required:
      - pet_type
    properties:
      pet_type:
        type: string
    discriminator: pet_type
  Dog:     # "Dog" is a value for the pet_type property (the discriminator value)
    allOf: # Combines the main `Pet` schema with `Dog`-specific properties 
      - $ref: '#/definitions/Pet'
      - type: object
        # all other properties specific to a `Dog`
        properties:
          bark:
            type: boolean
          breed:
            type: string
            enum: [Dingo, Husky, Retriever, Shepherd]
  Cat:     # "Cat" is a value for the pet_type property (the discriminator value)
    allOf: # Combines the main `Pet` schema with `Cat`-specific properties 
      - $ref: '#/definitions/Pet'
      - type: object
        # all other properties specific to a `Cat`
        properties:
          hunts:
            type: boolean
          age:
            type: integer

Steps to reproduce

Run swagger validate on a file containing the specs above.

It gives two warnings:

2020/06/08 13:52:07 - WARNING: definition "#/definitions/Dog" is not used anywhere
2020/06/08 13:52:07 - WARNING: definition "#/definitions/Cat" is not used anywhere

Environment

swagger version: v0.23.0

go version: go1.12.17 darwin/amd64

OS: MacOS 10.15.4

@fredbi fredbi added validate spec Related to github.com/go-openapi/validate discriminator Related to polymorphic types labels Jun 29, 2020
@fredbi
Copy link
Contributor

fredbi commented Jun 29, 2020

@folago yes the validator (github.com/go-openapi/validate) is not aware of special rules with discriminated types.
This is indeed a shortcoming and we shouldn't have this warning (since subtypes are indirectly used through their base type).
There are a handful of other validation rules specifically for discriminated types that should be implemented.
A PR fixing this would definitely be welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discriminator Related to polymorphic types enhancement validate spec Related to github.com/go-openapi/validate
Projects
None yet
Development

No branches or pull requests

2 participants