Skip to content
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 @@ -10,6 +10,8 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -870,7 +872,7 @@ private void processRefExamples(Map<String, Example> examples, String $ref) {
if (example.get$ref() != null) {
RefFormat ref = computeRefFormat(example.get$ref());
if (isAnExternalRefFormat(ref)) {
processRefExample(example, $ref);
processRefExample(example, file);
} else {
processRefToExternalExample(file + example.get$ref(), RefFormat.RELATIVE);
}
Expand All @@ -879,22 +881,8 @@ private void processRefExamples(Map<String, Example> examples, String $ref) {
}

private void processRefExample(Example example, String externalFile) {
RefFormat format = computeRefFormat(example.get$ref());

if (!isAnExternalRefFormat(format)) {
example.set$ref(RefType.SCHEMAS.getInternalPrefix()+ processRefToExternalSchema(externalFile + example.get$ref(), RefFormat.RELATIVE));
return;
}
String $ref = example.get$ref();
String subRefExternalPath = getExternalPath(example.get$ref())
.orElse(null);

if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
$ref = join(externalFile, example.get$ref());
example.set$ref($ref);
}else {
processRefToExternalExample($ref, format);
}
processRef(example.get$ref(), externalFile, example::set$ref, this::processRefToExternalExample,
ref -> join(externalFile, ref));
}

private void processRefSchemaObject(Schema schema, String $ref) {
Expand All @@ -917,7 +905,7 @@ private void processRefHeaders(Map<String, Header> headers, String $ref) {
if (header.get$ref() != null) {
RefFormat ref = computeRefFormat(header.get$ref());
if (isAnExternalRefFormat(ref)) {
processRefHeader(header, $ref);
processRefHeader(header, file);
} else {
processRefToExternalHeader(file + header.get$ref(), RefFormat.RELATIVE);
}
Expand All @@ -931,7 +919,7 @@ private void processRefLinks(Map<String, Link> links, String $ref) {
if (link.get$ref() != null) {
RefFormat ref = computeRefFormat(link.get$ref());
if (isAnExternalRefFormat(ref)) {
processRefLink(link, $ref);
processRefLink(link, file);
} else {
processRefToExternalLink(file + link.get$ref(), RefFormat.RELATIVE);
}
Expand All @@ -942,65 +930,41 @@ private void processRefLinks(Map<String, Link> links, String $ref) {


private void processRefSchema(Schema subRef, String externalFile) {
RefFormat format = computeRefFormat(subRef.get$ref());

if (!isAnExternalRefFormat(format)) {
subRef.set$ref(RefType.SCHEMAS.getInternalPrefix()+ processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE));
return;
}
String $ref = subRef.get$ref();
String subRefExternalPath = getExternalPath(subRef.get$ref())
.orElse(null);

if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
$ref = constructRef(subRef, externalFile);
subRef.set$ref($ref);
}else {
processRefToExternalSchema($ref, format);
}
processRef(subRef.get$ref(), externalFile, subRef::set$ref, this::processRefToExternalSchema,
ref -> constructRef(subRef, externalFile));
}


protected String constructRef(Schema refProperty, String rootLocation) {
String ref = refProperty.get$ref();
return join(rootLocation, ref);
}

private void processRefHeader(Header subRef, String externalFile) {
RefFormat format = computeRefFormat(subRef.get$ref());

if (!isAnExternalRefFormat(format)) {
subRef.set$ref(RefType.SCHEMAS.getInternalPrefix()+ processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE));
return;
}
String $ref = subRef.get$ref();
String subRefExternalPath = getExternalPath(subRef.get$ref())
.orElse(null);

if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
$ref = join(externalFile, subRef.get$ref());
subRef.set$ref($ref);
}else {
processRefToExternalHeader($ref, format);
}
processRef(subRef.get$ref(), externalFile, subRef::set$ref, this::processRefToExternalHeader,
ref -> join(externalFile, ref));
}

private void processRefLink(Link subRef, String externalFile) {
RefFormat format = computeRefFormat(subRef.get$ref());
processRef(subRef.get$ref(), externalFile, subRef::set$ref, this::processRefToExternalLink,
ref -> join(externalFile, ref));
}

private void processRef(String ref, String externalFile, Consumer<String> refSetter,
BiConsumer<String, RefFormat> externalRefProcessor,
Function<String, String> relativeRefResolver) {
RefFormat format = computeRefFormat(ref);

if (!isAnExternalRefFormat(format)) {
subRef.set$ref(RefType.SCHEMAS.getInternalPrefix()+ processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE));
refSetter.accept(RefType.SCHEMAS.getInternalPrefix()
+ processRefToExternalSchema(externalFile + ref, RefFormat.RELATIVE));
return;
}
String $ref = subRef.get$ref();
String subRefExternalPath = getExternalPath(subRef.get$ref())
.orElse(null);
String subRefExternalPath = getExternalPath(ref).orElse(null);

if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
$ref = join(externalFile, subRef.get$ref());
subRef.set$ref($ref);
}else {
processRefToExternalLink($ref, format);
refSetter.accept(relativeRefResolver.apply(ref));
} else {
externalRefProcessor.accept(ref, format);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.swagger.v3.parser.test;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.examples.Example;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.links.Link;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

public class Issue2033Test {

@Test
public void resolvesRelativeComponentRefsFromExternalTemplatedPathItem() {
ParseOptions options = new ParseOptions();
options.setResolve(true);

SwaggerParseResult result = new OpenAPIV3Parser().readLocation(
"src/test/resources/issue-2033/main.yaml", null, options);

assertNotNull(result.getOpenAPI());
assertTrue(result.getMessages().isEmpty(), "Unexpected parser messages: " + result.getMessages());

OpenAPI openAPI = result.getOpenAPI();
ApiResponse partialResponse = openAPI.getPaths()
.get("/nodes/{uuid}/rights")
.getGet()
.getResponses()
.get("206");

Header contentRange = partialResponse.getHeaders().get("Content-Range");

assertNotNull(contentRange);
assertEquals(contentRange.get$ref(), "#/components/headers/Content-Range");

Header resolvedContentRange = openAPI.getComponents().getHeaders().get("Content-Range");
assertNotNull(resolvedContentRange);
assertNotNull(resolvedContentRange.getSchema());
assertEquals(resolvedContentRange.getSchema().getType(), "string");
assertEquals(resolvedContentRange.getSchema().getPattern(), "\\d+-\\d+/\\d+");

Link nextPage = partialResponse.getLinks().get("nextPage");
assertNotNull(nextPage);
assertEquals(nextPage.get$ref(), "#/components/links/NextPage");

Link resolvedNextPage = openAPI.getComponents().getLinks().get("NextPage");
assertNotNull(resolvedNextPage);
assertEquals(resolvedNextPage.getOperationId(), "listNodeRights");

Example contentRangeExample = partialResponse.getContent()
.get("application/json")
.getExamples()
.get("contentRange");
assertNotNull(contentRangeExample);
assertEquals(contentRangeExample.get$ref(), "#/components/examples/ContentRangeExample");

Example resolvedExample = openAPI.getComponents().getExamples().get("ContentRangeExample");
assertNotNull(resolvedExample);
assertEquals(resolvedExample.getValue(), "items 0-9/42");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
openapi: 3.0.3
info:
title: Common components
version: 1.0.0
paths: {}
components:
examples:
ContentRangeExample:
value: items 0-9/42
headers:
Content-Range:
schema:
type: string
pattern: '\d+-\d+/\d+'
links:
NextPage:
operationId: listNodeRights
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
openapi: 3.0.3
info:
title: Issue 2033
version: 1.0.0
paths:
/nodes/{uuid}/rights:
$ref: "traceability/nodes.yaml#/paths/~1nodes~1{uuid}~1rights"
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.0.3
info:
title: Nodes
version: 1.0.0
paths:
/nodes/{uuid}/rights:
get:
operationId: listNodeRights
responses:
"206":
description: Partial
content:
application/json:
schema:
type: string
examples:
contentRange:
$ref: "../common-spec-openapi.yaml#/components/examples/ContentRangeExample"
headers:
Content-Range:
$ref: "../common-spec-openapi.yaml#/components/headers/Content-Range"
links:
nextPage:
$ref: "../common-spec-openapi.yaml#/components/links/NextPage"
Loading