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

Normalize relative ref paths to avoid duplicating schemas #2105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import io.swagger.v3.parser.ResolverCache;
import io.swagger.v3.parser.models.RefFormat;
import io.swagger.v3.parser.models.RefType;
import io.swagger.v3.parser.util.RefUtils;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -86,6 +88,18 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
return renamedRef;
}

RefFormat format = computeRefFormat($ref);
if (format.equals(RefFormat.RELATIVE)) {
String normalizedRef = Paths.get($ref).normalize().toString();
System.out.println("Normalized " + $ref + " to " + normalizedRef);
renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
} else {
$ref = normalizedRef;
}
}

final Schema schema = cache.loadRef($ref, refFormat, Schema.class);

if(schema == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,21 @@ public void testRelativePath2() {
Assert.assertEquals(readResult.getOpenAPI().getPaths().get("/pet/findByTags").getGet().getResponses().get("default").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ErrorModel");
}

@Test
public void testExternalRefsNormalization() throws Exception {
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = new OpenAPIV3Parser()
.readLocation("src/test/resources/oas3.fetched/openapi3.yaml", null, options);

OpenAPI openAPI = result.getOpenAPI();
Schema originalModel = openAPI.getComponents().getSchemas().get("Event");
Schema duplicateModel = openAPI.getComponents().getSchemas().get("Event_1");
System.out.println("component schemas found: " + openAPI.getComponents().getSchemas().keySet());
assertNull(duplicateModel);
assertNotNull(originalModel);
}

private OpenAPI doRelativeFileTest(String location) {
OpenAPIV3Parser parser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
Expand Down Expand Up @@ -3298,4 +3313,4 @@ public void testIssue2081() {
assertEquals(openAPI.getComponents().getSchemas().get("PetCreate").getRequired().size(), 1);
assertEquals(openAPI.getComponents().getSchemas().get("PetCreate").getProperties().size(), 2);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
description: Error responses are included with 4xx and 5xx HTTP responses from the
API service. Either "error" or "errors" will be set.
properties:
error:
description: A description of the error that caused the request to fail.
type: string
errors:
description: A list of errors that contributed to the request failing.
items:
description: An error message that contributed to the request failing.
type: string
type: array
type: object
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
properties:
body:
type: string
created_at:
format: date-time
type: string
href:
type: string
id:
format: uuid
type: string
interpolated:
type: string
relationships:
items:
$ref: './Href.yaml'
type: array
state:
type: string
type:
type: string
modified_by:
type: object
ip:
type: string
type: object
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
properties:
events:
items:
$ref: './Event.yaml'
type: array
meta:
$ref: './Meta.yaml'
type: object
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
properties:
href:
type: string
required:
- href
type: object
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
properties:
first:
$ref: './Href.yaml'
last:
$ref: './Href.yaml'
next:
$ref: './Href.yaml'
previous:
$ref: './Href.yaml'
self:
$ref: './Href.yaml'
total:
type: integer
current_page:
type: integer
last_page:
type: integer
type: object
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
openapi: 3.0.0
info:
version: 1.0.0
title: Example Duplicate Refs API
contact:
email: [email protected]
name: Example API Team
description: |
Sample API spec to validate handling of different file paths that reference the same file
license:
name: Equinix Metal
url: https://metal.equinix.com/legal/
termsOfService: https://metal.equinix.com/legal/
servers:
- url: https://api.example.com/duplicateRefs
components:
schemas:
Error:
$ref: "./components/schemas/Error.yaml"
Event:
$ref: "./components/schemas/Event.yaml"
EventList:
$ref: "./components/schemas/EventList.yaml"
paths:
/connections/{connection_id}/ports/{id}/events:
$ref: ./paths/connections/connection_id/ports/id/events.yaml
/devices/{id}/events:
$ref: ./paths/devices/id/events.yaml
/events:
$ref: ./paths/events.yaml
/events/{id}:
$ref: ./paths/events/id.yaml
/organizations/{id}/events:
$ref: ./paths/organizations/id/events.yaml
/projects/{id}/events:
$ref: ./paths/projects/id/events.yaml
/routes/{id}/events:
$ref: ./paths/routes/id/events.yaml
/virtual-circuits/{id}/events:
$ref: ./paths/virtual-circuits/id/events.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
get:
description: Returns a list of the interconnection events
operationId: findInterconnectionEvents
parameters:
- description: Interconnection UUID
in: path
name: connection_id
required: true
schema:
format: uuid
type: string
responses:
"200":
content:
application/json:
schema:
$ref: "../../../components/schemas/EventList.yaml"
description: ok
"401":
content:
application/json:
schema:
$ref: "../../../components/schemas/Error.yaml"
description: unauthorized
"403":
content:
application/json:
schema:
$ref: "../../../components/schemas/Error.yaml"
description: forbidden
"404":
content:
application/json:
schema:
$ref: "../../../components/schemas/Error.yaml"
description: not found
summary: Retrieve interconnection events
tags:
- Events
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
get:
description: Returns a list of the interconnection port events
operationId: findInterconnectionPortEvents
parameters:
- description: Interconnection UUID
in: path
name: connection_id
required: true
schema:
format: uuid
type: string
- description: Interconnection Port UUID
in: path
name: id
required: true
schema:
format: uuid
type: string
responses:
"200":
content:
application/json:
schema:
$ref: "../../../../../components/schemas/Event.yaml"
description: ok
"401":
content:
application/json:
schema:
$ref: "../../../../../components/schemas/Error.yaml"
description: unauthorized
"403":
content:
application/json:
schema:
$ref: "../../../../../components/schemas/Error.yaml"
description: forbidden
"404":
content:
application/json:
schema:
$ref: "../../../../../components/schemas/Error.yaml"
description: not found
summary: Retrieve interconnection port events
tags:
- Events
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
get:
description: Returns a list of events pertaining to a specific device
operationId: findDeviceEvents
parameters:
- description: Device UUID
in: path
name: id
required: true
schema:
format: uuid
type: string
responses:
"200":
content:
application/json:
schema:
$ref: "../../../components/schemas/EventList.yaml"
description: ok
"401":
content:
application/json:
schema:
$ref: "../../../components/schemas/Error.yaml"
description: unauthorized
"403":
content:
application/json:
schema:
$ref: "../../../components/schemas/Error.yaml"
description: forbidden
"404":
content:
application/json:
schema:
$ref: "../../../components/schemas/Error.yaml"
description: not found
summary: Retrieve device's events
tags:
- Events
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
get:
description: Returns a list of the current user’s events
operationId: findEvents
parameters:
responses:
"200":
content:
application/json:
schema:
$ref: "../components/schemas/EventList.yaml"
description: ok
"401":
content:
application/json:
schema:
$ref: "../components/schemas/Error.yaml"
description: unauthorized
summary: Retrieve current user's events
tags:
- Events
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
get:
description: Returns a single event if the user has access
operationId: findEventById
parameters:
- description: Event UUID
in: path
name: id
required: true
schema:
format: uuid
type: string
responses:
"200":
content:
application/json:
schema:
$ref: "../../components/schemas/Event.yaml"
description: ok
"401":
content:
application/json:
schema:
$ref: "../../components/schemas/Error.yaml"
description: unauthorized
"403":
content:
application/json:
schema:
$ref: "../../components/schemas/Error.yaml"
description: forbidden
"404":
content:
application/json:
schema:
$ref: "../../components/schemas/Error.yaml"
description: not found
summary: Retrieve an event
tags:
- Events
Loading