swagger-api / swagger-api/swagger-parser
Unable to parse Parameter $ref with cross-path references
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 867
- Forks
- 560
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 7
Description
Description
When parsing the Parameters section from an operation, cross-path reference parameters are ignored. This is not consistent with other elements which are properly included in the final OpenAPI object.
swagger-parser version
Version 2.1.22
OpenAPI declaration file content or url
"/attribute/{namespace}":
delete:
description: |
Delete an attribute namespace and all attributes below.
This operation is the same as the one defined with [DELETE /attribute/{namespace}/_meta](#/Attributes/delete_attribute__namespace___meta).
parameters:
- $ref: "#/paths/~1attribute~1%7Bnamespace%7D/get/parameters/0"
Full spec file is here: https://github.com/APIs-guru/openapi-directory/blob/main/APIs/opensuse.org/obs/2.10.50/openapi.yaml
Generation Details
Use the following code:
OpenAPIParser openAPIV3Parser = new OpenAPIParser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setFlatten(true);
OpenAPI openAPI = openAPIV3Parser.readContents(Files.readString(Paths.get("openapi.yaml")), null, options).getOpenAPI();
PathItem pathItem = openAPI.getPaths().get("/attribute/{namespace}");
You can then check the Parameters list of the DELETE operation is empty instead of having one parameter.
Steps to reproduce
See above
Suggest a fix
The issue seems to be this code from the io.swagger.v3.parser.processors.ParameterProcessor class:
if (parameter.get$ref().startsWith("#") && parameter.get$ref().indexOf("#/components/parameters") <= -1) {
//TODO: Not possible to add warning during resolve doesn't accept result as an input. Hence commented below line.
//result.warning(location, "The parameter should use Reference Object to link to parameters that are defined at the OpenAPI Object's components/parameters.");
continue;
}
Which only includes refs that are from the components section. One possible fix is that it should just include parameters that are referencing other paths 'as is' rather than trying to solve it.
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
Start with io.swagger.v3.parser.processors.ParameterProcessor and reproduce the issue using the supplied OpenAPI YAML and Java parsing snippet with resolve and flatten enabled. Inspect how the $ref to another path is handled, then verify that the DELETE operation's Parameters list contains the referenced parameter rather than being empty.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100