Skip to content

Commit

Permalink
fix: use more accurate formats for byte, short, and char types (#2128)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar authored Jan 6, 2025
1 parent 0409fed commit 5b2a911
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
"arrayFromSchema": {
"type": "array",
"items": {
"format": "byte",
"format": "char",
"type": "string"
}
},
"arrayFromType": {
"type": "array",
"items": {
"format": "byte",
"format": "char",
"type": "string"
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}

0 comments on commit 5b2a911

Please sign in to comment.