OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Relative references to different directories always followed from the main openapi.yaml

Open
#3,848 13 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Issue: Bug Swagger-Parser
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?
Description

I have a spec with multiple references to external files and directories. It looks like that all the
references are treated as relative to the main openapi.yaml instead of being treated as related to the actual file who holds the reference

openapi-generator version

4.1.1 (installed from Brew)

OpenAPI declaration file content or url

https://github.com/lorthirk/kapua-openapi

Command line used for generation

openapi-generator generate -c openapi-generator.config.json -i openapi.yaml -g java

content of openapi-generator.config.json:

{
  "invokerPackage": "org.eclipse.kapua.app.rest.client",
  "apiPackage": "org.eclipse.kapua.app.rest.client.api",
  "modelPackage": "org.eclipse.kapua.app.rest.client.model",
  "groupId": "org.eclipse.kapua",
  "artifactId": "kapua-rest-client-java",
  "library": "jersey2",
  "dateLibrary": "java8"
}
Steps to reproduce

launch openapi-generator as described above

Command output:

[main] WARN  io.swagger.v3.parser.OpenAPIV3Parser - Exception while reading:
java.lang.RuntimeException: Unable to load RELATIVE ref: ./accessInfo.yaml path: /Users/claudio.mezzasalma/dev/git/lorthirk/kapua-openapi
	at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:239)
	at io.swagger.v3.parser.ResolverCache.loadRef(ResolverCache.java:119)
	at io.swagger.v3.parser.processors.ExternalRefProcessor.processRefToExternalSchema(ExternalRefProcessor.java:56)
	at io.swagger.v3.parser.processors.SchemaProcessor.processReferenceSchema(SchemaProcessor.java:202)
	at io.swagger.v3.parser.processors.SchemaProcessor.processSchema(SchemaProcessor.java:37)
	at io.swagger.v3.parser.processors.ResponseProcessor.processResponse(ResponseProcessor.java:52)
	at io.swagger.v3.parser.processors.OperationProcessor.processOperation(OperationProcessor.java:67)
	at io.swagger.v3.parser.processors.PathsProcessor.processPaths(PathsProcessor.java:83)
	at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:49)
	at io.swagger.v3.parser.OpenAPIV3Parser.readLocation(OpenAPIV3Parser.java:66)
	at io.swagger.parser.OpenAPIParser.readLocation(OpenAPIParser.java:16)
	at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:400)
	at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:448)
	at org.openapitools.codegen.cmd.Generate.run(Generate.java:399)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:60)
Caused by: java.lang.RuntimeException: Could not find ./accessInfo.yaml on the classpath
	at io.swagger.v3.parser.util.ClasspathHelper.loadFileFromClasspath(ClasspathHelper.java:31)
	at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:233)
	... 14 common frames omitted
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./accessInfo.yaml#/components/schemas/accessInfoListResult
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./accessInfo.yaml#/components/schemas/accessInfoListResult
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ../openapi.yaml#/components/responses/subjectUnauthorized
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ../openapi.yaml#/components/responses/kapuaError
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ../openapi.yaml#/components/parameters/scopeId
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./accessInfo.yaml#/components/schemas/accessInfoCreator
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./accessInfo.yaml#/components/schemas/accessInfoCreator
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./accessInfo.yaml#/components/schemas/accessInfoListResult
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./accessInfo.yaml#/components/schemas/accessInfoListResult
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ../openapi.yaml#/components/responses/subjectUnauthorized
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ../openapi.yaml#/components/responses/kapuaError
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: 10
Errors:
	-Unable to load RELATIVE ref: ./accessInfo.yaml path: /Users/claudio.mezzasalma/dev/git/lorthirk/kapua-openapi
Warnings:
	-Unable to load RELATIVE ref: ./accessInfo.yaml path: /Users/claudio.mezzasalma/dev/git/lorthirk/kapua-openapi

	at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:419)
	at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:448)
	at org.openapitools.codegen.cmd.Generate.run(Generate.java:399)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:60)
Related issues/PRs

#2412 seems similar
swagger-api/swagger-parser#1074

Suggest a fix

I guess the references should always be resolved starting from the file who holds the $ref... Unfortunately I don't know exactly where in the code the references are resolved (in swagger-parser maybe?).

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

Reproduce the failure with the linked kapua-openapi specification and the shown openapi-generator command. Start by tracing swagger-parser's RefUtils.readExternalRef and ResolverCache.loadRef, using the stack trace as the entry point. Done means external references resolve relative to the file containing each $ref and Java generation completes without the reported validation errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
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.