From 604a51d683914fa06ea26cdd4408c7f8babde97d Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 18 Jan 2023 20:03:34 -0800 Subject: [PATCH] Fix #3742: from `schemaType` for `LongSerializer` (and `ShortSerializer`) --- release-notes/VERSION-2.x | 2 ++ .../databind/ser/std/NumberSerializers.java | 4 ++-- .../jsonschema/TestGenerateJsonSchema.java | 21 +++++++++++++++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 211f1aa1ec..8cac1fe524 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -27,6 +27,8 @@ Project: jackson-databind #3708: Seems like `java.nio.file.Path` is safe for Android API level 26 (contributed by @pjfanning) #3736: Try to avoid auto-detecting Fields for Record types +#3742: schemaType of `LongSerializer` is wrong + (reported by @luozhenyu) 2.14.2 (not yet released) diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/NumberSerializers.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/NumberSerializers.java index 52412b3a0b..f9c2c6bd15 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/std/NumberSerializers.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/NumberSerializers.java @@ -121,7 +121,7 @@ public static class ShortSerializer extends Base { final static ShortSerializer instance = new ShortSerializer(); public ShortSerializer() { - super(Short.class, JsonParser.NumberType.INT, "number"); + super(Short.class, JsonParser.NumberType.INT, "integer"); } @Override @@ -186,7 +186,7 @@ public void serialize(Object value, JsonGenerator gen, @JacksonStdImpl public static class LongSerializer extends Base { public LongSerializer(Class cls) { - super(cls, JsonParser.NumberType.LONG, "number"); + super(cls, JsonParser.NumberType.LONG, "integer"); } @Override diff --git a/src/test/java/com/fasterxml/jackson/databind/jsonschema/TestGenerateJsonSchema.java b/src/test/java/com/fasterxml/jackson/databind/jsonschema/TestGenerateJsonSchema.java index 30856e88e1..faacf0b2f2 100644 --- a/src/test/java/com/fasterxml/jackson/databind/jsonschema/TestGenerateJsonSchema.java +++ b/src/test/java/com/fasterxml/jackson/databind/jsonschema/TestGenerateJsonSchema.java @@ -34,7 +34,7 @@ public static class SimpleBean private Collection property4; @JsonProperty(required=true) private String property5; - + public int getProperty1() { return property1; @@ -80,10 +80,13 @@ public String getProperty5() return property5; } - public void setProperty5(String property5) - { + public void setProperty5(String property5) { this.property5 = property5; } + + public long getProperty6() { + return 0L; + } } public class TrivialBean { @@ -119,7 +122,7 @@ static class Numbers { /********************************************************** */ - private final ObjectMapper MAPPER = new ObjectMapper(); + private final ObjectMapper MAPPER = newJsonMapper(); /** * tests generating json-schema stuff. @@ -162,6 +165,16 @@ public void testOldSchemaGeneration() throws Exception assertEquals("array", property4Schema.get("type").asText()); assertEquals(false, property4Schema.path("required").booleanValue()); assertEquals("number", property4Schema.get("items").get("type").asText()); + + JsonNode property5Schema = propertiesSchema.get("property5"); + assertNotNull(property5Schema); + assertEquals("string", property5Schema.get("type").asText()); + assertEquals(true, property5Schema.path("required").booleanValue()); + + JsonNode property6Schema = propertiesSchema.get("property6"); + assertNotNull(property6Schema); + assertEquals("integer", property6Schema.get("type").asText()); + assertEquals(false, property6Schema.path("required").booleanValue()); } @JsonFilter("filteredBean")