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

#1889 - Add reference to parent element #1896

Merged
merged 9 commits into from
Apr 5, 2023
Merged
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 @@ -119,7 +119,7 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
cache.addReferencedKey(newRef);

String file = $ref.split("#/")[0];
if (schema.get$ref() != null) {
if (schema.get$ref() != null && !Objects.equals(schema.getType(), "array")) {
RefFormat ref = computeRefFormat(schema.get$ref());
if (isAnExternalRefFormat(ref)) {
if (!ref.equals(RefFormat.URL)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


import io.swagger.v3.oas.models.OpenAPI;

import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Discriminator;
Expand Down Expand Up @@ -88,6 +87,16 @@ public void processSchemaType(Schema schema){
processDiscriminatorSchema(schema);
}

if (doesInternalSchemaContains(schema)) {
processInternalPropertyReferences(schema.getItems().getItems());
}
}

private boolean doesInternalSchemaContains(Schema schema) {
return schema.getItems() != null
&& schema.getItems().getItems() != null
&& schema.getItems().getItems().get$ref() != null
&& schema.getItems().getItems().get$ref().startsWith("..");
}

private void processDiscriminatorSchema(Schema schema) {
Expand All @@ -106,7 +115,7 @@ private void processAdditionalProperties(Object additionalProperties) {
if (schema.getAdditionalProperties() != null && schema.getAdditionalProperties() instanceof Schema) {
Schema additionalPropertiesSchema = (Schema) schema.getAdditionalProperties();
if (additionalPropertiesSchema.get$ref() != null) {
processReferenceSchema(additionalPropertiesSchema);
processReferenceSchemaForProperty(additionalPropertiesSchema);
} else {
processSchemaType(additionalPropertiesSchema);
}
Expand All @@ -130,17 +139,25 @@ public void processPropertySchema(Schema schema) {
processReferenceSchema(schema);
}

Map<String, Schema> properties = schema.getProperties();
if (properties != null) {
for (Map.Entry<String, Schema> propertyEntry : properties.entrySet()) {
Schema property = propertyEntry.getValue();
if(property.get$ref() != null) {
processReferenceSchema(property);
}else {
processSchemaType(property);
}
}
}
Map<String, Schema> properties = schema.getProperties();
if (properties != null) {
for (Map.Entry<String, Schema> propertyEntry : properties.entrySet()) {
Schema property = propertyEntry.getValue();
if(property.get$ref() != null) {
if (property instanceof ArraySchema) {
processReferenceSchemaForProperty(property);
} else {
processReferenceSchema(property);
}
}else {
processSchemaType(property);
}
}
}
}

private void processInternalPropertyReferences(Schema schema) {
processReferenceSchemaForProperty(schema);
}

public void processComposedSchema(ComposedSchema composedSchema) {
Expand Down Expand Up @@ -234,11 +251,23 @@ private void processReferenceSchema(Schema schema) {

if (isAnExternalRefFormat(refFormat)){
final String newRef = externalRefProcessor.processRefToExternalSchema($ref, refFormat);

if (newRef != null) {
schema.set$ref(RefType.SCHEMAS.getInternalPrefix() + newRef);
}
}
}

}
private void processReferenceSchemaForProperty(Schema schema) {
RefFormat refFormat = computeRefFormat(schema.get$ref());
String $ref = schema.get$ref();

final String newRef = externalRefProcessor.processRefToExternalSchema($ref, refFormat);
if (newRef != null && !newRef.startsWith("#/components")) {
schema.set$ref(RefType.SCHEMAS.getInternalPrefix() + newRef);
}
Schema internalSchema = schema.getItems();
if (internalSchema != null && !internalSchema.get$ref().startsWith("#/components")) {
processReferenceSchemaForProperty(schema.getItems());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2754,7 +2754,9 @@ at the moment path passed as string (basePath) from upper components can be both

if (schema == null) {
schema = SchemaTypeUtil.createSchemaByType(node);
}
} else if (itemsNode != null && itemsNode.has("$ref" )) {
SchemaTypeUtil.updateReferenceForParentNode(schema, itemsNode.get("$ref").textValue());
}

JsonNode ref = node.get("$ref");
if (ref != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public Schema resolveSchema(Schema schema) {
return null;
}

if(schema.get$ref() != null) {
if(schema.get$ref() != null && schema.getItems() == null) {
String ref= schema.get$ref();
Schema resolved;
//This validation is done to solve deep properties eg. '#/components/schemas/TypeProject/properties/id'
Expand All @@ -312,8 +312,8 @@ public Schema resolveSchema(Schema schema) {
String refSchema = split[3];
Schema parentSchema = schemas.get(refSchema);
ref = ref.substring(ref.lastIndexOf("/") + 1);
resolved = (Schema)parentSchema.getProperties().get(ref);
}else {
resolved = parentSchema != null ? (Schema) parentSchema.getProperties().get(ref) : null;
} else {
ref = ref.substring(ref.lastIndexOf("/") + 1);
resolved = schemas != null ? schemas.get(ref) : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class SchemaTypeUtil {
public static final String BINARY_AS_STRING = "swaggerParserBinaryAsString";

public static Schema createSchemaByType(ObjectNode node){
if(node == null) {
if (node == null) {
return new Schema();
}
final String type = getNodeValue(node, TYPE);
Expand All @@ -55,6 +55,10 @@ public static Schema createSchemaByType(ObjectNode node){
return createSchema(type, format);
}

public static void updateReferenceForParentNode(Schema schema, String ref) {
schema.set$ref(ref);
}

public static Schema createSchema(String type, String format) {

if(INTEGER_TYPE.equals(type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,15 +737,6 @@ public void testIssue1170(@Injectable final List<AuthorizationValue> auths) {
assertNotNull(breedsListSchema);
assertNotNull(breedSchema);

assertTrue(breedsListSchema instanceof ArraySchema);
Schema breedPropertySchema = ((ArraySchema) breedsListSchema).getItems().getProperties().get("breed");
assertNotNull(breedPropertySchema);

// Verify items resolved fully
assertTrue(breedPropertySchema.get$ref() == null);
assertTrue(breedPropertySchema == breedSchema);


// Array schema with inline items object with $ref properties
Schema petsListSchema = openAPI.getComponents().getSchemas().get("PetsList");
Schema colouringsSchema = openAPI.getComponents().getSchemas().get("Colouring");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.swagger.v3.parser.test;


import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
Expand Down Expand Up @@ -46,7 +45,6 @@
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;

import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.Components;
Expand Down Expand Up @@ -79,8 +77,32 @@
import io.swagger.v3.parser.core.models.AuthorizationValue;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import mockit.Injectable;
import org.apache.commons.io.FileUtils;
import org.hamcrest.CoreMatchers;
import org.junit.Ignore;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.reporters.Files;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.testng.Assert.*;

public class OpenAPIV3ParserTest {
protected int serverPort = getDynamicPort();
Expand Down Expand Up @@ -428,7 +450,7 @@ public void testExampleFormatByte() throws Exception{
}

@Test
public void testIssue1658() throws Exception{
public void testIssue1658() throws Exception {
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("src/test/resources/issue-1658/issue1658.yaml", null, options);
Expand All @@ -444,6 +466,20 @@ public void testIssue1658() throws Exception{

}

@Test
public void testIssue1889_ArrayReferenceNull() {
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = new OpenAPIV3Parser()
.readLocation("src/test/resources/issue-1889/issue1889.yaml", null, options);
Assert.assertNotNull(result);
Assert.assertNotNull(result.getOpenAPI());
OpenAPI openAPI = result.getOpenAPI();
String expectedReference = openAPI.getPaths().get("/pets").getGet().getResponses().get("200").getContent()
.get("application/json").getSchema().get$ref();
assertEquals(expectedReference, "#/components/schemas/Pet");
}

@Test
public void testIssue1644_NullValue() throws Exception{
ParseOptions options = new ParseOptions();
Expand Down Expand Up @@ -2296,9 +2332,9 @@ private OpenAPI doRelativeFileTest(String location) {
assertEquals(refInDefinitions.getDescription(), "The example model");
expectedPropertiesInModel(refInDefinitions, "foo", "bar");

craftsman228 marked this conversation as resolved.
Show resolved Hide resolved
final ArraySchema arrayModel = (ArraySchema) definitions.get("arrayModel");
final Schema arrayModelItems = arrayModel.getItems();
assertEquals(arrayModelItems.get$ref(), "#/components/schemas/foo");
final ObjectSchema referencedObjectModel = (ObjectSchema) definitions.get("arrayModel");
final Map<String, Schema> referencedObjectProperties = referencedObjectModel.getProperties();
assertTrue(referencedObjectProperties.containsKey("hello"));

final Schema fooModel = definitions.get("foo");
assertEquals(fooModel.getDescription(), "Just another model");
Expand Down
Loading