Skip to content

Commit 6aed37b

Browse files
authored
Merge branch 'master' into patch-1
2 parents c1d7418 + a5234f5 commit 6aed37b

File tree

4 files changed

+148
-48
lines changed

4 files changed

+148
-48
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/InlineModelResolver.java

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package io.swagger.v3.parser.util;
22

3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import io.swagger.v3.core.util.Json;
312
import io.swagger.v3.oas.models.Components;
413
import io.swagger.v3.oas.models.OpenAPI;
514
import io.swagger.v3.oas.models.Operation;
@@ -9,24 +18,16 @@
918
import io.swagger.v3.oas.models.media.MediaType;
1019
import io.swagger.v3.oas.models.media.ObjectSchema;
1120
import io.swagger.v3.oas.models.media.Schema;
12-
import io.swagger.v3.oas.models.media.StringSchema;
1321
import io.swagger.v3.oas.models.media.XML;
1422
import io.swagger.v3.oas.models.parameters.Parameter;
1523
import io.swagger.v3.oas.models.parameters.RequestBody;
1624
import io.swagger.v3.oas.models.responses.ApiResponse;
17-
import io.swagger.v3.core.util.Json;
1825
import io.swagger.v3.parser.models.RefType;
19-
import org.slf4j.Logger;
20-
import org.slf4j.LoggerFactory;
21-
22-
import java.util.ArrayList;
23-
import java.util.HashMap;
24-
import java.util.List;
25-
import java.util.Map;
2626

27+
@SuppressWarnings("rawtypes")
2728
public class InlineModelResolver {
2829
private OpenAPI openAPI;
29-
static Logger LOGGER = LoggerFactory.getLogger(InlineModelResolver.class);
30+
static final Logger LOGGER = LoggerFactory.getLogger(InlineModelResolver.class);
3031

3132
Map<String, Schema> addedModels = new HashMap<>();
3233
Map<String, String> generatedSignature = new HashMap<>();
@@ -394,8 +395,8 @@ public String uniqueName(String key) {
394395
if (camelCaseFlattenNaming) {
395396
String uniqueKey;
396397
String concatenated = "";
397-
for (int i = 0; i < key.split("-").length; i++) {
398-
uniqueKey = key.split("-")[i];
398+
for (int i = 0; i < key.split("[-|\\s|_]").length; i++) {
399+
uniqueKey = key.split("[-|\\s|_]")[i];
399400
uniqueKey = uniqueKey.substring(0, 1).toUpperCase() + uniqueKey.substring(1);
400401
concatenated = concatenated.concat(uniqueKey);
401402
}
@@ -588,13 +589,37 @@ public Schema createModelFromProperty(Schema schema, String path) {
588589

589590

590591
} else {
591-
Schema model = new Schema();//TODO Verify this!
592+
Schema model = new Schema();
593+
model.setAdditionalProperties(schema.getAdditionalProperties());
592594
model.setDescription(description);
595+
model.setDeprecated(schema.getDeprecated());
596+
model.setDiscriminator(schema.getDiscriminator());
597+
model.setEnum(schema.getEnum());
593598
model.setExample(example);
599+
model.setExclusiveMaximum(schema.getExclusiveMaximum());
600+
model.setExclusiveMinimum(schema.getExclusiveMinimum());
601+
model.setFormat(schema.getFormat());
602+
model.setMinLength(schema.getMinLength());
603+
model.setMaximum(schema.getMaximum());
604+
model.setMaxItems(schema.getMaxItems());
605+
model.setMaxProperties(schema.getMaxProperties());
606+
model.setMaxLength(schema.getMaxLength());
607+
model.setMinimum(schema.getMinimum());
608+
model.setMinItems(schema.getMinItems());
609+
model.setMinLength(schema.getMinLength());
610+
model.setMinProperties(schema.getMinProperties());
611+
model.setMultipleOf(schema.getMultipleOf());
594612
model.setName(name);
595-
model.setXml(xml);
596-
model.setType(schema.getType());
613+
model.setNullable(schema.getNullable());
614+
model.setNot(schema.getNot());
615+
model.setPattern(schema.getPattern());
616+
model.setReadOnly(schema.getReadOnly());
597617
model.setRequired(requiredList);
618+
model.setUniqueItems(schema.getUniqueItems());
619+
model.setTitle(schema.getTitle());
620+
model.setType(schema.getType());
621+
model.setXml(xml);
622+
model.setWriteOnly(schema.getWriteOnly());
598623

599624
if (properties != null) {
600625
flattenProperties(properties, path);

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/RemoteUrl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
5454
}};
5555

5656
// Install the all-trusting trust manager
57-
final SSLContext sc = SSLContext.getInstance("SSL");
57+
final SSLContext sc = SSLContext.getInstance("TLS");
5858
sc.init(null, trustAllCerts, new java.security.SecureRandom());
5959
final SSLSocketFactory sf = sc.getSocketFactory();
6060

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,10 @@ public void testIssueFlattenAdditionalPropertiesSchemaInlineModelTrue() {
212212
OpenAPI openAPI = parseResult.getOpenAPI();
213213

214214
//responses
215-
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_map200"));
216-
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_response_map200")).getOneOf().get(0).get$ref(),"#/components/schemas/Macaw1");
217-
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_map_items404"));
218-
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_response_map_items404")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw2");
215+
assertNotNull(openAPI.getComponents().getSchemas().get("InlineResponseMap200"));
216+
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("InlineResponseMap200")).getOneOf().get(0).get$ref(),"#/components/schemas/Macaw1");
217+
assertNotNull(openAPI.getComponents().getSchemas().get("InlineResponseMapItems404"));
218+
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("InlineResponseMapItems404")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw2");
219219
}
220220

221221

@@ -259,26 +259,26 @@ public void testIssueFlattenArraySchemaItemsInlineModelTrue() {
259259
OpenAPI openAPI = parseResult.getOpenAPI();
260260

261261
//responses
262-
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_items200"));
263-
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_response_items200")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw");
264-
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_400"));
265-
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_response_400")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw3");
262+
assertNotNull(openAPI.getComponents().getSchemas().get("InlineResponseItems200"));
263+
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("InlineResponseItems200")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw");
264+
assertNotNull(openAPI.getComponents().getSchemas().get("InlineResponse400"));
265+
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("InlineResponse400")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw3");
266266

267267
//parameters
268-
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_parameter_items_bodylimit"));
269-
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_parameter_items_bodylimit")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw1");
268+
assertNotNull(openAPI.getComponents().getSchemas().get("InlineParameterItemsBodylimit"));
269+
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("InlineParameterItemsBodylimit")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw1");
270270
assertNotNull(openAPI.getComponents().getSchemas().get("Pagelimit"));
271271
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Pagelimit")).getOneOf().get(0).get$ref(),"#/components/schemas/Macaw2");
272272

273273
//requestBodies
274274
assertNotNull(openAPI.getComponents().getSchemas().get("Body"));
275-
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Body")).getAllOf().get(1).get$ref(),"#/components/schemas/requestBodiesAllOf_2");
276-
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_items200"));
277-
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_body_items_applicationxml_requestBodies")).getAllOf().get(1).get$ref(),"#/components/schemas/ApplicationxmlAllOf_2");
275+
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Body")).getAllOf().get(1).get$ref(),"#/components/schemas/requestBodiesAllOf2");
276+
assertNotNull(openAPI.getComponents().getSchemas().get("InlineResponseItems200"));
277+
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("InlineBodyItemsApplicationxmlrequestBodies")).getAllOf().get(1).get$ref(),"#/components/schemas/ApplicationxmlAllOf2");
278278

