OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Parser] ref's are handled improperly inside additionalProperties if the schema is an array and using multiple files

Open
#2,249 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

When having a $ref inside an array inside an additionalProperties section the parser doesn't seem to pick up the definition properly - especially when using multiple files

For example let's say you want to describe a schema like the following:

{
  "validations": {
    "first_name": [{
      "presence":true
    }]
  }
}

Where the validations object can contain a wildcard of properties - each of them an array of another specific schema. See the example below for a minimum failing case

openapi-generator version

Tested with 0.4.0-beta2 and the latest cli from docker

OpenAPI declaration file content or url

Check the following files:

# ==> api.yaml <==
openapi: 3.0.2
servers:
  - url: 'https://example.com'
info:
  title: Example API
  version: 1.0
paths:
  /test:
    $ref: './endpoints/endpoint.yaml#/collection'

# ==> definitions/definition.yaml <==
TestResponse:
  additionalProperties:
    type: array
    items:
      $ref: '#/TestDescription'

TestDescription:
  type: object
  properties:
    error:
      type: string

# ==> endpoints/endpoint.yaml <==
collection:
  get:
    operationId: 'test'
    responses:
      '200':
        content:
          application/json:
            schema:
              $ref: '../definitions/definition.yaml#/TestResponse'
Command line used for generation
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
    -i /local/api.yaml \
    -g openapi \
    -o /local/gen
Steps to reproduce

Run the generator against the example provided above.

The generated file will return the following error:

[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #/TestDescription
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #/TestDescription

although the TestDescription definition is right there in the file.

Not using an array, e.g.

TestResponse:
  additionalProperties:
    $ref: '#/TestDescription'

works as expected.

Suggest a fix

The following workaround seems to be working (see the two inline comments on what's changed):

# ==> api.yaml <==
openapi: 3.0.2
servers:
  - url: 'https://example.com'
info:
  title: Example API
  version: 1.0
paths:
  /test:
    $ref: './endpoints/endpoint.yaml#/collection'
# duplicating the schemas explicitly here
components:
  schemas:
    TestDescription:
      $ref: './definitions/definition.yaml#/components/schemas/TestDescription'

# ==> definitions/definition.yaml <==
TestResponse:
  additionalProperties:
    type: array
    items:
      $ref: '#/components/schemas/TestDescription'

# duplicating the main structure of components/schemas here as well
components:
  schemas:
    TestDescription:
      type: object
      properties:
        error:
          type: string

# ==> endpoints/endpoint.yaml <==
collection:
  get:
    operationId: 'test'
    responses:
      '200':
        content:
          application/json:
            schema:
              $ref: '../definitions/definition.yaml#/TestResponse'

i.e. defining the model on the main file, and also faking it's location in the sub-files seems to be okay. It is also picked up properly by multiple languages we tried including java, ruby, php and openapi

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the multi-file fixture described in api.yaml, endpoints/endpoint.yaml, and definitions/definition.yaml, then run the supplied Docker generation command with the openapi generator. Trace how the parser resolves the $ref under additionalProperties.items and review the ModelUtils warning. Done means the example generates without the Failed to get the schema name warning and resolves TestDescription correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, java, openapi
Domain
api, backend, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.