use of io.fabric8.java.generator.nodes.GeneratorResult in project kubernetes-client by fabric8io.
the class JObject method generateJava.
@Override
public GeneratorResult generateJava() {
CompilationUnit cu = new CompilationUnit();
if (!this.pkg.isEmpty()) {
cu.setPackageDeclaration(this.pkg);
}
ClassOrInterfaceDeclaration clz = cu.addClass(this.className);
clz.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonInclude"), new NameExpr("com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL")));
clz.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonPropertyOrder"), new NameExpr(getSortedFieldsAsParam(this.fields.keySet()))));
clz.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.databind.annotation.JsonDeserialize"), new NameExpr("using = com.fasterxml.jackson.databind.JsonDeserializer.None.class")));
if (config.isObjectExtraAnnotations()) {
addExtraAnnotations(clz);
}
clz.addImplementedType("io.fabric8.kubernetes.api.model.KubernetesResource");
List<GeneratorResult.ClassResult> buffer = new ArrayList<>(this.fields.size() + 1);
List<String> sortedKeys = this.fields.keySet().stream().sorted().collect(Collectors.toList());
for (String k : sortedKeys) {
AbstractJSONSchema2Pojo prop = this.fields.get(k);
boolean isRequired = this.required.contains(k);
GeneratorResult gr = prop.generateJava();
// For now the inner types are only for enums
if (!gr.getInnerClasses().isEmpty()) {
for (GeneratorResult.ClassResult enumCR : gr.getInnerClasses()) {
Optional<EnumDeclaration> ed = enumCR.getCompilationUnit().getEnumByName(enumCR.getName());
if (ed.isPresent()) {
clz.addMember(ed.get());
}
}
}
buffer.addAll(gr.getTopLevelClasses());
String originalFieldName = k;
String fieldName = AbstractJSONSchema2Pojo.sanitizeString(k);
String fieldType = prop.getType();
try {
FieldDeclaration objField = clz.addField(fieldType, fieldName, Modifier.Keyword.PRIVATE);
objField.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonProperty"), new StringLiteralExpr(originalFieldName)));
if (isRequired) {
objField.addAnnotation("javax.validation.constraints.NotNull");
}
objField.createGetter();
objField.createSetter();
if (Utils.isNotNullOrEmpty(prop.getDescription())) {
objField.setJavadocComment(prop.getDescription());
objField.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonPropertyDescription"), new StringLiteralExpr(prop.getDescription().replace("\"", "\\\""))));
}
} catch (Exception cause) {
throw new JavaGeneratorException("Error generating field " + fieldName + " with type " + prop.getType(), cause);
}
}
if (this.preserveUnknownFields || config.isAlwaysPreserveUnknownFields()) {
ClassOrInterfaceType mapType = new ClassOrInterfaceType().setName(Keywords.JAVA_UTIL_MAP).setTypeArguments(new ClassOrInterfaceType().setName("String"), new ClassOrInterfaceType().setName("Object"));
FieldDeclaration objField = clz.addField(mapType, Keywords.ADDITIONAL_PROPERTIES, Modifier.Keyword.PRIVATE);
objField.setVariables(new NodeList<>(new VariableDeclarator().setName(Keywords.ADDITIONAL_PROPERTIES).setType(mapType).setInitializer("new java.util.HashMap<>()")));
objField.addAnnotation("com.fasterxml.jackson.annotation.JsonIgnore");
objField.createGetter().addAnnotation("com.fasterxml.jackson.annotation.JsonAnyGetter");
objField.createSetter().addAnnotation("com.fasterxml.jackson.annotation.JsonAnySetter");
}
buffer.add(new GeneratorResult.ClassResult(this.className, cu));
return new GeneratorResult(buffer);
}
use of io.fabric8.java.generator.nodes.GeneratorResult in project kubernetes-client by fabric8io.
the class GeneratorTest method testObjectOfObjectsWithAlwaysPrefix.
@Test
void testObjectOfObjectsWithAlwaysPrefix() {
// Arrange
Config config = new Config(null, Config.Prefix.ALWAYS, null, null, null, null);
Map<String, JSONSchemaProps> props = new HashMap<>();
JSONSchemaProps newObj = new JSONSchemaProps();
newObj.setType("object");
props.put("o1", newObj);
JObject obj = new JObject(null, "t", props, null, false, "My", "", config, null);
// Act
GeneratorResult res = obj.generateJava();
// Assert
assertEquals(2, res.getTopLevelClasses().size());
assertEquals("MyO1", res.getTopLevelClasses().get(0).getName());
assertEquals("MyT", res.getTopLevelClasses().get(1).getName());
}
use of io.fabric8.java.generator.nodes.GeneratorResult in project kubernetes-client by fabric8io.
the class GeneratorTest method testObjectOfPrimitives.
@Test
void testObjectOfPrimitives() {
// Arrange
Map<String, JSONSchemaProps> props = new HashMap<>();
JSONSchemaProps newBool = new JSONSchemaProps();
newBool.setType("boolean");
props.put("o1", newBool);
JObject obj = new JObject("v1alpha1", "t", props, null, false, "", "", defaultConfig, null);
// Act
GeneratorResult res = obj.generateJava();
// Assert
assertEquals("v1alpha1.T", obj.getType());
assertEquals(1, res.getTopLevelClasses().size());
assertEquals("T", res.getTopLevelClasses().get(0).getName());
Optional<ClassOrInterfaceDeclaration> clz = res.getTopLevelClasses().get(0).getCompilationUnit().getClassByName("T");
assertTrue(clz.isPresent());
assertEquals(1, clz.get().getFields().size());
assertTrue(clz.get().getFieldByName("o1").isPresent());
}
use of io.fabric8.java.generator.nodes.GeneratorResult in project kubernetes-client by fabric8io.
the class GeneratorTest method testDefaultEnum.
@Test
void testDefaultEnum() {
// Arrange
Map<String, JSONSchemaProps> props = new HashMap<>();
JSONSchemaProps newEnum = new JSONSchemaProps();
newEnum.setType("string");
List<JsonNode> enumValues = new ArrayList<>();
enumValues.add(new TextNode("foo"));
enumValues.add(new TextNode("bar"));
enumValues.add(new TextNode("baz"));
props.put("e1", newEnum);
JEnum enu = new JEnum("t", enumValues, defaultConfig, null);
// Act
GeneratorResult res = enu.generateJava();
// Assert
assertEquals("T", enu.getType());
assertEquals(1, res.getInnerClasses().size());
assertEquals("T", res.getInnerClasses().get(0).getName());
Optional<EnumDeclaration> en = res.getInnerClasses().get(0).getCompilationUnit().getEnumByName("T");
assertTrue(en.isPresent());
assertEquals(3, en.get().getEntries().size());
assertEquals("FOO", en.get().getEntries().get(0).getName().asString());
assertEquals("BAR", en.get().getEntries().get(1).getName().asString());
assertEquals("BAZ", en.get().getEntries().get(2).getName().asString());
}
use of io.fabric8.java.generator.nodes.GeneratorResult in project kubernetes-client by fabric8io.
the class GeneratorTest method testClassNamesDisambiguationWithPackageNesting.
@Test
void testClassNamesDisambiguationWithPackageNesting() {
// Arrange
Map<String, JSONSchemaProps> props = new HashMap<>();
JSONSchemaProps newObj1 = new JSONSchemaProps();
JSONSchemaProps newObj2 = new JSONSchemaProps();
newObj1.setType("object");
newObj2.setType("object");
Map<String, JSONSchemaProps> obj2Props = new HashMap<>();
obj2Props.put("o1", newObj1);
obj2Props.put("o2", newObj1);
obj2Props.put("o3", newObj1);
newObj2.setProperties(obj2Props);
props.put("o1", newObj1);
props.put("o2", newObj2);
JObject obj = new JObject("v1alpha1", "t", props, null, false, "", "", defaultConfig, null);
// Act
GeneratorResult res = obj.generateJava();
// Assert
assertEquals(6, res.getTopLevelClasses().size());
// The order here is not important
assertEquals("O1", res.getTopLevelClasses().get(0).getName());
assertEquals("O1", res.getTopLevelClasses().get(1).getName());
assertEquals("O2", res.getTopLevelClasses().get(2).getName());
assertEquals("O3", res.getTopLevelClasses().get(3).getName());
assertEquals("O2", res.getTopLevelClasses().get(4).getName());
assertEquals("T", res.getTopLevelClasses().get(5).getName());
CompilationUnit cuT = res.getTopLevelClasses().get(5).getCompilationUnit();
Optional<ClassOrInterfaceDeclaration> clzT = cuT.getClassByName("T");
assertTrue(clzT.isPresent());
assertEquals(2, clzT.get().getFields().size());
assertTrue(clzT.get().getFieldByName("o1").isPresent());
assertEquals("v1alpha1", cuT.getPackageDeclaration().get().getNameAsString());
assertEquals("v1alpha1.t.O1", clzT.get().getFieldByName("o1").get().getElementType().toString());
CompilationUnit cuO1 = res.getTopLevelClasses().get(0).getCompilationUnit();
Optional<ClassOrInterfaceDeclaration> clzO1 = cuO1.getClassByName("O1");
assertTrue(clzO1.isPresent());
assertEquals("v1alpha1.t", cuO1.getPackageDeclaration().get().getNameAsString());
CompilationUnit cuO2 = res.getTopLevelClasses().get(4).getCompilationUnit();
Optional<ClassOrInterfaceDeclaration> clzO2 = cuO2.getClassByName("O2");
assertTrue(clzO2.isPresent());
assertTrue(clzO2.get().getFieldByName("o1").isPresent());
assertEquals("v1alpha1.t", cuO2.getPackageDeclaration().get().getNameAsString());
assertEquals("v1alpha1.t.o2.O1", clzO2.get().getFieldByName("o1").get().getElementType().toString());
assertTrue(clzO2.get().getFieldByName("o2").isPresent());
assertTrue(clzO2.get().getFieldByName("o3").isPresent());
}
Aggregations