OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.15k stars 6.41k forks source link

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

Open lorthirk opened 4 years ago

lorthirk commented 4 years ago

Bug Report Checklist

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?).

auto-labeler[bot] commented 4 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

jmini commented 4 years ago

OpenAPI-Generator is relying on Swagger-Parser. If they have an issue (which seems to be the case), then there is nothing we can do.

You need to work on a solution for the issue in the Upstream project, then we can see how to integrate it.

lorthirk commented 4 years ago

I see. As you can see, I already linked an issue on their side. Thanks anyway!

jhellingman commented 4 years ago

Could it be that this issue is resolved in Swagger-Parser now (https://github.com/swagger-api/swagger-parser/pull/1343), and updating the pom to refer to swagger-parser-version 2.0.18 would import that fix here.

lorthirk commented 4 years ago

@jhellingman it looks like swagger-api/swagger-parser#1343 has been merged on March, 20th while 2.0.18 has been released on February, 23rd. Are you sure that that version would include that fix?

jhellingman commented 4 years ago

Not sure, but in that case we may have to wait for 2.0.19.

RaduFurnea commented 4 years ago

Having the same problem myself. Seems like 2.0.19 is out, should that fix this issue if it's added to openapi-generator?

squ1d123 commented 3 years ago

Any update on this? I'm still experiencing this issue with version 5.0.1

gvlekke commented 3 years ago

5.2.0 same problem :(

ydmitry commented 3 years ago

Any news on this issue?

AlexandreMELIN commented 2 years ago

Hi, Any news on this topic. I ran into the same problems in 5.4.0. Is there any progress or features planned for this problem ?

mhays118 commented 1 year ago

Also ran into this issue on 6.0.1 using the java-camel generator.

Marcanta commented 1 month ago

Hi, Is there some news ? I'm still experienced that in 7.3.0...