swagger-api / swagger-api/swagger-codegen

[JAVA] allOf Only Works in YAML Format When Referencing External Document

Open
#7,666 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When an allOf key is used and the $ref inside of it does not reference a definition contained within the body of the document the allOf it is being used in, swagger-codegen is unable to process the file and fails. If however, the allOf key refers to a definition inside of the body, and that definition refers to an external document, the allOf key is processed accordingly. Furthermore, this ONLY happens when code is being generated from an API schema that is in the JSON file format, as opposed to YAML. If the code is being generated from an API schema that is in the YAML file format, it will be able to process these exact same references correctly.

Swagger-codegen version

2.3.1

Swagger declaration file content or url

The below does not work however, ONLY when generating from an API schema that references this component when the API schema is in JSON format. If the same API is converted to YAML format, it is able to resolve this very same component, without any issue and generates Java.

{
	"$schema": "http://json-schema.org/draft-04/schema#",
	"type": "object",
	"definitions": {
		"hr.personalContact.add": {
			"title": "hr.personalContact.add",
			"type": "object",
			"additionalProperties": false,
			"allOf": [
				{
					"$ref": "../../../../common/core/event/eventHeaderRequestType_v01.json"
				}
                         ]
                  }
         }
Command line used for generation

swagger-codegen generate -i apis/hr/personal-contacts.v2/worker-personal-contacts-v2-design.json -l java -o apis/hr/personal-contacts.v2/java --type-mappings indicatorType=boolean,dateType=Date,dateTimeType=Date --invoker-package hr.v2.PersonalContact --model-package hr.v2.PersonalContact.model

Steps to reproduce

Any definition or component that references allOf via a filepath rather than a definition, is unable to be resolved. The error comes back as:

Exception in thread "main" java.lang.RuntimeException: Unable to load RELATIVE ref: ../../../../common/core/event/eventHeaderRequestType_v01.json
        at io.swagger.parser.util.RefUtils.readExternalRef(RefUtils.java:146)
        at io.swagger.parser.ResolverCache.loadRef(ResolverCache.java:112)
        at io.swagger.parser.processors.ExternalRefProcessor.processRefToExternalDefinition(ExternalRefProcessor.java:37)
        at io.swagger.parser.processors.ExternalRefProcessor.processRefToExternalDefinition(ExternalRefProcessor.java:90)
        at io.swagger.parser.processors.ModelProcessor.processRefModel(ModelProcessor.java:92)
        at io.swagger.parser.processors.ModelProcessor.processModel(ModelProcessor.java:33)
        at io.swagger.parser.processors.ParameterProcessor.processParameters(ParameterProcessor.java:78)
        at io.swagger.parser.processors.OperationProcessor.processOperation(OperationProcessor.java:26)
        at io.swagger.parser.processors.PathsProcessor.processPaths(PathsProcessor.java:101)
        at io.swagger.parser.SwaggerResolver.resolve(SwaggerResolver.java:50)
        at io.swagger.parser.SwaggerParser.read(SwaggerParser.java:67)
        at io.swagger.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:431)
        at io.swagger.codegen.cmd.Generate.run(Generate.java:283)
        at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:35)
Caused by: java.lang.RuntimeException: Could not find ../../../../common/core/event/eventHeaderRequestType_v01.json on the classpath
        at io.swagger.parser.util.ClasspathHelper.loadFileFromClasspath(ClasspathHelper.java:31)
        at io.swagger.parser.util.RefUtils.readExternalRef(RefUtils.java:141)

It should be note that this ONLY occurs when using a JSON file format to generate. If I used YAML, which refers to these same files, it is able to process correctly.

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

Start with the failing stack trace in RefUtils.java, ResolverCache.java, and ExternalRefProcessor.java, then reproduce the issue with the supplied JSON schema and swagger-codegen generate command. Compare JSON and YAML parsing of the external allOf reference; done means the JSON case resolves ../../../../common/core/event/eventHeaderRequestType_v01.json and generates Java successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.