openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open timofriedlberlin opened this issue 4 years ago • 14 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [ ] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
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:

  • 5.0.0 (working)
  • 5.0.1 (not working)
  • 5.2.1 (not working)
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.

timofriedlberlin avatar Sep 03 '21 09:09 timofriedlberlin

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

ineednousername avatar Sep 03 '21 09:09 ineednousername

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

Blubb324 avatar Sep 04 '21 12:09 Blubb324

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 :)

timofriedlberlin avatar Sep 04 '21 17:09 timofriedlberlin

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

Blubb324 avatar Sep 09 '21 15:09 Blubb324

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

Donorlin avatar Sep 10 '21 19:09 Donorlin

@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>

ineednousername avatar Sep 11 '21 15:09 ineednousername

Any news about this, please?

Donorlin avatar Feb 04 '22 11:02 Donorlin

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

tim-hilt avatar Aug 18 '22 15:08 tim-hilt

This is still broken in v6.4.0

Eufix avatar Mar 16 '23 13:03 Eufix

broken in v6.6.0 too

muswain avatar Aug 02 '23 14:08 muswain

Any news about this, please?

jorgerod avatar Aug 03 '23 06:08 jorgerod

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.

mjuergens-cg avatar Oct 12 '23 10:10 mjuergens-cg

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 ?

B-apt avatar Jun 25 '24 13:06 B-apt

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

wing328 avatar Jun 25 '24 14:06 wing328