From f230dc36b830d79f2a97f48640d4fe84ca93d8b6 Mon Sep 17 00:00:00 2001 From: nico Date: Mon, 18 Nov 2019 23:27:23 +0100 Subject: [PATCH 1/4] created test case --- .../jackson/jsonSchema/SubtypeOrderTest.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/test/java/com/kjetland/jackson/jsonSchema/SubtypeOrderTest.java diff --git a/src/test/java/com/kjetland/jackson/jsonSchema/SubtypeOrderTest.java b/src/test/java/com/kjetland/jackson/jsonSchema/SubtypeOrderTest.java new file mode 100644 index 0000000..58dc57b --- /dev/null +++ b/src/test/java/com/kjetland/jackson/jsonSchema/SubtypeOrderTest.java @@ -0,0 +1,38 @@ +package com.kjetland.jackson.jsonSchema; + +import java.util.List; +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; + +public class SubtypeOrderTest { + + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") + @JsonSubTypes({ + @JsonSubTypes.Type(Line.class), + @JsonSubTypes.Type(Pt.class) }) + public static class Loc { + } + @JsonSubTypes({ + @JsonSubTypes.Type(Abs.class) + }) + public static class Pt extends Loc { + } + public static class Abs extends Pt { + } + public static class Line extends Loc { + public List attr; + } + + private final ObjectMapper MAPPER = new ObjectMapper(); + + public void testGenerateSchema() { + com.kjetland.jackson.jsonSchema.JsonSchemaGenerator generator = new com.kjetland.jackson.jsonSchema.JsonSchemaGenerator(MAPPER); + ObjectWriter objectWriter = MAPPER.writerWithDefaultPrettyPrinter(); + JsonNode jsonNode = generator.generateJsonSchema(Loc.class); + System.out.println(objectWriter.writeValueAsString(jsonNode)); + + } +} From 5df13c3a0415fa5d9e9536cc1e29d264aa93b0bc Mon Sep 17 00:00:00 2001 From: nico Date: Mon, 18 Nov 2019 23:39:49 +0100 Subject: [PATCH 2/4] declare exception --- .../java/com/kjetland/jackson/jsonSchema/SubtypeOrderTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/kjetland/jackson/jsonSchema/SubtypeOrderTest.java b/src/test/java/com/kjetland/jackson/jsonSchema/SubtypeOrderTest.java index 58dc57b..fc6b5aa 100644 --- a/src/test/java/com/kjetland/jackson/jsonSchema/SubtypeOrderTest.java +++ b/src/test/java/com/kjetland/jackson/jsonSchema/SubtypeOrderTest.java @@ -1,5 +1,6 @@ package com.kjetland.jackson.jsonSchema; +import java.io.IOException; import java.util.List; import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; @@ -28,7 +29,7 @@ public static class Line extends Loc { private final ObjectMapper MAPPER = new ObjectMapper(); - public void testGenerateSchema() { + public void testGenerateSchema() throws IOException { com.kjetland.jackson.jsonSchema.JsonSchemaGenerator generator = new com.kjetland.jackson.jsonSchema.JsonSchemaGenerator(MAPPER); ObjectWriter objectWriter = MAPPER.writerWithDefaultPrettyPrinter(); JsonNode jsonNode = generator.generateJsonSchema(Loc.class); From 764dbe39710c642a1b2d86c04a3cb20d45e61883 Mon Sep 17 00:00:00 2001 From: Alessio Stalla Date: Fri, 4 Jun 2021 14:24:15 +0200 Subject: [PATCH 3/4] =?UTF-8?q?Attempt=20at=20a=20fix=20for=20https://gith?= =?UTF-8?q?ub.com/mbknor/mbknor-jackson-jsonSchema/issues/106=20=E2=80=93?= =?UTF-8?q?=20Wrong=20type=20exception=20when=20changing=20the=20order=20o?= =?UTF-8?q?f=20@JsonSubTypes=20annotation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jsonSchema/JsonSchemaGenerator.scala | 4 ++-- .../jackson/jsonSchema/SubtypeOrderTest.java | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/scala/com/kjetland/jackson/jsonSchema/JsonSchemaGenerator.scala b/src/main/scala/com/kjetland/jackson/jsonSchema/JsonSchemaGenerator.scala index b7814e5..60c64c4 100755 --- a/src/main/scala/com/kjetland/jackson/jsonSchema/JsonSchemaGenerator.scala +++ b/src/main/scala/com/kjetland/jackson/jsonSchema/JsonSchemaGenerator.scala @@ -357,9 +357,9 @@ class JsonSchemaGenerator case Some(w) => // this is a recursive polymorphism call - if ( _type != w.typeInProgress) throw new Exception(s"Wrong type - working on ${w.typeInProgress} - got ${_type}") + if (!w.typeInProgress.isTypeOrSuperTypeOf(_type.getRawClass)) throw new Exception(s"Wrong type - working on ${w.typeInProgress} - got ${_type}") - DefinitionInfo(None, objectDefinitionBuilder(w.nodeInProgress)) + DefinitionInfo(if(_type == w.typeInProgress) None else Some(ref), objectDefinitionBuilder(w.nodeInProgress)) } case None => diff --git a/src/test/java/com/kjetland/jackson/jsonSchema/SubtypeOrderTest.java b/src/test/java/com/kjetland/jackson/jsonSchema/SubtypeOrderTest.java index fc6b5aa..ac1a440 100644 --- a/src/test/java/com/kjetland/jackson/jsonSchema/SubtypeOrderTest.java +++ b/src/test/java/com/kjetland/jackson/jsonSchema/SubtypeOrderTest.java @@ -1,6 +1,7 @@ package com.kjetland.jackson.jsonSchema; import java.io.IOException; +import java.util.Collections; import java.util.List; import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; @@ -20,10 +21,14 @@ public static class Loc { @JsonSubTypes.Type(Abs.class) }) public static class Pt extends Loc { + public String somePayload; } public static class Abs extends Pt { } public static class Line extends Loc { + public List attr; + } + public static class Proxy { public List attr; } @@ -34,6 +39,19 @@ public void testGenerateSchema() throws IOException { ObjectWriter objectWriter = MAPPER.writerWithDefaultPrettyPrinter(); JsonNode jsonNode = generator.generateJsonSchema(Loc.class); System.out.println(objectWriter.writeValueAsString(jsonNode)); + String value = objectWriter.writeValueAsString(new Pt()); + System.out.println(value); + Pt pt = MAPPER.readValue(value, Pt.class); + Line line = new Line(); + Proxy proxy = new Proxy(); + proxy.attr = Collections.singletonList(new Abs()); + line.attr = Collections.singletonList(proxy); + value = objectWriter.writeValueAsString(line); + System.out.println(value); + line = MAPPER.readValue(value, Line.class); + } + public static void main(String[] args) throws IOException { + new SubtypeOrderTest().testGenerateSchema(); } } From 6bb44c343a3ab52917805057d858d1bfc304374c Mon Sep 17 00:00:00 2001 From: Alessio Stalla Date: Mon, 7 Jun 2021 08:47:03 +0200 Subject: [PATCH 4/4] Remove duplicate type property names in required array --- .../jsonSchema/JsonSchemaGenerator.scala | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/scala/com/kjetland/jackson/jsonSchema/JsonSchemaGenerator.scala b/src/main/scala/com/kjetland/jackson/jsonSchema/JsonSchemaGenerator.scala index 18e023a..3632d09 100755 --- a/src/main/scala/com/kjetland/jackson/jsonSchema/JsonSchemaGenerator.scala +++ b/src/main/scala/com/kjetland/jackson/jsonSchema/JsonSchemaGenerator.scala @@ -12,7 +12,7 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize import com.fasterxml.jackson.databind.introspect.{AnnotatedClass, AnnotatedClassResolver} import com.fasterxml.jackson.databind.jsonFormatVisitors._ import com.fasterxml.jackson.databind.jsontype.impl.MinimalClassNameIdResolver -import com.fasterxml.jackson.databind.node.{ArrayNode, JsonNodeFactory, ObjectNode} +import com.fasterxml.jackson.databind.node.{ArrayNode, JsonNodeFactory, ObjectNode, TextNode} import com.fasterxml.jackson.databind.util.ClassUtil import com.kjetland.jackson.jsonSchema.annotations._ import io.github.classgraph.{ClassGraph, ScanResult} @@ -1064,8 +1064,8 @@ class JsonSchemaGenerator val propertiesNode = getOrCreateObjectChild(thisObjectNode, "properties") - extractPolymorphismInfo(_type).map { - case pi: PolymorphismInfo => + extractPolymorphismInfo(_type).foreach { + pi: PolymorphismInfo => // This class is a child in a polymorphism config.. // Set the title = subTypeName thisObjectNode.put("title", pi.subTypeName) @@ -1088,14 +1088,24 @@ class JsonSchemaGenerator optionsNode.put("hidden", true) } - getRequiredArrayNode(thisObjectNode).add(pi.typePropertyName) + var found = false + val reqArrayNode = getRequiredArrayNode(thisObjectNode) + val iterator = reqArrayNode.elements() + while(iterator.hasNext) { + if(iterator.next().equals(TextNode.valueOf(pi.typePropertyName))) { + found = true + } + } + if(!found) { + reqArrayNode.add(pi.typePropertyName) + } if (config.useMultipleEditorSelectViaProperty) { // https://github.com/jdorn/json-editor/issues/709 // Generate info to help generated editor to select correct oneOf-type // when populating the gui/schema with existing data - val objectOptionsNode = getOrCreateObjectChild( thisObjectNode, "options") - val multipleEditorSelectViaPropertyNode = getOrCreateObjectChild( objectOptionsNode, "multiple_editor_select_via_property") + val objectOptionsNode = getOrCreateObjectChild(thisObjectNode, "options") + val multipleEditorSelectViaPropertyNode = getOrCreateObjectChild(objectOptionsNode, "multiple_editor_select_via_property") multipleEditorSelectViaPropertyNode.put("property", pi.typePropertyName) multipleEditorSelectViaPropertyNode.put("value", pi.subTypeName) ()