From cbfc0c5a2d9b904c7cbbf1de56111230abfb8ed3 Mon Sep 17 00:00:00 2001 From: Joe Littlejohn Date: Sun, 28 Feb 2021 01:02:58 +0000 Subject: [PATCH] Add package when javaType is not qualified in enum properties See #1054 for the original change. Enums now have the same behaviour. Closes #1103 --- .../org/jsonschema2pojo/rules/EnumRule.java | 4 ++ .../org/jsonschema2pojo/rules/ObjectRule.java | 65 +++++++++---------- .../jsonschema2pojo/integration/TypeIT.java | 7 ++ .../src/test/resources/schema/type/types.json | 5 ++ 4 files changed, 48 insertions(+), 33 deletions(-) diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/EnumRule.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/EnumRule.java index 9e3a59a67..4888e83e7 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/EnumRule.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/EnumRule.java @@ -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)); diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java index 0d370d60d..8b39e3874 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java @@ -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; @@ -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". @@ -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; } @@ -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 @@ -327,10 +326,10 @@ private void addToString(JDefinedClass jclass) { JExpr.refthis(fieldVar.name()).eq(JExpr._null()), JExpr.lit(""), 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( @@ -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(']'))); @@ -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 diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/TypeIT.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/TypeIT.java index 09995969b..5bf7f3ff1 100644 --- a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/TypeIT.java +++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/TypeIT.java @@ -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") diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/type/types.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/type/types.json index 0c50e584b..1ffebd6a3 100644 --- a/jsonschema2pojo-integration-tests/src/test/resources/schema/type/types.json +++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/type/types.json @@ -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"