From 5b2a9114554fd7501d32db29733813da309c31b8 Mon Sep 17 00:00:00 2001 From: Michael Edgar Date: Mon, 6 Jan 2025 07:29:09 -0500 Subject: [PATCH] fix: use more accurate formats for byte, short, and char types (#2128) Signed-off-by: Michael Edgar --- .../openapi/runtime/util/TypeUtil.java | 16 ++++----- .../scanner/StandaloneSchemaScanTest.java | 16 +++++++++ ...omponents.schemas.array-type-override.json | 4 +-- .../components.schemas.primitive-formats.json | 36 +++++++++++++++++++ 4 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 core/src/test/resources/io/smallrye/openapi/runtime/scanner/components.schemas.primitive-formats.json diff --git a/core/src/main/java/io/smallrye/openapi/runtime/util/TypeUtil.java b/core/src/main/java/io/smallrye/openapi/runtime/util/TypeUtil.java index 94d3b71cb..c219bc00b 100644 --- a/core/src/main/java/io/smallrye/openapi/runtime/util/TypeUtil.java +++ b/core/src/main/java/io/smallrye/openapi/runtime/util/TypeUtil.java @@ -56,8 +56,7 @@ public class TypeUtil { private static final TypeWithFormat ANY = TypeWithFormat.anyType().build(); private static final TypeWithFormat STRING_FORMAT = TypeWithFormat.of(SchemaType.STRING).build(); private static final TypeWithFormat BINARY_FORMAT = TypeWithFormat.of(SchemaType.STRING).format(DataFormat.BINARY).build(); - private static final TypeWithFormat BYTE_FORMAT = TypeWithFormat.of(SchemaType.STRING).format(DataFormat.BYTE).build(); - private static final TypeWithFormat CHAR_FORMAT = TypeWithFormat.of(SchemaType.STRING).format(DataFormat.BYTE).build(); + private static final TypeWithFormat CHAR_FORMAT = TypeWithFormat.of(SchemaType.STRING).format(DataFormat.CHAR).build(); private static final TypeWithFormat UUID_FORMAT = TypeWithFormat.of(SchemaType.STRING).format(DataFormat.UUID) .pattern(UUID_PATTERN).build(); private static final TypeWithFormat URI_FORMAT = TypeWithFormat.of(SchemaType.STRING).format(DataFormat.URI).build(); @@ -68,7 +67,8 @@ public class TypeUtil { private static final TypeWithFormat BIGINTEGER_FORMAT = TypeWithFormat.of(SchemaType.INTEGER).build(); private static final TypeWithFormat INTEGER_FORMAT = TypeWithFormat.of(SchemaType.INTEGER).format(DataFormat.INT32).build(); private static final TypeWithFormat LONG_FORMAT = TypeWithFormat.of(SchemaType.INTEGER).format(DataFormat.INT64).build(); - private static final TypeWithFormat SHORT_FORMAT = TypeWithFormat.of(SchemaType.INTEGER).build(); + private static final TypeWithFormat SHORT_FORMAT = TypeWithFormat.of(SchemaType.INTEGER).format(DataFormat.INT16).build(); + private static final TypeWithFormat BYTE_FORMAT = TypeWithFormat.of(SchemaType.INTEGER).format(DataFormat.INT8).build(); private static final TypeWithFormat BOOLEAN_FORMAT = TypeWithFormat.of(SchemaType.BOOLEAN).build(); // SPECIAL FORMATS private static final TypeWithFormat ARRAY_FORMAT = TypeWithFormat.of(SchemaType.ARRAY).build(); @@ -122,10 +122,6 @@ public class TypeUtil { TYPE_MAP.put(DotName.createSimple(java.net.URI.class.getName()), URI_FORMAT); TYPE_MAP.put(DotName.createSimple(java.net.URL.class.getName()), STRING_FORMAT); TYPE_MAP.put(DotName.createSimple(java.util.UUID.class.getName()), UUID_FORMAT); - - // B64 String - TYPE_MAP.put(DotName.createSimple(Byte.class.getName()), BYTE_FORMAT); - TYPE_MAP.put(DotName.createSimple(byte.class.getName()), BYTE_FORMAT); TYPE_MAP.put(DotName.createSimple(Character.class.getName()), CHAR_FORMAT); TYPE_MAP.put(DotName.createSimple(char.class.getName()), CHAR_FORMAT); @@ -153,6 +149,8 @@ public class TypeUtil { TYPE_MAP.put(DotName.createSimple(long.class.getName()), LONG_FORMAT); TYPE_MAP.put(DotName.createSimple(Short.class.getName()), SHORT_FORMAT); TYPE_MAP.put(DotName.createSimple(short.class.getName()), SHORT_FORMAT); + TYPE_MAP.put(DotName.createSimple(Byte.class.getName()), BYTE_FORMAT); + TYPE_MAP.put(DotName.createSimple(byte.class.getName()), BYTE_FORMAT); // Boolean TYPE_MAP.put(DotName.createSimple(Boolean.class.getName()), BOOLEAN_FORMAT); @@ -882,12 +880,14 @@ public boolean isOpaque() { } private static class DataFormat { + static final String INT8 = "int8"; + static final String INT16 = "int16"; static final String INT32 = "int32"; static final String INT64 = "int64"; static final String FLOAT = "float"; static final String DOUBLE = "double"; static final String BINARY = "binary"; - static final String BYTE = "byte"; + static final String CHAR = "char"; static final String DATE = "date"; static final String DATE_TIME = "date-time"; static final String DURATION = "duration"; diff --git a/core/src/test/java/io/smallrye/openapi/runtime/scanner/StandaloneSchemaScanTest.java b/core/src/test/java/io/smallrye/openapi/runtime/scanner/StandaloneSchemaScanTest.java index 0a3a564a2..181ce431d 100644 --- a/core/src/test/java/io/smallrye/openapi/runtime/scanner/StandaloneSchemaScanTest.java +++ b/core/src/test/java/io/smallrye/openapi/runtime/scanner/StandaloneSchemaScanTest.java @@ -872,4 +872,20 @@ class DTO { assertJsonEquals("components.schemas.example-not-merged.json", scan(config(SmallRyeOASConfig.SMALLRYE_MERGE_SCHEMA_EXAMPLES, "false"), null, new Class[] { DTO.class })); } + + @Test + void testPrimitiveFormats() throws IOException, JSONException { + @Schema(name = "Bean") + @SuppressWarnings("unused") + class Bean { + byte bint8; + short sint16; + int iint32; + long lint64; + char cchar; + byte[] babinary; + } + + assertJsonEquals("components.schemas.primitive-formats.json", Bean.class); + } } diff --git a/core/src/test/resources/io/smallrye/openapi/runtime/scanner/components.schemas.array-type-override.json b/core/src/test/resources/io/smallrye/openapi/runtime/scanner/components.schemas.array-type-override.json index 0d0469191..e1a1a7a3e 100644 --- a/core/src/test/resources/io/smallrye/openapi/runtime/scanner/components.schemas.array-type-override.json +++ b/core/src/test/resources/io/smallrye/openapi/runtime/scanner/components.schemas.array-type-override.json @@ -8,14 +8,14 @@ "arrayFromSchema": { "type": "array", "items": { - "format": "byte", + "format": "char", "type": "string" } }, "arrayFromType": { "type": "array", "items": { - "format": "byte", + "format": "char", "type": "string" } }, diff --git a/core/src/test/resources/io/smallrye/openapi/runtime/scanner/components.schemas.primitive-formats.json b/core/src/test/resources/io/smallrye/openapi/runtime/scanner/components.schemas.primitive-formats.json new file mode 100644 index 000000000..f0440dc50 --- /dev/null +++ b/core/src/test/resources/io/smallrye/openapi/runtime/scanner/components.schemas.primitive-formats.json @@ -0,0 +1,36 @@ +{ + "openapi" : "3.1.0", + "components" : { + "schemas" : { + "Bean" : { + "type" : "object", + "properties" : { + "bint8" : { + "type" : "integer", + "format" : "int8" + }, + "sint16" : { + "type" : "integer", + "format" : "int16" + }, + "iint32" : { + "type" : "integer", + "format" : "int32" + }, + "lint64" : { + "type" : "integer", + "format" : "int64" + }, + "cchar" : { + "type" : "string", + "format" : "char" + }, + "babinary" : { + "type" : "string", + "format" : "binary" + } + } + } + } + } +}