Skip to content

[BUG] "Unused model" warning for discriminator schemas from validate --recommend #4193

@kevinoid

Description

@kevinoid

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

Validating an OpenAPI 2 or 3 document which uses discriminator with --recommend will produce an "Unused model" warning for schemas which are only referenced by discriminator.

openapi-generator version

4.1.3

OpenAPI declaration file content or url
(Click to expand) OpenAPI 2 example (from the OpenAPI 2.0 spec with addition of `paths` for completeness).
swagger: '2.0'
info:
  title: Discriminator example from OpenAPI 2.0 specification
  version: '1.0.0'
definitions:
  Pet:
    type: object
    discriminator: petType
    properties:
      name:
        type: string
      petType:
        type: string
    required:
    - name
    - petType
  Cat:
    description: A representation of a cat
    allOf:
    - $ref: '#/definitions/Pet'
    - type: object
      properties:
        huntingSkill:
          type: string
          description: The measured skill for hunting
          default: lazy
          enum:
          - clueless
          - lazy
          - adventurous
          - aggressive
      required:
      - huntingSkill
  Dog:
    description: A representation of a dog
    allOf:
    - $ref: '#/definitions/Pet'
    - type: object
      properties:
        packSize:
          type: integer
          format: int32
          description: the size of the pack the dog is from
          default: 0
          minimum: 0
      required:
      - packSize
paths:
  /pets:
    get:
      operationId: getPets
      responses:
        '200':
          description: Success
          schema:
            type: array
            items:
              $ref: '#/components/schemas/Pet'
(Click to expand) OpenAPI 3 example (from the OpenAPI 3.0.2 spec with addition of `paths` for completeness).
openapi: '3.0.2'
info:
  title: OpenAPI 3.0.2 discriminator example
  version: '1.0.0'
servers:
- url: https://example.com/api
components:
  schemas:
    Pet:
      type: object
      required:
      - petType
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType
        mapping:
          dog: Dog
    Cat:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Cat`
        properties:
          name:
            type: string
    Dog:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Dog`
        properties:
          bark:
            type: string
    Lizard:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Lizard`
        properties:
          lovesRocks:
            type: boolean

paths:
  /pets:
    get:
      operationId: getPets
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Pet'
Command line used for generation
./openapi-generator-cli.jar validate --recommend -i openapi.yaml
Steps to reproduce
wget http://central.maven.org/maven2/org/openapitools/openapi-generator-cli/4.1.3/openapi-generator-cli-4.1.3.jar -O openapi-generator-cli.jar
chmod +x openapi-generator-cli.jar
vim openapi.yaml  # Paste example from above
./openapi-generator-cli.jar validate --recommend -i openapi.yaml

This should produce the following output:

Validating spec (openapi.yaml)
Warnings: 
	-Unused model: Cat
	-Unused model: Dog
	-Unused model: Lizard

[info] Spec has 3 recommendation(s).
Related issues/PRs

Possibly related: swagger-api/swagger-editor#765

Note: This issue also occurs with allOf, anyOf, oneOf without discriminator. Let me know if I should open a separate issue about those.

Thanks for considering,
Kevin

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions