Skip to content

[HTML] Unable to parse multi-level $refs #1437

Description

@john3300
Description

My API spec is split across two files, test-base.yaml and test-common.yaml. In test-base.yaml I have my paths defined with responses. Some of my responses contain $ref links to test-common.yaml. In test-common.yaml I have both responses and definitions sections. The responses have another layer of $ref to the definitions within the same file. The parser is seemingly unable to handle this second layer of $ref.

Here's the output I get:

[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./test-common.yaml#/responses/notFound
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./test-common.yaml#/responses/serverError
Exception in thread "main" org.openapitools.codegen.SpecValidationException: There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI).
 | Error count: 1, Warning count: 0
Errors: 
	-Invalid ref format: ./test-common.yaml#/responses/notFound#/definitions/error

	at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:569)
	at org.openapitools.codegen.cmd.Generate.run(Generate.java:348)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:62)

Disabling the spec validation does not help.

openapi-generator version

3.3.2

OpenAPI declaration file content or url

test-base.yaml

swagger: '2.0'
info:
  title: Test
  description: Test YAML
  version: '1.0'
schemes:
  - http
host: http://localhost
paths:
  /employees/{id}:
    get:
      operationId: getEmployeeById
      produces:
        - application/json
      parameters:
        - name: id
          in: path
          type: string
          required: true
      responses:
        200:
          description: Success
          schema:
            $ref: '#/definitions/employee'
        404:
          $ref: './test-common.yaml#/responses/notFound'
        500:
          $ref: './test-common.yaml#/responses/serverError'
definitions:
  employee:
    type: object
    properties:
      name:
        type: string

test-common.yaml

responses:
  notFound:
    description: Not found
    schema:
      $ref: '#/definitions/error'
  serverError:
    description: Server error
    schema:
      $ref: '#/definitions/error'
definitions:
  error:
    type: object
    properties:
      msg:
        type: string
Command line used for generation

java -jar openapi-generator-cli-3.3.2.jar generate -g html -i test-base.yaml -o html

Suggest a fix/enhancement

Other tools such as the Swagger UI don't have a problem with this, so I believe this tool should be able to resolve multi-layer $refs like this as well.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions