Skip to content

Commit

Permalink
fix issue swagger-api#1527
Browse files Browse the repository at this point in the history
Signed-off-by: Cristiano V. Gavião <cvgaviao@gmail.com>
  • Loading branch information
cvgaviao committed Feb 17, 2021
1 parent 92a516d commit 197a13f
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 28 deletions.
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 @@ -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 @@ -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);

}
}

0 comments on commit 197a13f

Please sign in to comment.