Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse @Schema's defaultValue and enum entries for non-string schemas #1204

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Down Expand Up @@ -218,7 +219,22 @@ public static Schema readSchema(final AnnotationScannerContext context,
}
}

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 @@ -323,21 +339,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