279279
//components
280-
assertNotNull(openAPI.getComponents().getSchemas().get("Inline_array_items_ArrayTest"));
281-
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_array_items_ArrayTest")).getOneOf().get(1).get$ref(),"#/components/schemas/ArrayTestOneOf_2");
280+
assertNotNull(openAPI.getComponents().getSchemas().get("InlineArrayItemsArrayTest"));
281+
assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("InlineArrayItemsArrayTest")).getOneOf().get(1).get$ref(),"#/components/schemas/ArrayTestOneOf2");
282282
}
283283

284284

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

314314
@Test
@@ -2572,7 +2572,7 @@ public void testParseOptionsSkipMatchesFalse() {
25722572
assertNotNull(openAPI);
25732573
assertNotNull(openAPI.getComponents());
25742574
assertNotNull(openAPI.getComponents().getSchemas());
2575-
assertEquals(4, openAPI.getComponents().getSchemas().size());
2575+
assertEquals(openAPI.getComponents().getSchemas().size(), 6);
25762576
}
25772577

25782578
@Test

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/InlineModelResolverTest.java

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@
22

33

44

5-
import io.swagger.v3.core.util.Yaml;
5+
import static org.testng.AssertJUnit.assertEquals;
6+
import static org.testng.AssertJUnit.assertNotNull;
7+
import static org.testng.AssertJUnit.assertNull;
8+
import static org.testng.AssertJUnit.assertTrue;
9+
10+
import java.math.BigDecimal;
11+
import java.util.ArrayList;
12+
import java.util.HashMap;
13+
import java.util.LinkedList;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
import org.testng.annotations.Test;
18+
619
import io.swagger.v3.oas.models.Components;
720
import io.swagger.v3.oas.models.OpenAPI;
821
import io.swagger.v3.oas.models.Operation;
@@ -20,18 +33,8 @@
2033
import io.swagger.v3.oas.models.responses.ApiResponses;
2134
import io.swagger.v3.parser.OpenAPIV3Parser;
2235
import io.swagger.v3.parser.core.models.ParseOptions;
23-
import org.testng.annotations.Test;
24-
25-
26-
import java.util.ArrayList;
27-
import java.util.HashMap;
28-
import java.util.LinkedList;
29-
import java.util.List;
30-
import java.util.Map;
3136

