spectral
spectral copied to clipboard
Subtypes of polymorphic types are incorrectly flagged as unused definitions
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
-
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 -
Run this CLI command
spectral lint example.yaml -
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
Same issue here.
@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
- all affected schemas would have to be adjusted, and
- openapi-generator does not really support
oneOfwell 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 :)