From 005deb74f0204581577e68b5b19943acafd6dee5 Mon Sep 17 00:00:00 2001 From: Chetan Narsude Date: Tue, 5 Mar 2019 12:18:57 -0800 Subject: [PATCH 1/2] optimized the format handling and converted to treating binary formats as byte[] --- .../codegen/rest/OpenApiGenerator.java | 48 ++++++++++++++----- .../resources/templates/rest/pojo.rocker.raw | 13 +++-- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java index fbdadb23b..169674c63 100644 --- a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java +++ b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java @@ -505,24 +505,46 @@ private void handleProperties(List> props, Map pro this.addUnderscores(entryElement); propMap.put("value", Any.wrap(entryElement.getValue())); } + if ("format".equals(entryElement.getKey())) { String s = entryElement.getValue().toString(); - if ("date-time".equals(s)) { - propMap.put("type", Any.wrap("java.time.LocalDateTime")); - } - if ("date".equals(s)) { - propMap.put("type", Any.wrap("java.time.LocalDate")); - } - if ("double".equals(s)) { - propMap.put("type", Any.wrap("java.lang.Double")); - } - if ("float".equals(s)) { - propMap.put("type", Any.wrap("java.lang.Float")); + + String ultimateType; + switch (s) { + case "date-time": + ultimateType = "java.time.LocalDateTime"; + break; + + case "date": + ultimateType = "java.time.LocalDate"; + break; + + case "double": + ultimateType = "java.lang.Double"; + break; + + case "float": + ultimateType = "java.lang.Float"; + break; + + case "int64": + ultimateType = "java.lang.Long"; + break; + + case "binary": + ultimateType = "byte[]"; + propMap.put("isArray", Any.wrap(true)); + break; + + default: + ultimateType = null; } - if ("int64".equals(s)) { - propMap.put("type", Any.wrap("java.lang.Long")); + + if (ultimateType != null) { + propMap.put("type", Any.wrap(ultimateType)); } } + if ("oneOf".equals(entryElement.getKey())) { List list = entryElement.getValue().asList(); Any t = list.get(0).asMap().get("type"); diff --git a/light-rest-4j/src/main/resources/templates/rest/pojo.rocker.raw b/light-rest-4j/src/main/resources/templates/rest/pojo.rocker.raw index b5d423760..91db7eb90 100644 --- a/light-rest-4j/src/main/resources/templates/rest/pojo.rocker.raw +++ b/light-rest-4j/src/main/resources/templates/rest/pojo.rocker.raw @@ -5,7 +5,10 @@ @args (String modelPackage, String className, String classVarName, List> props) package @modelPackage; + +import java.util.Arrays; import java.util.Objects; + import com.fasterxml.jackson.annotation.JsonProperty; public class @className { @@ -27,6 +30,7 @@ public class @className { public void @prop.get("setter")(@prop.get("nameWithEnum") @prop.get("name")) { this.@prop.get("name") = @prop.get("name"); } + } else { @@JsonProperty("@prop.get("jsonProperty")") public @prop.get("type") @prop.get("getter")() { @@ -36,10 +40,10 @@ public class @className { public void @prop.get("setter")(@prop.get("type") @prop.get("name")) { this.@prop.get("name") = @prop.get("name"); } + } } @if(props.size() > 0) { - @@Override public boolean equals(Object o) { if (this == o) { @@ -48,15 +52,16 @@ public class @className { if (o == null || getClass() != o.getClass()) { return false; } + @className @classVarName = (@className) o; - return @for ((i, prop): props) {@if (i.index() < props.size() - 1) {Objects.equals(@prop.get("name"), @classVarName.@prop.get("name")) &&} - @if(i.index() == props.size() - 1){Objects.equals(@prop.get("name"), @classVarName.@prop.get("name"))}}; + return @for ((i, prop): props) {@if (i.index() < props.size() - 1) {@if(prop.get("isArray") == null){Objects}else{Arrays}.equals(@prop.get("name"), @classVarName.@prop.get("name")) &&} + @if(i.index() == props.size() - 1){@if(prop.get("isArray") == null){Objects}else{Arrays}.equals(@prop.get("name"), @classVarName.@prop.get("name"))}}; } @@Override public int hashCode() { - return Objects.hash(@for((i, prop): props) {@if(i.index() < props.size() - 1) {@prop.get("name"),} @if(i.index() == props.size() - 1) {@prop.get("name"));}} + return Objects.hash(7@for((i, prop): props) {, @if (prop.get("isArray") == null){@prop.get("name")} else {Arrays.hashCode(@prop.get("name"))}}); } } From f16f915837f017243bb8300432043dab00038046 Mon Sep 17 00:00:00 2001 From: Chetan Narsude Date: Fri, 8 Mar 2019 22:43:29 -0800 Subject: [PATCH 2/2] handle the strings with binary and byte format properly --- .../codegen/rest/OpenApiGenerator.java | 11 +++++-- .../resources/templates/rest/pojo.rocker.raw | 10 +++--- .../OpenApiArrayReferenceGeneratorTest.java | 32 ++++++++++++++++--- .../src/test/resources/array_ref-oa3.json | 20 ++++++++++++ pom.xml | 2 +- 5 files changed, 62 insertions(+), 13 deletions(-) diff --git a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java index 169674c63..f9c15c0d0 100644 --- a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java +++ b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java @@ -77,8 +77,6 @@ public OpenApiGenerator() { typeMapping.put("double", "Double"); typeMapping.put("object", "Object"); typeMapping.put("integer", "Integer"); - typeMapping.put("ByteArray", "byte[]"); - typeMapping.put("binary", "byte[]"); } @Override @@ -533,7 +531,12 @@ private void handleProperties(List> props, Map pro case "binary": ultimateType = "byte[]"; - propMap.put("isArray", Any.wrap(true)); + propMap.put(COMPARATOR, Any.wrap("Arrays")); + propMap.put(HASHER, Any.wrap("Arrays")); + break; + + case "byte": + ultimateType = "byte"; break; default: @@ -588,6 +591,8 @@ private void handleProperties(List> props, Map pro props.add(propMap); } } + public static final String HASHER = "hasher"; + public static final String COMPARATOR = "comparator"; private Any getListOf(String s) { return new UnresolvedTypeListAny(s); diff --git a/light-rest-4j/src/main/resources/templates/rest/pojo.rocker.raw b/light-rest-4j/src/main/resources/templates/rest/pojo.rocker.raw index 91db7eb90..f8e610512 100644 --- a/light-rest-4j/src/main/resources/templates/rest/pojo.rocker.raw +++ b/light-rest-4j/src/main/resources/templates/rest/pojo.rocker.raw @@ -49,19 +49,20 @@ public class @className { if (this == o) { return true; } + if (o == null || getClass() != o.getClass()) { return false; } @className @classVarName = (@className) o; - return @for ((i, prop): props) {@if (i.index() < props.size() - 1) {@if(prop.get("isArray") == null){Objects}else{Arrays}.equals(@prop.get("name"), @classVarName.@prop.get("name")) &&} - @if(i.index() == props.size() - 1){@if(prop.get("isArray") == null){Objects}else{Arrays}.equals(@prop.get("name"), @classVarName.@prop.get("name"))}}; + return @for ((i, prop): props) {@if (i.index() < props.size() - 1) {@prop.get("comparator")?:"Objects".equals(@prop.get("name"), @classVarName.@prop.get("name")) && + } else {@prop.get("comparator")?:"Objects".equals(@prop.get("name"), @classVarName.@prop.get("name"))}}; } @@Override public int hashCode() { - return Objects.hash(7@for((i, prop): props) {, @if (prop.get("isArray") == null){@prop.get("name")} else {Arrays.hashCode(@prop.get("name"))}}); + return Objects.hash(@for((i, prop): props) {@with(String hasher = prop.get("hasher"), String NULL = null){@if (i.index() < props.size() - 1) {@if (hasher == null){@prop.get("name")} else {@hasher@NULL?:"".hashCode(@prop.get("name"))}, } else {@if (hasher == null){@prop.get("name")} else {@hasher@NULL?:"".hashCode(@prop.get("name"))}}}}); } } @@ -70,7 +71,8 @@ public class @className { StringBuilder sb = new StringBuilder(); sb.append("class @className {\n"); @for(prop: props) { - sb.append(" @prop.get("name"): ").append(toIndentedString(@prop.get("name"))).append("\n");} + sb.append(" @prop.get("name"): ").append(toIndentedString(@prop.get("name"))).append("\n"); + } sb.append("}"); return sb.toString(); } diff --git a/light-rest-4j/src/test/java/com/networknt/codegen/OpenApiArrayReferenceGeneratorTest.java b/light-rest-4j/src/test/java/com/networknt/codegen/OpenApiArrayReferenceGeneratorTest.java index d8ddc4a3f..772181392 100644 --- a/light-rest-4j/src/test/java/com/networknt/codegen/OpenApiArrayReferenceGeneratorTest.java +++ b/light-rest-4j/src/test/java/com/networknt/codegen/OpenApiArrayReferenceGeneratorTest.java @@ -28,10 +28,14 @@ public class OpenApiArrayReferenceGeneratorTest { public static String openapiJson = "/array_ref-oa3.json"; public static String packageName = "com.networknt.petstore.model"; + static JavaPackage javaPackage; + @BeforeClass public static void setUp() throws IOException { delete(Paths.get(targetPath).toFile()); Files.createDirectories(Paths.get(targetPath)); + + javaPackage = prepareJavaPackage(targetPath, packageName); } static void delete(File f) throws IOException { @@ -46,8 +50,7 @@ static void delete(File f) throws IOException { } } - @Test - public void testInvalidVaribleNameGeneratorJson() throws IOException { + public static JavaPackage prepareJavaPackage(String targetPath, String packageName) throws IOException { Any anyConfig = JsonIterator.parse(OpenApiGeneratorTest.class.getResourceAsStream(OpenApiArrayReferenceGeneratorTest.configName), 1024).readAny(); Any anyModel = JsonIterator.parse(OpenApiGeneratorTest.class.getResourceAsStream(OpenApiArrayReferenceGeneratorTest.openapiJson), 1024).readAny(); @@ -57,12 +60,15 @@ public void testInvalidVaribleNameGeneratorJson() throws IOException { File file = new File(targetPath); JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder(); javaProjectBuilder.addSourceTree(file); - JavaPackage javaPackage = javaProjectBuilder.getPackageByName(packageName); + return javaProjectBuilder.getPackageByName(packageName); + } + @Test + public void testTypeReferences() { JavaClass classResponse = javaPackage.getClassByName("Response"); - Assert.assertEquals("Count of fields", 2, classResponse.getFields().size()); + Assert.assertEquals("Count of fields", 3, classResponse.getFields().size()); - logger.debug("The test is to check that the type of contacts is List and not ArrayofContacts"); + logger.debug("The test is to check that the type of contacts is List> and not ArrayofContacts"); JavaField contacts = classResponse.getFieldByName("contacts"); Assert.assertTrue("Data structure for contacts type", contacts.getType() instanceof JavaParameterizedType); @@ -78,5 +84,21 @@ public void testInvalidVaribleNameGeneratorJson() throws IOException { Assert.assertEquals("Type of generic of generic", String.format("%s.Contact", packageName), type.getFullyQualifiedName()); } + @Test + public void testStringFormats() { + JavaClass classSignature = javaPackage.getClassByName("Signature"); + Assert.assertEquals("Count of fields", 2, classSignature.getFields().size()); + + logger.debug("The test is to check that the type of contacts is List and not ArrayofContacts"); + JavaField fieldType = classSignature.getFieldByName("type"); + JavaField fieldData = classSignature.getFieldByName("data"); + + JavaClass type = fieldType.getType(); + Assert.assertEquals("Type of type", "byte", type.getFullyQualifiedName()); + + JavaClass data = fieldData.getType(); + Assert.assertEquals("Type of type", "byte[]", data.getFullyQualifiedName()); + } + private static final Logger logger = LoggerFactory.getLogger(OpenApiArrayReferenceGeneratorTest.class); } diff --git a/light-rest-4j/src/test/resources/array_ref-oa3.json b/light-rest-4j/src/test/resources/array_ref-oa3.json index 68ecaec40..58cf1fde1 100644 --- a/light-rest-4j/src/test/resources/array_ref-oa3.json +++ b/light-rest-4j/src/test/resources/array_ref-oa3.json @@ -173,6 +173,23 @@ "$ref": "#/components/schemas/Contacts" } }, + "Signature": { + "type": "object", + "required": [ + "type", + "data" + ], + "properties": { + "type": { + "type": "string", + "format": "byte" + }, + "data": { + "type": "string", + "format": "binary" + } + } + }, "Response": { "type": "object", "properties": { @@ -181,6 +198,9 @@ }, "contacts": { "$ref": "#/components/schemas/ArrayOfContacts" + }, + "signature": { + "$ref": "#/components/schemas/Signature" } } }, diff --git a/pom.xml b/pom.xml index 4789fa8c5..f26ccef04 100644 --- a/pom.xml +++ b/pom.xml @@ -93,7 +93,7 @@ 0.3 4.0.0 1.20 - 0.22.0 + 1.2.1 1.72 2.18.1 8.0