Skip to content
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
@@ -1,5 +1,14 @@
package io.swagger.v3.parser.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
Expand All @@ -9,24 +18,16 @@
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.media.XML;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.parser.models.RefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("rawtypes")
public class InlineModelResolver {
private OpenAPI openAPI;
static Logger LOGGER = LoggerFactory.getLogger(InlineModelResolver.class);
static final Logger LOGGER = LoggerFactory.getLogger(InlineModelResolver.class);

Map<String, Schema> addedModels = new HashMap<>();
Map<String, String> generatedSignature = new HashMap<>();
Expand Down Expand Up @@ -394,8 +395,8 @@ public String uniqueName(String key) {
if (camelCaseFlattenNaming) {
String uniqueKey;
String concatenated = "";
for (int i = 0; i < key.split("-").length; i++) {
uniqueKey = key.split("-")[i];
for (int i = 0; i < key.split("[-|\\s|_]").length; i++) {
uniqueKey = key.split("[-|\\s|_]")[i];
uniqueKey = uniqueKey.substring(0, 1).toUpperCase() + uniqueKey.substring(1);
concatenated = concatenated.concat(uniqueKey);
}
Expand Down Expand Up @@ -588,13 +589,37 @@ public Schema createModelFromProperty(Schema schema, String path) {


} else {
Schema model = new Schema();//TODO Verify this!
Schema model = new Schema();
model.setAdditionalProperties(schema.getAdditionalProperties());
model.setDescription(description);
model.setDeprecated(schema.getDeprecated());
model.setDiscriminator(schema.getDiscriminator());
model.setEnum(schema.getEnum());
model.setExample(example);
model.setExclusiveMaximum(schema.getExclusiveMaximum());
model.setExclusiveMinimum(schema.getExclusiveMinimum());
model.setFormat(schema.getFormat());
model.setMinLength(schema.getMinLength());
model.setMaximum(schema.getMaximum());
model.setMaxItems(schema.getMaxItems());
model.setMaxProperties(schema.getMaxProperties());
model.setMaxLength(schema.getMaxLength());
model.setMinimum(schema.getMinimum());
model.setMinItems(schema.getMinItems());
model.setMinLength(schema.getMinLength());
model.setMinProperties(schema.getMinProperties());
model.setMultipleOf(schema.getMultipleOf());
model.setName(name);
model.setXml(xml);
model.setType(schema.getType());
model.setNullable(schema.getNullable());
model.setNot(schema.getNot());
model.setPattern(schema.getPattern());
model.setReadOnly(schema.getReadOnly());
model.setRequired(requiredList);
model.setUniqueItems(schema.getUniqueItems());
model.setTitle(schema.getTitle());
model.setType(schema.getType());
model.setXml(xml);
model.setWriteOnly(schema.getWriteOnly());

if (properties != null) {
flattenProperties(properties, path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ public void testIssueFlattenAdditionalPropertiesSchemaInlineModelTrue() {
OpenAPI openAPI = parseResult.getOpenAPI();

//responses
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_map200"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_response_map200")).getOneOf().get(0).get$ref(),"#/components/schemas/Macaw1");
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_map_items404"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_response_map_items404")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw2");
assertNotNull(openAPI.getComponents().getSchemas().get("InlineResponseMap200"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("InlineResponseMap200")).getOneOf().get(0).get$ref(),"#/components/schemas/Macaw1");
assertNotNull(openAPI.getComponents().getSchemas().get("InlineResponseMapItems404"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("InlineResponseMapItems404")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw2");
}


Expand Down Expand Up @@ -259,26 +259,26 @@ public void testIssueFlattenArraySchemaItemsInlineModelTrue() {
OpenAPI openAPI = parseResult.getOpenAPI();

//responses
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_items200"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_response_items200")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw");
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_400"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_response_400")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw3");
assertNotNull(openAPI.getComponents().getSchemas().get("InlineResponseItems200"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("InlineResponseItems200")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw");
assertNotNull(openAPI.getComponents().getSchemas().get("InlineResponse400"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("InlineResponse400")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw3");

//parameters
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_parameter_items_bodylimit"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_parameter_items_bodylimit")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw1");
assertNotNull(openAPI.getComponents().getSchemas().get("InlineParameterItemsBodylimit"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("InlineParameterItemsBodylimit")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw1");
assertNotNull(openAPI.getComponents().getSchemas().get("Pagelimit"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Pagelimit")).getOneOf().get(0).get$ref(),"#/components/schemas/Macaw2");

//requestBodies
assertNotNull(openAPI.getComponents().getSchemas().get("Body"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Body")).getAllOf().get(1).get$ref(),"#/components/schemas/requestBodiesAllOf_2");
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_items200"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_body_items_applicationxml_requestBodies")).getAllOf().get(1).get$ref(),"#/components/schemas/ApplicationxmlAllOf_2");
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Body")).getAllOf().get(1).get$ref(),"#/components/schemas/requestBodiesAllOf2");
assertNotNull(openAPI.getComponents().getSchemas().get("InlineResponseItems200"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("InlineBodyItemsApplicationxmlrequestBodies")).getAllOf().get(1).get$ref(),"#/components/schemas/ApplicationxmlAllOf2");

//components
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_array_items_ArrayTest"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_array_items_ArrayTest")).getOneOf().get(1).get$ref(),"#/components/schemas/ArrayTestOneOf_2");
assertNotNull(openAPI.getComponents().getSchemas().get("InlineArrayItemsArrayTest"));
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("InlineArrayItemsArrayTest")).getOneOf().get(1).get$ref(),"#/components/schemas/ArrayTestOneOf2");
}


Expand Down Expand Up @@ -307,8 +307,8 @@ public void testIssueFlattenComposedSchemaInlineModel() {
SwaggerParseResult parseResult = openApiParser.readLocation("FlattenComposedSchemasAtComponents.yaml", null, options);
OpenAPI openAPI = parseResult.getOpenAPI();
assertNotNull(openAPI);
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("contact-base-model")).getAllOf().get(0).get$ref(),"#/components/schemas/ContactBaseModelAllOf_1");
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Test")).getOneOf().get(1).get$ref(),"#/components/schemas/TestOneOf_2");
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("contact-base-model")).getAllOf().get(0).get$ref(),"#/components/schemas/ContactBaseModelAllOf1");
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Test")).getOneOf().get(1).get$ref(),"#/components/schemas/TestOneOf2");
}

