Skip to content

Commit

Permalink
Parse @Schema's defaultValue and enum entries for non-string schemas
Browse files Browse the repository at this point in the history
Fixes smallrye#1174

Signed-off-by: Michael Edgar <michael@xlate.io>
  • Loading branch information
MikeEdgar committed Aug 8, 2022
1 parent a09663a commit c9a4d9e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ public static Schema readSchema(final AnnotationScannerContext context,
schema.setDeprecated(readAttr(annotation, SchemaConstant.PROP_DEPRECATED, defaults));
schema.setType(readSchemaType(annotation, schema, defaults));
schema.setExample(parseSchemaAttr(context, annotation, SchemaConstant.PROP_EXAMPLE, defaults, schema.getType()));
schema.setDefaultValue(readAttr(annotation, SchemaConstant.PROP_DEFAULT_VALUE, defaults));
schema.setDefaultValue(
parseSchemaAttr(context, annotation, SchemaConstant.PROP_DEFAULT_VALUE, defaults, schema.getType()));
schema.setDiscriminator(
readDiscriminator(context,
JandexUtil.value(annotation, SchemaConstant.PROP_DISCRIMINATOR_PROPERTY),
Expand All @@ -206,7 +207,22 @@ public static Schema readSchema(final AnnotationScannerContext context,
return propertySchemas;
}, defaults));

List<Object> enumeration = readAttr(annotation, SchemaConstant.PROP_ENUMERATION, defaults);
final Schema.SchemaType type = schema.getType();

List<Object> enumeration = readAttr(annotation, SchemaConstant.PROP_ENUMERATION, (Object[] values) -> {
List<Object> parsed = new ArrayList<>(values.length);

if (type == Schema.SchemaType.STRING) {
parsed.addAll(Arrays.asList(values));
} else {
Arrays.stream(values)
.map(String.class::cast)
.map(v -> parseValue(context, v, type))
.forEach(parsed::add);
}

return parsed;
}, defaults);

if (enumeration != null && !enumeration.isEmpty()) {
schema.setEnumeration(enumeration);
Expand Down Expand Up @@ -311,21 +327,24 @@ static <T> T readAttr(AnnotationInstance annotation, String propertyName, T defa
static Object parseSchemaAttr(AnnotationScannerContext context, AnnotationInstance annotation, String propertyName,
Map<String, Object> defaults, SchemaType schemaType) {
return readAttr(annotation, propertyName, value -> {
if (!(value instanceof String)) {
return value;
if (value instanceof String) {
return parseValue(context, (String) value, schemaType);
}
String stringValue = ((String) value);
if (schemaType != SchemaType.STRING) {
Object parsedValue;
for (AnnotationScannerExtension e : context.getExtensions()) {
parsedValue = e.parseValue(stringValue);
if (parsedValue != null) {
return parsedValue;
}
return value;
}, defaults);
}

static Object parseValue(AnnotationScannerContext context, String stringValue, SchemaType schemaType) {
if (schemaType != SchemaType.STRING) {
Object parsedValue;
for (AnnotationScannerExtension e : context.getExtensions()) {
parsedValue = e.parseValue(stringValue);
if (parsedValue != null) {
return parsedValue;
}
}
return stringValue;
}, defaults);
}
return stringValue;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ public static class PagedResponse<T> {
@Parameter(name = "filter:op[description]", in = ParameterIn.QUERY, description = "Operations used with the filter", schema = @Schema(type = SchemaType.STRING, enumeration = {
"equal", "like", "ilike", "not_equal" }, defaultValue = "equal")),
@Parameter(name = "filter[is_enabled]", in = ParameterIn.QUERY, description = "Filtering policies by the is_enabled field."
+ "Defaults to true if no operand is given.", schema = @Schema(type = SchemaType.STRING, defaultValue = "true", enumeration = {
"true", "false" })) })
+ "Defaults to true if no operand is given.", schema = @Schema(type = SchemaType.BOOLEAN, defaultValue = "true", enumeration = {
"true", "false" })),
@Parameter(name = "X-Session-Info", in = ParameterIn.COOKIE, description = "Data about the session", schema = @Schema(type = SchemaType.OBJECT, enumeration = {
"{ \"status\": \"active\" }",
"{ \"status\": \"expired\" }"
})),
})
@APIResponse(responseCode = "400", description = "Bad parameter for sorting was passed")
@APIResponse(responseCode = "404", description = "No policies found for customer")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ public static class PagedResponse<T> {
@Parameter(name = "filter:op[description]", in = ParameterIn.QUERY, description = "Operations used with the filter", schema = @Schema(type = SchemaType.STRING, enumeration = {
"equal", "like", "ilike", "not_equal" }, defaultValue = "equal")),
@Parameter(name = "filter[is_enabled]", in = ParameterIn.QUERY, description = "Filtering policies by the is_enabled field."
+ "Defaults to true if no operand is given.", schema = @Schema(type = SchemaType.STRING, defaultValue = "true", enumeration = {
"true", "false" })) })
+ "Defaults to true if no operand is given.", schema = @Schema(type = SchemaType.BOOLEAN, defaultValue = "true", enumeration = {
"true", "false" })),
@Parameter(name = "X-Session-Info", in = ParameterIn.COOKIE, description = "Data about the session", schema = @Schema(type = SchemaType.OBJECT, enumeration = {
"{ \"status\": \"active\" }",
"{ \"status\": \"expired\" }"
})),
})
@APIResponse(responseCode = "400", description = "Bad parameter for sorting was passed")
@APIResponse(responseCode = "404", description = "No policies found for customer")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
"in": "query",
"description": "Filtering policies by the is_enabled field.Defaults to true if no operand is given.",
"schema": {
"default": "true",
"default": true,
"enum": [
"true",
"false"
true,
false
],
"type": "string"
"type": "boolean"
}
},
{
Expand Down Expand Up @@ -105,6 +105,22 @@
],
"type": "string"
}
},
{
"name": "X-Session-Info",
"in": "cookie",
"description": "Data about the session",
"schema": {
"enum": [
{
"status": "active"
},
{
"status": "expired"
}
],
"type": "object"
}
}
],
"responses": {
Expand Down Expand Up @@ -145,10 +161,11 @@
"PagedResponse": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "object"
"meta": {
"type": "object",
"additionalProperties": {
"format": "int64",
"type": "integer"
}
},
"links": {
Expand All @@ -157,11 +174,10 @@
"type": "string"
}
},
"meta": {
"type": "object",
"additionalProperties": {
"format": "int64",
"type": "integer"
"data": {
"type": "array",
"items": {
"type": "object"
}
}
}
Expand Down

0 comments on commit c9a4d9e

Please sign in to comment.