32-
import static org.testng.AssertJUnit.*;
33-
34-
@SuppressWarnings("static-method")
37+
@SuppressWarnings({"static-method", "rawtypes"})
3538
public class InlineModelResolverTest {
3639

3740
@Test
@@ -476,7 +479,7 @@ public void testSkipInlineMatchesFalse() {
476479
assertNotNull(openAPI);
477480
assertNotNull(openAPI.getComponents());
478481
assertNotNull(openAPI.getComponents().getSchemas());
479-
assertEquals(4, openAPI.getComponents().getSchemas().size());
482+
assertEquals(openAPI.getComponents().getSchemas().size(), 6);
480483
}
481484

482485
@Test
@@ -1334,7 +1337,7 @@ public void testArbitraryObjectModelWithArrayInlineWithTitle() {
13341337
}
13351338

13361339

1337-
@Test(description = "https://github.com/swagger-api/swagger-parser/issues/1200")
1340+
@Test(description = "https://github.com/swagger-api/swagger-parser/issues/1527")
13381341
public void testInlineItemsSchema() throws Exception {
13391342
ParseOptions options = new ParseOptions();
13401343
options.setFlatten(true);
@@ -1343,4 +1346,76 @@ public void testInlineItemsSchema() throws Exception {
13431346
assertNotNull(openAPI);
13441347
assertNotNull(openAPI.getComponents().getSchemas().get("inline_response_200"));
13451348
}
1349+
1350+
@Test(description = "https://github.com/swagger-api/swagger-parser/issues/1200")
1351+
public void testSchemaPropertiesBeingPassedToFlattenedModel() {
1352+
OpenAPI openAPI = new OpenAPI();
1353+
openAPI.setComponents(new Components());
1354+
1355+
Schema address = new ObjectSchema();
1356+
address.setDeprecated(false);
1357+
address.setDescription("My address");
1358+
address.setExclusiveMaximum(true);
1359+
address.setExclusiveMinimum(true);
1360+
address.setFormat("format");
1361+
address.setMinLength(Integer.getInteger("10"));
1362+
address.setMaximum(BigDecimal.valueOf(50));
1363+
address.setMaxItems(Integer.getInteger("1"));
1364+
address.setMaxLength(Integer.getInteger("100"));
1365+
address.setMaxProperties(Integer.getInteger("1"));
1366+
address.setMinimum(BigDecimal.ZERO);
1367+
address.setMinItems(Integer.getInteger("0"));
1368+
address.setMinLength(Integer.getInteger("10"));
1369+
address.setMinProperties(Integer.getInteger("0"));
1370+
address.setMultipleOf(BigDecimal.valueOf(2));
1371+
address.setName("Address");
1372+
address.setNullable(true);
1373+
address.setPattern("%dd");
1374+
address.setReadOnly(false);
1375+
address.setTitle("my address");
1376+
address.setUniqueItems(true);
1377+
address.setWriteOnly(false);
1378+
address.addProperties("city", new StringSchema());
1379+
1380+
1381+
Schema user = new ObjectSchema();
1382+
user.setTitle("InnerUserTitle");
1383+
user.setDefault("default");
1384+
user.setReadOnly(false);
1385+
user.setDescription("user description");
1386+
user.setName("user name");
1387+
user.addProperties("address", address);
1388+
1389+
openAPI.getComponents().addSchemas("User", user);
1390+
1391+
new InlineModelResolver(true, true).flatten(openAPI);
1392+
1393+
Schema model = openAPI.getComponents().getSchemas().get("User");
1394+
assertTrue(model instanceof ObjectSchema);
1395+
1396+
Schema userAddress = openAPI.getComponents().getSchemas().get("MyAddress");
1397+
assertNotNull(userAddress);
1398+
assertEquals(userAddress.getDeprecated(), Boolean.FALSE);
1399+
assertEquals(userAddress.getDescription(), "My address");
1400+
assertEquals(userAddress.getExclusiveMaximum(), Boolean.TRUE);
1401+
assertEquals(userAddress.getExclusiveMinimum(), Boolean.TRUE);
1402+
assertEquals(userAddress.getFormat(), "format");
1403+
assertEquals(userAddress.getMaximum(), BigDecimal.valueOf(50));
1404+
assertEquals(userAddress.getMaxItems(), Integer.getInteger("1"));
1405+
assertEquals(userAddress.getMaxLength(), Integer.getInteger("100"));
1406+
assertEquals(userAddress.getMaxProperties(), Integer.getInteger("1"));
1407+
assertEquals(userAddress.getMinimum(), BigDecimal.ZERO);
1408+
assertEquals(userAddress.getMinItems(), Integer.getInteger("1"));
1409+
assertEquals(userAddress.getMinLength(), Integer.getInteger("100"));
1410+
assertEquals(userAddress.getMinProperties(), Integer.getInteger("0"));
1411+
assertEquals(userAddress.getMultipleOf(), BigDecimal.valueOf(2));
1412+
assertEquals(userAddress.getName(), "Address");
1413+
assertEquals(userAddress.getNullable(), Boolean.TRUE);
1414+
assertEquals(userAddress.getPattern(), "%dd");
1415+
assertEquals(userAddress.getReadOnly(), Boolean.FALSE);
1416+
assertEquals(userAddress.getTitle(), "my address");
1417+
assertEquals(userAddress.getUniqueItems(), Boolean.TRUE);
1418+
assertEquals(userAddress.getWriteOnly(), Boolean.FALSE);
1419+
1420+
}
13461421
}

0 commit comments

Comments
 (0)