Skip to content

Commit

Permalink
Merge pull request #1956 from loplex/bugfix-1955
Browse files Browse the repository at this point in the history
Bugfix 1955 Schema refs of array parameters are not resolved for renamed schema components
  • Loading branch information
gracekarina authored Sep 22, 2023
2 parents 03238c7 + c5a9e0a commit cf7eaf9
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ else if(model.getProperties() != null) {
for(String key : properties.keySet()) {
Schema property = properties.get(key);
if (property != null) {
updateRefs(property, pathRef);
updateRefs(property, "");
}
}
}
Expand All @@ -254,22 +254,22 @@ else if(model instanceof ComposedSchema) {
ComposedSchema composedSchema = (ComposedSchema) model;
if (composedSchema.getAllOf() != null) {
for (Schema innerModel : composedSchema.getAllOf()) {
updateRefs(innerModel, pathRef);
updateRefs(innerModel, "");
}
}if (composedSchema.getAnyOf() != null) {
for(Schema innerModel : composedSchema.getAnyOf()) {
updateRefs(innerModel, pathRef);
updateRefs(innerModel, "");
}
}if (composedSchema.getOneOf() != null) {
for (Schema innerModel : composedSchema.getOneOf()) {
updateRefs(innerModel, pathRef);
updateRefs(innerModel, "");
}
}
}
else if(model instanceof ArraySchema) {
ArraySchema arraySchema = (ArraySchema) model;
if(arraySchema.getItems() != null) {
updateRefs(arraySchema.getItems(), pathRef);
updateRefs(arraySchema.getItems(), "");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.PathItem.HttpMethod;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
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 java.net.URL;
import java.util.Map.Entry;
import java.util.Objects;

import static java.lang.String.format;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public class PathsProcessorTest {

Expand Down Expand Up @@ -42,6 +49,36 @@ public void testProcessPaths_parameters_refOperationLevelDefinition() {
assertOperationsHasParameters(openAPI, "/ref/test/{id}/operationlevelparam");
}

@Test
public void testProcessPaths_refsToRenamedSchemasResolved() {
URL mergeSpecLocation = getClass().getClassLoader().getResource("issue-1955/merged_spec12.yaml");
Objects.requireNonNull(mergeSpecLocation);
ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolve(true);

SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(mergeSpecLocation.toString(), null, parseOptions);

swaggerParseResult.getOpenAPI()
.getPaths().values().stream()
.flatMap(path -> path.readOperationsMap().values().stream())
.flatMap(operation -> operation.getParameters().stream())
.map(Parameter::getSchema)
.forEach(this::assertSchemaNoExternalRefs);
}

private void assertSchemaNoExternalRefs(Schema<?> schema) {
if (schema.get$ref() != null) {
assertSchemaRefInternal(schema.get$ref());
}
if (schema.getItems() != null && schema.getItems().get$ref() != null) {
assertSchemaRefInternal(schema.getItems().get$ref());
}
}

private void assertSchemaRefInternal(String ref) {
assertTrue(ref.startsWith("#"));
}

private void assertOperationsHasParameters(OpenAPI openAPI, String path) {
PathItem pathItem = openAPI.getPaths().get(path);

Expand All @@ -54,4 +91,4 @@ private void assertOperationsHasParameters(OpenAPI openAPI, String path) {
assertFalse(operation.getParameters() == null || operation.getParameters().isEmpty(), format("Expected parameters on %s operation for %s but found none", httpMethod, path));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
openapi: 3.0.1
info:
title: merged spec
version: 1.0.0
servers:
- url: http://localhost:8080

paths:

/getEmptyOne:
$ref: "./spec1.yaml#/paths/~1getEmptyOne"

/getEmptyTwo:
$ref: "./spec2.yaml#/paths/~1getEmptyTwo"
32 changes: 32 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/issue-1955/spec1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
openapi: 3.0.1
info:
title: spec 1
version: 1.0.0
servers:
- url: http://localhost:8080

paths:

/getEmptyOne:
get:
operationId: getEmptyOne
parameters:
- name: element
in: query
schema:
$ref: '#/components/schemas/myType'
- name: elements
in: query
schema:
type: array
items:
$ref: '#/components/schemas/myType'
responses:
"204":
description: empty

components:
schemas:

myType:
type: string
32 changes: 32 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/issue-1955/spec2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
openapi: 3.0.1
info:
title: spec 2
version: 1.0.0
servers:
- url: http://localhost:8080

paths:

/getEmptyTwo:
get:
operationId: getEmptyTwo
parameters:
- name: element
in: query
schema:
$ref: '#/components/schemas/myType'
- name: elements
in: query
schema:
type: array
items:
$ref: '#/components/schemas/myType'
responses:
"204":
description: empty

components:
schemas:

myType:
type: integer
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
<artifactId>surefire-junit4</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng-version}</version>
</dependency>
</dependencies>
<configuration>
<testNGArtifactName>none:none</testNGArtifactName>
Expand Down

0 comments on commit cf7eaf9

Please sign in to comment.