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 c11361d86..0ce1c0d89 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java @@ -259,6 +259,16 @@ private JDefinedClass createClass(String nodeName, JsonNode node, JPackage _pack if (isPrimitive(fqn, _package.owner())) { throw new ClassAlreadyExistsException(primitiveType(fqn, _package.owner())); } + JClass existingClass; + + try { + _package.owner().ref(Thread.currentThread().getContextClassLoader().loadClass(fqn)); + existingClass = resolveType(_package, fqn + (node.get("javaType").asText().contains("<") ? "<" + substringAfter(node.get("javaType").asText(), "<") : "")); + + throw new ClassAlreadyExistsException(existingClass); + } catch (ClassNotFoundException e) { + + } int index = fqn.lastIndexOf(".") + 1; if (index >= 0 && index < fqn.length()) { @@ -266,16 +276,18 @@ private JDefinedClass createClass(String nodeName, JsonNode node, JPackage _pack } try { + _package.owner().ref(Thread.currentThread().getContextClassLoader().loadClass(fqn)); - JClass existingClass = resolveType(_package, fqn + (node.get("javaType").asText().contains("<") ? "<" + substringAfter(node.get("javaType").asText(), "<") : "")); + existingClass = resolveType(_package, fqn + (node.get("javaType").asText().contains("<") ? "<" + substringAfter(node.get("javaType").asText(), "<") : "")); throw new ClassAlreadyExistsException(existingClass); } catch (ClassNotFoundException e) { - if (usePolymorphicDeserialization) { - newType = _package.owner()._class(JMod.PUBLIC, fqn, ClassType.CLASS); - } else { - newType = _package.owner()._class(fqn); - } + + } + if (usePolymorphicDeserialization) { + newType = _package.owner()._class(JMod.PUBLIC, fqn, ClassType.CLASS); + } else { + newType = _package.owner()._class(fqn); } } else { if (usePolymorphicDeserialization) { diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/config/PrefixSuffixIT.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/config/PrefixSuffixIT.java index 0d9ee6b9b..cdc356767 100644 --- a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/config/PrefixSuffixIT.java +++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/config/PrefixSuffixIT.java @@ -39,6 +39,14 @@ public void customClassPrefix() throws ClassNotFoundException{ ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/properties/primitiveProperties.json", "com.example", config("classNamePrefix","Abstract")); resultsClassLoader.loadClass("com.example.AbstractPrimitiveProperties"); } + + @Test(expected = ClassNotFoundException.class) + public void customClassPrefixExistingClass() throws ClassNotFoundException { + + ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/properties/objectPropertiesJavaType.json", + "com.example", config("classNamePrefix", "SomePrefix")); + resultsClassLoader.loadClass("org.jsonschema2pojo.SomePrefixNoopAnnotator"); + } @Test public void noCapsCustomClassPrefix() throws ClassNotFoundException{ @@ -130,4 +138,5 @@ public void customClassPrefixAndSuffixNoJavaType() throws ClassNotFoundException "com.example", config("classNamePrefix", "Prefix", "classNameSuffix","Suffix")); resultsClassLoader.loadClass("com.example.PrefixPrimitivePropertiesNoJavaTypeSuffix"); } + } diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/properties/objectPropertiesJavaType.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/properties/objectPropertiesJavaType.json new file mode 100644 index 000000000..e39723de8 --- /dev/null +++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/properties/objectPropertiesJavaType.json @@ -0,0 +1,9 @@ +{ + "type" : "object", + "properties" : { + "a" : { + "javaType": "org.jsonschema2pojo.NoopAnnotator", + "type": "object" + } + } +} \ No newline at end of file