Unable to parse Parameter $ref with cross-path references

Open
#2,116 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
java
Domain
api

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.

Description

P2
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.

Dominant language
Java
Stars
867
Forks
560
Avg merge
2d 21h
Merged PRs (30d)
7

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.

More from swagger-api/swagger-parser

All issues in swagger-api/swagger-parser

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.