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
20.6k stars 6.29k forks source link

[BUG][JAVA] Regression "-Unable to load RELATIVE ref:" #10320

Open timo-klarshift opened 2 years ago

timo-klarshift commented 2 years ago

Bug Report Checklist

Description

This is a regression of the bug described here: https://github.com/swagger-api/swagger-parser/issues/505

Its working in 5.0.0 but is broken in 5.0.1 and also in the latest version 5.2.1

[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./openapi/model.yaml#/components/responses/FooResponse
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: 17
Errors: 
        -Unable to load RELATIVE ref: openapi/openapi/model.yaml path: /home/$user/$project
Warnings: 
        -Unable to load RELATIVE ref: openapi/openapi/model.yaml path: /home/$user/$project

        at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:541)
        at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:568)
        at org.openapitools.codegen.cmd.Generate.execute(Generate.java:424)
        at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
├── openapi
│   ├── model.yaml
└── openapi.yaml
...
$ref: ./openapi/model.yaml#/components/parameters/XXX
...
openapi-generator version

Broken versions probably everything > 5.0.0 I tested:

Generation Details
npx openapi-generator-cli generate \
    -i openapi.yaml \
    -g typescript-axios \
    -o ${OUTDIR} \
    --additional-properties=ngVersion=6.1.7,npmName=restClient,supportsES6=true,npmVersion=6.9.0,withInterfaces=true
Related issues/PRs

Here are all pull-requests between 5.0.0 and 5.0.1

https://github.com/OpenAPITools/openapi-generator/pulls?q=is%3Amerged+is%3Apr+milestone%3A5.0.1++

Suggest a fix

It could temporary be fixed by dereferencing everything into a single file.

ineednousername commented 2 years ago

Just a note: I have the same issue using the maven plugin > 5.0.0 with version 5.0.0 it still works With a similar setup

   <groupId>org.openapitools</groupId>
   <artifactId>openapi-generator-maven-plugin</artifactId>
   <version>${openapi-generator-maven-plugin.version}</version>
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: 17
Errors: 
    -Unable to load RELATIVE ref: openapi/openapi/model.yaml path: /home/$user/$project
Warnings: 
    -Unable to load RELATIVE ref: openapi/openapi/model.yaml path: /home/$user/$project
Blubb324 commented 2 years ago

Hey guys! I could reproduce it. Can you please try to set the ref in quotes:

...
$ref: './openapi/model.yaml#/components/parameters/XXX'
...

Tested with 5.2.1 In my case it was doing the magic. Greetings from Berlin. :)

timo-klarshift commented 2 years ago

Hey guys! I could reproduce it. Can you please try to set the ref in quotes:

...
$ref: './openapi/model.yaml#/components/parameters/XXX'
...

Tested with 5.2.1 In my case it was doing the magic. Greetings from Berlin. :)

I will try this at beginning of next week. This sounds quite strange to me as YAML would not require quotes there technically. So this sound still like a bug to me and I would prefer to address this properly :)

Blubb324 commented 2 years ago

After reading your error message again and following the code it leads me to following position:

org.openapitools.codegen.config.CodegenConfigurator > io.swagger.parser.OpenAPIParser

As you can see the OpenAPIParser.java is part of swagger.io so their parser is used.

Reading the spec from swagger.io shows, that they are doing it with quotes: https://swagger.io/docs/specification/using-ref/

If you want to change that behaviour you can create ticket here: https://github.com/swagger-api

Donorlin commented 2 years ago

Can confirm. Any relative path deeper than "../XYZ" ("../../XYZ", "../../../XYZ" and so on) is not working with version greater than 5.0.0

ineednousername commented 2 years ago

@Blubb324

thank you for suggesting this workaround, I will check this out.

Reading the spec from swagger.io shows, that they are doing it with quotes: https://swagger.io/docs/specification/using-ref/

indeed they use quotes everywhere in the examples however they also mention this in the documentation.

Note: When using local references such as #/components/schemas/User in YAML, enclose the value in quotes: '#/components/schemas/User'. Otherwise it will be treated as a comment.

I am not sure why they would mention this if you would be required to use quotes. Of course in the example above, they use it, since it is required otherwise it would be a comment in yaml.

As you can see the OpenAPIParser.java is part of swagger.io so their parser is used.

what I also wonder is why swagger ui has no problems interpreting, the same relative yaml without quotes, that this parser from swagger seem to struggle with resolving correctly. Also in 5.0.0 it was still working with the generator plugin so I guess we have to check if the swagger parser library changed between 5.0.0 and 5.0.1, maybe the problem was introduced there.

Indeed this was changed. <swagger-parser-version>2.0.23</swagger-parser-version> to <swagger-parser-version>2.0.24</swagger-parser-version>

Donorlin commented 2 years ago

Any news about this, please?

tim-hilt commented 1 year ago

Having the same issue on v5.1.1. referencing ../../model.yml makes the generation fail.

Eufix commented 1 year ago

This is still broken in v6.4.0

muswain commented 11 months ago

broken in v6.6.0 too

jorgerod commented 11 months ago

Any news about this, please?

mjuergens-cg commented 8 months ago

Is the original issue described by @timo-klarshift still relevant? There is a reference to this bug: https://github.com/swagger-api/swagger-parser/issues/505 which has been fixed in 4/20 and the fixed swagger-parser also seems to be part of the later openapi-generator versions.

As a side node, we were already running into:

java.lang.RuntimeException: Unable to load RELATIVE ref: ..\components\

This could be solved by changing all backslash occurrences () with slash occurrences.

Also we were running into:

[main] WARN o.o.codegen.utils.ModelUtils - #/components/schemas/accounts_{id} is not defined

This could be solved by not using { } in file naming, e.g., rename paths/accounts_{id}.yaml to paths/accounts_id.yaml

But I am not sure if these challenges are in the scope of the original issue (besides a similar error message). Other than that, version 7.0.1 is working fine for us.

B-apt commented 1 week ago

Bug still present with org.openapitools:openapi-generator-maven-plugin:7.6.0 Using specifications coming from another company, I can't really modify the original files, so not sure how I could circumvent this bug in my case. By the way, IntelliJ also show an Unresolved reference until I add the quotes around the reference. So I wonder, is this a bug on openapi generator or is it the desired behaviour of OpenAPi specifications itself ?

wing328 commented 1 week ago

you may want to try the workaround mentioned in https://github.com/OpenAPITools/openapi-generator/issues/8844#issuecomment-789714081