spectral icon indicating copy to clipboard operation
spectral copied to clipboard

Subtypes of polymorphic types are incorrectly flagged as unused definitions

Open ggoodvmw opened this issue 4 years ago • 2 comments

Describe the bug When a polymorphic type is "subtyped" using allOf, and only the parent type is actually used in an operation, the subtype is incorrectly flagged as being unused.

To Reproduce Using Spectral 5.9.1

  1. Given this OpenAPI/AsyncAPI document:

     swagger: "2.0"
     info:
       description: "Bug example"
       version: "1.0.0"
       title: "Swagger Petstore"
       termsOfService: "http://swagger.io/terms/"
       contact:
         email: "[email protected]"
       license:
         name: "Apache 2.0"
         url: "http://www.apache.org/licenses/LICENSE-2.0.html"
     host: "petstore.swagger.io"
     basePath: "/v2"
     tags:
     - name: "pet"
       description: "Everything about your Pets"
       externalDocs:
         description: "Find out more"
         url: "http://swagger.io"
     - name: "store"
       description: "Access to Petstore orders"
     - name: "user"
       description: "Operations about user"
       externalDocs:
         description: "Find out more about our store"
         url: "http://swagger.io"
     schemes:
     - "https"
     - "http"
     paths:
       /pet:
         post:
           tags:
           - "pet"
           summary: "Add a new pet to the store"
           description: "Add a new pet to the store"
           operationId: "addPet"
           consumes:
           - "application/json"
           - "application/xml"
           produces:
           - "application/xml"
           - "application/json"
           parameters:
           - in: "body"
             name: "body"
             description: "Pet object that needs to be added to the store"
             required: true
             schema:
               $ref: "#/definitions/Pet"
           responses:
             "200":
               description: "Success"
             "405":
               description: "Invalid input"
     definitions:
       Pet:
         type: "object"
         required:
         - "name"
         - "photoUrls"
         - "petType"
         discriminator: petType
         properties:
           petType:
             type: "string"
           id:
             type: "integer"
             format: "int64"
       Cat:
         type: "object"
         allOf:
           - $ref: "#/definitions/Pet"
           - properties:
               meowVolume:
                 type: integer
    
  2. Run this CLI command

     spectral lint example.yaml
    
  3. See error

     OpenAPI 2.0 (Swagger) detected
    
     /tmp/example.yaml
      70:7  warning  oas2-unused-definition  Potentially unused definition has been detected.  definitions.Cat
    
     ✖ 1 problem (0 errors, 1 warning, 0 infos, 0 hints)
    

Expected behavior I would expect that "Cat" is not flagged as an unused definition. Its "parent" type "Pet" is used in the POST API, so it's legal to pass a Cat to the POST.

Environment (remove any that are not applicable):

  • OS: MacOS Big Sur

ggoodvmw avatar Jul 01 '21 23:07 ggoodvmw

Same issue here.

GoossensMichael avatar Jan 25 '23 13:01 GoossensMichael

@P0lip, any idea whether the detection algorithm can be adjusted to avoid this kind of false positives? This issue has also been bugging us for some time. We found that it can be worked around in the schema by using oneOf instead of allOf to model the polymorphism. However, this is less than optimal for us because

  1. all affected schemas would have to be adjusted, and
  2. openapi-generator does not really support oneOf well from our experience, and we rely on this to generate our models from the schemas.

You might argue "okay, just don't use the oas3-unused-component rule", but unfortunately this is prescribed by our company and we cannot alter the ruleset easily. So a "proper" fix would be really nice :)

markusdlugi avatar Feb 05 '24 10:02 markusdlugi