OpenAPITools / OpenAPITools/openapi-generator
[BUG] Generator tried to load relative ref from a non-existing path.
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
I have created some yaml files, structured like:
- [common]
- [data]
- components.yml
- [operation]
- auth.yml
- common.yml
- [data]
- [amp]
- amp.yml
Both common.yml and amp.yml refer to auth.yml, which refers to components.yml. See the following for details.
components.yml (Meta refers to ErrorCode):
ErrorCode:
type: integer
format: int64
description: 0 for NoError, 100 for AuthenticationFailed
Meta:
title: Meta
type: object
properties:
error_code:
$ref: ./component.yml#/ErrorCode
redirect:
title: Redirect
type: object
properties:
url:
type: string
auth.yml (oauth2_code2token response refers to Meta in components.yml):
oauth2_code2token:
get:
responses:
200:
description: OK
content:
application/json:
schema:
type: object
title: Code2TokenResponse
properties:
meta:
$ref: ../data/component.yml#/Meta
data:
common.yml (code2token refers to oauth2_code2token in auth.yml)
paths:
/oauth2/code2token:
$ref: ./operation/auth.yml#/oauth2_code2token
amp.yml (code2token refers to oauth2_code2token in auth.yml)
paths:
/oauth2/code2token:
$ref: ../common/operation/auth.yml#/oauth2_code2token
As above the only difference between amp.yml and common.yml is the relative reference path according to their different parent directories.
However, only common.yml can be successfully parsed and generated, while the amp.yml would generate the following error:
java.lang.RuntimeException: Unable to load RELATIVE ref: ../common/common/data/component.yml path: /autra/./api/amp
at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:220)
at io.swagger.v3.parser.ResolverCache.loadRef(ResolverCache.java:150)
at io.swagger.v3.parser.processors.ExternalRefProcessor.processRefToExternalSchema(ExternalRefProcessor.java:88)
at io.swagger.v3.parser.processors.SchemaProcessor.processReferenceSchema(SchemaProcessor.java:236)
at io.swagger.v3.parser.processors.SchemaProcessor.processPropertySchema(SchemaProcessor.java:138)
at io.swagger.v3.parser.processors.SchemaProcessor.processSchemaType(SchemaProcessor.java:77)
at io.swagger.v3.parser.processors.SchemaProcessor.processSchema(SchemaProcessor.java:62)
at io.swagger.v3.parser.processors.ComponentsProcessor.processSchemas(ComponentsProcessor.java:231)
at io.swagger.v3.parser.processors.ComponentsProcessor.processComponents(ComponentsProcessor.java:145)
at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:73)
at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:59)
at io.swagger.v3.parser.OpenAPIV3Parser.resolve(OpenAPIV3Parser.java:238)
at io.swagger.v3.parser.OpenAPIV3Parser.readContents(OpenAPIV3Parser.java:181)
at io.swagger.v3.parser.OpenAPIV3Parser.readLocation(OpenAPIV3Parser.java:97)
at io.swagger.parser.OpenAPIParser.readLocation(OpenAPIParser.java:16)
at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:637)
at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:695)
at org.openapitools.codegen.cmd.Generate.execute(Generate.java:503)
at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.RuntimeException: Could not find ../common/common/data/component.yml on the classpath
at io.swagger.v3.parser.util.ClasspathHelper.loadFileFromClasspath(ClasspathHelper.java:33)
at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:214)
... 19 common frames omitted
[main] WARN o.o.codegen.utils.ModelUtils - Failed to get the schema name: ../common/common/data/component.yml#/ErrorCode
[main] WARN o.o.codegen.utils.ModelUtils - Failed to get the schema name: ../common/common/data/component.yml#/ErrorCode
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:
-Unable to load RELATIVE ref: ../common/common/data/component.yml path: /autra/./api/amp
at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:668)
at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:695)
at org.openapitools.codegen.cmd.Generate.execute(Generate.java:503)
at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
This really confused me, for the string "common/common/data" does not even exist in my workspace.
I haved tried the followings:
- Run
ag "common/comon/data" .in my workspace, with no output at all. - Find string "common/common/data" in my Visual Studio Code, with no result.
- Run
docker system prune -ato remove all the cache, and force update the generator, tried to generate again, ending in the same error. - Run the generator image with sh as the substitution for the default entry point, and manually run
/usr/local/bin/docker-entrypoint.sh generatein the container, ending in the same error.
Now I have no idea about what happened... Could anyone help me about that?
Thanks.
openapi-generator version
7.0.1-SNAPSHOT
(Using the docker image)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the nested relative-reference case from the reported components.yml, auth.yml, common.yml, and amp.yml files. Start with RefUtils.readExternalRef and ResolverCache.loadRef, then trace how ExternalRefProcessor resolves the nested $ref path. Done means amp.yml resolves the same component chain as common.yml without constructing the duplicated common/common/data path.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100