Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External Refs with same name are ignored #2055

Open
anthochristen opened this issue Feb 9, 2024 · 1 comment
Open

External Refs with same name are ignored #2055

anthochristen opened this issue Feb 9, 2024 · 1 comment

Comments

@anthochristen
Copy link

anthochristen commented Feb 9, 2024

When external referenced components have the same name but different definitions, only the first is considered, the rest seem to be ignored. This seems to be because of the way the JSON pointers are used to derive the component name in Refutils.

RefUtils.java

   if (definitionPath != null) { //the name will come from the last element of the definition path

"the name will come from the last element of the definition path" - I couldn't find any documentation as to why this is done, nor did I see any mention of this in this spec.
This could likely cause issues as the definitions with same names are allow if the it is provided in-line.

P.S: Originally identified by @crankydillo.

openapi.json

{
    "openapi": "3.0.3",
    "info": {
        "version": "1.0.0",
        "title": "OpenAPI Service",
        "description": "Sample OpenAPI generated swagger contract"
    },
    "paths": {
        "/a-path": {
            "post": {
                "operationId": "ignoredRefPost",
                "parameters": [
                    { "$ref": "./external_ref_1.json#/limit" }
                ],
                "requestBody": { "$ref": "external_ref_1.json#/a-request" },
                "responses": {
                    "200": {
                        "$ref": "external_ref_1.json#/a-response"
                    },
                    "default": {
                        "$ref": "#/components/responses/default-string"
                    }
                }
            },
            "put": {
                "operationId": "ignoredRefPut",
                "parameters": [
                    { "$ref": "./external_ref_2.json#/limit" }
                ],
                "requestBody": { "$ref": "external_ref_2.json#/a-request" },
                "responses": {
                    "200": {
                        "$ref": "external_ref_2.json#/a-response"
                    },
                    "default": {
                        "$ref": "#/components/responses/default-string"
                    }
                }
            }
        }
    },
    "components": {
        "responses": {
            "default-string": {
                "description": "some string",
                "content": {
                    "text/plain": {
                        "schema": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    }
}

external_ref1.json

{
  "limit": {
    "name": "limit",
    "description": "There can be only 1!",
    "in": "query",
    "schema": {
      "type": "integer",
      "maximum": 1,
      "minimum": 0
    }
  },
 "a-request": {
   "required": true,
   "description": "a string req",
   "content": {
     "text/plain": {
       "schema": {
         "type": "string"
       }
     }
   }
},
  "a-response": {
    "description": "some string response",
    "content": {
      "text/plain": {
        "schema": {
          "type": "string"
        }
      }
    }
  }
}

external_ref_2.json

{
  "limit": {
    "name": "limit",
    "description": "There can be only 2!",
    "in": "query",
    "schema": {
      "type": "integer",
      "maximum": 2,
      "minimum": 0
    }
  },
  "a-response": {
    "description": "some string response",
    "content": {
      "appilication/json": {
        "schema": {
          "schema": {
            "$ref": "#/an-object"
            }
        }
      }
    }
  },
  "an-object": {
    "type": "object",
    "properties": {
      "a-prop": {
        "type": "string"
      }
    }
  },
  "a-request": {
    "required": true,
    "description": "a JSON req",
    "content": {
      "application/json": {
        "schema": {
          "$ref": "#/an-object"
        }
      }
    }
  }
}

Main.java

ParseOptions options = new ParseOptions();
options.setResolve(true);
OpenAPI openAPI = new OpenAPIV3Parser().read("openapi.json", null, options);
System.out.println("Parameters: " + openAPI.getComponents().getParameters().keySet());
System.out.println("Responses: " + openAPI.getComponents().getResponses().keySet());
System.out.println("Requests: " + openAPI.getComponents().getRequestBodies().keySet());
@kota65535
Copy link

Same here. Is this an expected behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants