Skip to content

Commit

Permalink
Add package when javaType is not qualified in enum properties
Browse files Browse the repository at this point in the history
See #1054 for the original change. Enums now have the same behaviour.

Closes #1103
  • Loading branch information
joelittlejohn committed Feb 28, 2021
1 parent dd269c9 commit cbfc0c5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ protected JDefinedClass createEnum(JsonNode node, String nodeName, JClassContain
throw new GenerationException("Primitive type '" + fqn + "' cannot be used as an enum.");
}

if (fqn.lastIndexOf(".") == -1) { // not a fully qualified name
fqn = container.getPackage().name() + "." + fqn;
}

try {
Class<?> existingClass = Thread.currentThread().getContextClassLoader().loadClass(fqn);
throw new ClassAlreadyExistsException(container.owner().ref(existingClass));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,25 @@

package org.jsonschema2pojo.rules;

import static org.apache.commons.lang3.StringUtils.substringAfter;
import static org.apache.commons.lang3.StringUtils.substringBefore;
import static org.jsonschema2pojo.rules.PrimitiveTypes.isPrimitive;
import static org.jsonschema2pojo.rules.PrimitiveTypes.primitiveType;
import static org.jsonschema2pojo.util.TypeUtil.resolveType;
import static org.apache.commons.lang3.StringUtils.*;
import static org.jsonschema2pojo.rules.PrimitiveTypes.*;
import static org.jsonschema2pojo.util.TypeUtil.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.jsonschema2pojo.AnnotationStyle;
import org.jsonschema2pojo.Annotator;
import org.jsonschema2pojo.Schema;
import org.jsonschema2pojo.exception.ClassAlreadyExistsException;
import org.jsonschema2pojo.exception.GenerationException;
import org.jsonschema2pojo.util.ParcelableHelper;
import org.jsonschema2pojo.util.ReflectionHelper;
import org.jsonschema2pojo.util.SerializableHelper;

import com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.ClassType;
Expand All @@ -39,21 +53,6 @@
import com.sun.codemodel.JPackage;
import com.sun.codemodel.JType;
import com.sun.codemodel.JVar;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.jsonschema2pojo.AnnotationStyle;
import org.jsonschema2pojo.Annotator;
import org.jsonschema2pojo.Schema;
import org.jsonschema2pojo.exception.ClassAlreadyExistsException;
import org.jsonschema2pojo.exception.GenerationException;
import org.jsonschema2pojo.util.ParcelableHelper;
import org.jsonschema2pojo.util.ReflectionHelper;
import org.jsonschema2pojo.util.SerializableHelper;

/**
* Applies the generation steps required for schemas of type "object".
Expand Down Expand Up @@ -220,7 +219,7 @@ private JDefinedClass createClass(String nodeName, JsonNode node, JPackage _pack
}

int index = fqn.lastIndexOf(".") + 1;
if(index == 0){ //Actually not a fully qualified name
if (index == 0) { //Actually not a fully qualified name
fqn = _package.name() + "." + fqn;
index = fqn.lastIndexOf(".") + 1;
}
Expand Down Expand Up @@ -292,16 +291,16 @@ private void addToString(JDefinedClass jclass) {

superToStringInnerConditional._then().add(
sb.invoke("append")
.arg(superString)
.arg(contentStart.plus(JExpr.lit(1)))
.arg(contentEnd));
.arg(superString)
.arg(contentStart.plus(JExpr.lit(1)))
.arg(contentEnd));

// Otherwise, just append super.toString()
superToStringInnerConditional._else().add(sb.invoke("append").arg(superString));

// Append a comma if needed
body._if(sb.invoke("length").gt(baseLength))
._then().add(sb.invoke("append").arg(JExpr.lit(',')));
._then().add(sb.invoke("append").arg(JExpr.lit(',')));
}

// For each included instance field, add to the StringBuilder in the field=value format
Expand All @@ -327,10 +326,10 @@ private void addToString(JDefinedClass jclass) {
JExpr.refthis(fieldVar.name()).eq(JExpr._null()),
JExpr.lit("<null>"),
jclass.owner().ref(Arrays.class).staticInvoke("toString")
.arg(JExpr.refthis(fieldVar.name()))
.invoke("replace").arg(JExpr.lit('[')).arg(JExpr.lit('{'))
.invoke("replace").arg(JExpr.lit(']')).arg(JExpr.lit('}'))
.invoke("replace").arg(JExpr.lit(", ")).arg(JExpr.lit(",")))));
.arg(JExpr.refthis(fieldVar.name()))
.invoke("replace").arg(JExpr.lit('[')).arg(JExpr.lit('{'))
.invoke("replace").arg(JExpr.lit(']')).arg(JExpr.lit('}'))
.invoke("replace").arg(JExpr.lit(", ")).arg(JExpr.lit(",")))));
} else {
body.add(sb.invoke("append")
.arg(JOp.cond(
Expand All @@ -345,12 +344,12 @@ private void addToString(JDefinedClass jclass) {
// Add the trailer
JConditional trailerConditional = body._if(
sb.invoke("charAt").arg(sb.invoke("length").minus(JExpr.lit(1)))
.eq(JExpr.lit(',')));
.eq(JExpr.lit(',')));

trailerConditional._then().add(
sb.invoke("setCharAt")
.arg(sb.invoke("length").minus(JExpr.lit(1)))
.arg(JExpr.lit(']')));
.arg(sb.invoke("length").minus(JExpr.lit(1)))
.arg(JExpr.lit(']')));

trailerConditional._else().add(
sb.invoke("append").arg(JExpr.lit(']')));
Expand Down Expand Up @@ -498,7 +497,7 @@ private void addEquals(JDefinedClass jclass, JsonNode node) {
} else {
fieldEquals = thisFieldRef.eq(otherFieldRef).cor(
thisFieldRef.ne(JExpr._null())
.cand(thisFieldRef.invoke("equals").arg(otherFieldRef)));
.cand(thisFieldRef.invoke("equals").arg(otherFieldRef)));
}

// Chain the equality of this field with the previous comparisons
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ public void javaTypeCanBeUsedForAnyShemaType() throws NoSuchMethodException {

}

@Test
public void enumCanHaveUnqualifiedJavaType() throws NoSuchMethodException {

assertThat(classWithManyTypes.getMethod("getEnumWithUnqualifiedJavaType").getReturnType().getName(), is("com.example.Bar"));

}

@Test
public void maximumGreaterThanIntegerMaxCausesIntegersToBecomeLongs() throws ClassNotFoundException, NoSuchMethodException, SecurityException {
Class<?> classWithLongProperty = schemaRule.generateAndCompile("/schema/type/integerWithLongMaximumAsLong.json", "com.example")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
"enum" : ["a", "b", "c"],
"existingJavaType" : "java.lang.Boolean"
},
"enumWithUnqualifiedJavaType" : {
"type" : "string",
"enum" : ["a", "b", "c"],
"javaType" : "Bar"
},
"booleanWithJavaType" : {
"type" : "boolean",
"existingJavaType" : "long"
Expand Down

0 comments on commit cbfc0c5

Please sign in to comment.