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
Expand Up @@ -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
Expand Down Expand Up @@ -505,24 +503,51 @@ private void handleProperties(List<Map<String, Any>> props, Map<String, Any> 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(COMPARATOR, Any.wrap("Arrays"));
propMap.put(HASHER, Any.wrap("Arrays"));
break;

case "byte":
ultimateType = "byte";
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<Any> list = entryElement.getValue().asList();
Any t = list.get(0).asMap().get("type");
Expand Down Expand Up @@ -566,6 +591,8 @@ private void handleProperties(List<Map<String, Any>> props, Map<String, Any> 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);
Expand Down
17 changes: 12 additions & 5 deletions light-rest-4j/src/main/resources/templates/rest/pojo.rocker.raw
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

@args (String modelPackage, String className, String classVarName, List<Map<String, Any>> props)
package @modelPackage;

import java.util.Arrays;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonProperty;

public class @className {
Expand All @@ -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")() {
Expand All @@ -36,27 +40,29 @@ 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) {
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) {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) {@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(@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(@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"))}}}});
}

}
Expand All @@ -65,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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();

Expand All @@ -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<List<Contact> and not ArrayofContacts");
logger.debug("The test is to check that the type of contacts is List<List<Contact>> and not ArrayofContacts");
JavaField contacts = classResponse.getFieldByName("contacts");
Assert.assertTrue("Data structure for contacts type", contacts.getType() instanceof JavaParameterizedType);

Expand All @@ -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<List<Contact> 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);
}
20 changes: 20 additions & 0 deletions light-rest-4j/src/test/resources/array_ref-oa3.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -181,6 +198,9 @@
},
"contacts": {
"$ref": "#/components/schemas/ArrayOfContacts"
},
"signature": {
"$ref": "#/components/schemas/Signature"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
<version.zkclient>0.3</version.zkclient>
<version.curator>4.0.0</version.curator>
<version.snakeyaml>1.20</version.snakeyaml>
<version.rocker>0.22.0</version.rocker>
<version.rocker>1.2.1</version.rocker>
<version.jcommander>1.72</version.jcommander>
<version.fastscanner>2.18.1</version.fastscanner>
<version.graphql>8.0</version.graphql>
Expand Down