Repository: swagger-api/swagger-parser
Pull request #1528 (2021-02-17 07:16:16)

fix issue #1527
Signed-off-by: Cristiano V. Gavião <cvgaviao@gmail.com>


Patch:
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/InlineModelResolver.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/InlineModelResolver.java
index a8c704c3..7d02c7c2 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/InlineModelResolver.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/InlineModelResolver.java
@@ -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;
@@ -9,24 +18,16 @@ import io.swagger.v3.oas.models.media.ComposedSchema;
 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<>();
@@ -394,8 +395,8 @@ public class InlineModelResolver {
         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);
             }
@@ -588,13 +589,37 @@ public class InlineModelResolver {
 
 
         } 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);


Test patch:
diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java
index 5bbb5e21..3c6cdafd 100644
--- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java
+++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java
@@ -212,10 +212,10 @@ public class OpenAPIV3ParserTest {
         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");
     }
 
 
@@ -259,26 +259,26 @@ public class OpenAPIV3ParserTest {
         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");
     }
 
 
@@ -307,8 +307,8 @@ public class OpenAPIV3ParserTest {
         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
@@ -2572,7 +2572,7 @@ public class OpenAPIV3ParserTest {
         assertNotNull(openAPI);
         assertNotNull(openAPI.getComponents());
         assertNotNull(openAPI.getComponents().getSchemas());
-        assertEquals(4, openAPI.getComponents().getSchemas().size());
+        assertEquals(openAPI.getComponents().getSchemas().size(), 6);
     }
 
     @Test
diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/InlineModelResolverTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/InlineModelResolverTest.java
index f306114f..161f4430 100644
--- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/InlineModelResolverTest.java
+++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/InlineModelResolverTest.java
@@ -2,7 +2,20 @@ package io.swagger.v3.parser.util;
 
 
 
-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;
@@ -20,18 +33,8 @@ import io.swagger.v3.oas.models.responses.ApiResponse;
 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
@@ -476,7 +479,7 @@ public class InlineModelResolverTest {
         assertNotNull(openAPI);
         assertNotNull(openAPI.getComponents());
         assertNotNull(openAPI.getComponents().getSchemas());
-        assertEquals(4, openAPI.getComponents().getSchemas().size());
+        assertEquals(openAPI.getComponents().getSchemas().size(), 6);
     }
 
     @Test
@@ -1334,7 +1337,7 @@ public class InlineModelResolverTest {
     }
 
 
-    @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);
@@ -1343,4 +1346,76 @@ public class InlineModelResolverTest {
         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);
+
+    }
 }