openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] validate command reports unused model when the model is referred for array items

Open yima77 opened this issue 2 years ago • 1 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When openapi-generator-cli validate is run on an OpenAPI in either YAML or JSON format, it reports a warning about an unused model when that model is referred by an array item. It is clearly an error since the model is referred so it isn't unused. The same warning doesn't appear when the model is referred by an object.

openapi-generator version

6.3.0, 7.2.0 and 7.3.0-SNAPSHOT

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  version: DEVELOPMENT
  title: Title
  description: |
    Sample API
paths:
  /api:
    get:
      operationId: accessAPI
      summary: Access the API
      description: API description
      requestBody:
        description: Information about the access
        content:
          application/json:
            schema:
              type: object
              required:
                - info
              properties:
                info:
                  type: array
                  description: A list of info items
                  items:
                    $ref: '#/components/schemas/Item'
      responses:
        '200':
          description: OK response
components:
  schemas:
    Item:
      type: object
      description: Info item
      properties:
        summary:
          type: string
          description: Summary
        detail:
          type: string
          description: Detail
Generation Details
Steps to reproduce

Put the OpenAPI spec in a YAML file and run openapi-generator-cli validate on it, such as:

$ openapi-generator-cli validate -i sample.yaml

It will generate the following output:

Validating spec (sample.yaml)
Warnings:
        - Unused model: Item

[info] Spec has 1 recommendation(s).
Related issues/PRs
Suggest a fix

yima77 avatar Jan 18 '24 20:01 yima77

The described issue still occurs in the latest master.

Please note that the shown input spec uses openapi: 3.1.0, which isn't fully supported yet by OpenAPI Generator. see e.g. #9083. When using openapi: 3.0.3 instead, the described problem does not occur. So you could use that as workaround for now.

For anyone who wants to work on this: The place where Item gets flagged as unused model is ModelUtils.getUnusedSchemas.

martin-mfg avatar Feb 12 '24 16:02 martin-mfg