@Test
Expand Down Expand Up @@ -2572,7 +2572,7 @@ public void testParseOptionsSkipMatchesFalse() {
assertNotNull(openAPI);
assertNotNull(openAPI.getComponents());
assertNotNull(openAPI.getComponents().getSchemas());
assertEquals(4, openAPI.getComponents().getSchemas().size());
assertEquals(openAPI.getComponents().getSchemas().size(), 6);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@



import io.swagger.v3.core.util.Yaml;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertNull;
import static org.testng.AssertJUnit.assertTrue;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.testng.annotations.Test;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
Expand All @@ -20,18 +33,8 @@
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.testng.annotations.Test;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static org.testng.AssertJUnit.*;

@SuppressWarnings("static-method")
@SuppressWarnings({"static-method", "rawtypes"})
public class InlineModelResolverTest {

@Test
Expand Down Expand Up @@ -476,7 +479,7 @@ public void testSkipInlineMatchesFalse() {
assertNotNull(openAPI);
assertNotNull(openAPI.getComponents());
assertNotNull(openAPI.getComponents().getSchemas());
assertEquals(4, openAPI.getComponents().getSchemas().size());
assertEquals(openAPI.getComponents().getSchemas().size(), 6);
}

@Test
Expand Down Expand Up @@ -1334,7 +1337,7 @@ public void testArbitraryObjectModelWithArrayInlineWithTitle() {
}


@Test(description = "https://github.com/swagger-api/swagger-parser/issues/1200")
@Test(description = "https://github.com/swagger-api/swagger-parser/issues/1527")
public void testInlineItemsSchema() throws Exception {
ParseOptions options = new ParseOptions();
options.setFlatten(true);
Expand All @@ -1343,4 +1346,76 @@ public void testInlineItemsSchema() throws Exception {
assertNotNull(openAPI);
assertNotNull(openAPI.getComponents().getSchemas().get("inline_response_200"));
}

@Test(description = "https://github.com/swagger-api/swagger-parser/issues/1200")
public void testSchemaPropertiesBeingPassedToFlattenedModel() {
OpenAPI openAPI = new OpenAPI();
openAPI.setComponents(new Components());

Schema address = new ObjectSchema();
address.setDeprecated(false);
address.setDescription("My address");
address.setExclusiveMaximum(true);
address.setExclusiveMinimum(true);
address.setFormat("format");
address.setMinLength(Integer.getInteger("10"));
address.setMaximum(BigDecimal.valueOf(50));
address.setMaxItems(Integer.getInteger("1"));
address.setMaxLength(Integer.getInteger("100"));
address.setMaxProperties(Integer.getInteger("1"));
address.setMinimum(BigDecimal.ZERO);
address.setMinItems(Integer.getInteger("0"));
address.setMinLength(Integer.getInteger("10"));
address.setMinProperties(Integer.getInteger("0"));
address.setMultipleOf(BigDecimal.valueOf(2));
address.setName("Address");
address.setNullable(true);
address.setPattern("%dd");
address.setReadOnly(false);
address.setTitle("my address");
address.setUniqueItems(true);
address.setWriteOnly(false);
address.addProperties("city", new StringSchema());


Schema user = new ObjectSchema();
user.setTitle("InnerUserTitle");
user.setDefault("default");
user.setReadOnly(false);
user.setDescription("user description");
user.setName("user name");
user.addProperties("address", address);

openAPI.getComponents().addSchemas("User", user);

new InlineModelResolver(true, true).flatten(openAPI);

Schema model = openAPI.getComponents().getSchemas().get("User");
assertTrue(model instanceof ObjectSchema);

Schema userAddress = openAPI.getComponents().getSchemas().get("MyAddress");
assertNotNull(userAddress);
assertEquals(userAddress.getDeprecated(), Boolean.FALSE);
assertEquals(userAddress.getDescription(), "My address");
assertEquals(userAddress.getExclusiveMaximum(), Boolean.TRUE);
assertEquals(userAddress.getExclusiveMinimum(), Boolean.TRUE);
assertEquals(userAddress.getFormat(), "format");
assertEquals(userAddress.getMaximum(), BigDecimal.valueOf(50));
assertEquals(userAddress.getMaxItems(), Integer.getInteger("1"));
assertEquals(userAddress.getMaxLength(), Integer.getInteger("100"));
assertEquals(userAddress.getMaxProperties(), Integer.getInteger("1"));
assertEquals(userAddress.getMinimum(), BigDecimal.ZERO);
assertEquals(userAddress.getMinItems(), Integer.getInteger("1"));
assertEquals(userAddress.getMinLength(), Integer.getInteger("100"));
assertEquals(userAddress.getMinProperties(), Integer.getInteger("0"));
assertEquals(userAddress.getMultipleOf(), BigDecimal.valueOf(2));
assertEquals(userAddress.getName(), "Address");
assertEquals(userAddress.getNullable(), Boolean.TRUE);
assertEquals(userAddress.getPattern(), "%dd");
assertEquals(userAddress.getReadOnly(), Boolean.FALSE);
assertEquals(userAddress.getTitle(), "my address");
assertEquals(userAddress.getUniqueItems(), Boolean.TRUE);
assertEquals(userAddress.getWriteOnly(), Boolean.FALSE);

}
}