Skip to content

Commit

Permalink
Update SmallRye OpenAPI and MicroProfile OpenAPI to 4.0
Browse files Browse the repository at this point in the history
- OAS 3.1, normalize `type` to arrays, do not test order of health keys
- Fix health schema type tests
- Update Spring Web test for OAS 3.1

Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Oct 26, 2024
1 parent fcc7889 commit 03951ed
Show file tree
Hide file tree
Showing 18 changed files with 226 additions and 291 deletions.
4 changes: 2 additions & 2 deletions bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
<microprofile-rest-client.version>4.0</microprofile-rest-client.version>
<microprofile-jwt.version>2.1</microprofile-jwt.version>
<microprofile-lra.version>2.0</microprofile-lra.version>
<microprofile-openapi.version>3.1.2</microprofile-openapi.version>
<microprofile-openapi.version>4.0.2</microprofile-openapi.version>
<smallrye-common.version>2.8.0</smallrye-common.version>
<smallrye-config.version>3.10.0</smallrye-config.version>
<smallrye-health.version>4.1.0</smallrye-health.version>
<smallrye-metrics.version>4.0.0</smallrye-metrics.version>
<smallrye-open-api.version>3.13.0</smallrye-open-api.version>
<smallrye-open-api.version>4.0.0</smallrye-open-api.version>
<smallrye-graphql.version>2.11.0</smallrye-graphql.version>
<smallrye-fault-tolerance.version>6.6.0</smallrye-fault-tolerance.version>
<smallrye-jwt.version>4.6.0</smallrye-jwt.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import java.util.Map;

import org.eclipse.microprofile.openapi.OASFactory;
import org.eclipse.microprofile.openapi.OASFilter;
import org.eclipse.microprofile.openapi.models.OpenAPI;
import org.eclipse.microprofile.openapi.models.Operation;
Expand All @@ -13,16 +14,6 @@
import org.eclipse.microprofile.openapi.models.media.Schema;
import org.eclipse.microprofile.openapi.models.responses.APIResponses;

import io.smallrye.openapi.api.models.ComponentsImpl;
import io.smallrye.openapi.api.models.OperationImpl;
import io.smallrye.openapi.api.models.PathItemImpl;
import io.smallrye.openapi.api.models.PathsImpl;
import io.smallrye.openapi.api.models.media.ContentImpl;
import io.smallrye.openapi.api.models.media.MediaTypeImpl;
import io.smallrye.openapi.api.models.media.SchemaImpl;
import io.smallrye.openapi.api.models.responses.APIResponseImpl;
import io.smallrye.openapi.api.models.responses.APIResponsesImpl;

/**
* Create OpenAPI entries (if configured)
*/
Expand All @@ -32,37 +23,37 @@ public class HealthOpenAPIFilter implements OASFilter {
private static final String HEALTH_RESPONSE_SCHEMA_NAME = "HealthResponse";
private static final String HEALTH_CHECK_SCHEMA_NAME = "HealthCheck";

private static final Schema healthResponseSchemaDefinition = new SchemaImpl(HEALTH_RESPONSE_SCHEMA_NAME)
.type(Schema.SchemaType.OBJECT)
private static final Schema healthResponseSchemaDefinition = OASFactory.createSchema()
.type(Collections.singletonList(Schema.SchemaType.OBJECT))
.properties(Map.ofEntries(

Map.entry("status",
new SchemaImpl()
.type(Schema.SchemaType.STRING)
OASFactory.createSchema()
.type(Collections.singletonList(Schema.SchemaType.STRING))
.enumeration(List.of("UP", "DOWN"))),

Map.entry("checks",
new SchemaImpl()
.type(Schema.SchemaType.ARRAY)
.items(new SchemaImpl().ref("#/components/schemas/" + HEALTH_CHECK_SCHEMA_NAME)))));
OASFactory.createSchema()
.type(Collections.singletonList(Schema.SchemaType.ARRAY))
.items(OASFactory.createSchema()
.ref("#/components/schemas/" + HEALTH_CHECK_SCHEMA_NAME)))));

private static final Schema healthCheckSchemaDefinition = new SchemaImpl(HEALTH_CHECK_SCHEMA_NAME)
.type(Schema.SchemaType.OBJECT)
private static final Schema healthCheckSchemaDefinition = OASFactory.createSchema()
.type(Collections.singletonList(Schema.SchemaType.OBJECT))
.properties(Map.ofEntries(

Map.entry("name",
new SchemaImpl()
.type(Schema.SchemaType.STRING)),
OASFactory.createSchema()
.type(Collections.singletonList(Schema.SchemaType.STRING))),

Map.entry("status",
new SchemaImpl()
.type(Schema.SchemaType.STRING)
OASFactory.createSchema()
.type(Collections.singletonList(Schema.SchemaType.STRING))
.enumeration(List.of("UP", "DOWN"))),

Map.entry("data",
new SchemaImpl()
.type(Schema.SchemaType.OBJECT)
.nullable(Boolean.TRUE))));
OASFactory.createSchema()
.type(List.of(Schema.SchemaType.OBJECT, Schema.SchemaType.NULL)))));

private final String rootPath;
private final String livenessPath;
Expand All @@ -79,13 +70,14 @@ public HealthOpenAPIFilter(String rootPath, String livenessPath, String readines
@Override
public void filterOpenAPI(OpenAPI openAPI) {
if (openAPI.getComponents() == null) {
openAPI.setComponents(new ComponentsImpl());
openAPI.setComponents(OASFactory.createComponents());
}

openAPI.getComponents().addSchema(HEALTH_RESPONSE_SCHEMA_NAME, healthResponseSchemaDefinition);
openAPI.getComponents().addSchema(HEALTH_CHECK_SCHEMA_NAME, healthCheckSchemaDefinition);

if (openAPI.getPaths() == null) {
openAPI.setPaths(new PathsImpl());
openAPI.setPaths(OASFactory.createPaths());
}

final Paths paths = openAPI.getPaths();
Expand Down Expand Up @@ -150,31 +142,31 @@ private PathItem createHealthEndpoint(
String operationDescription,
String operationId,
String operationSummary) {
final Content content = new ContentImpl()
final Content content = OASFactory.createContent()
.addMediaType(
"application/json",
new MediaTypeImpl()
.schema(new SchemaImpl().ref("#/components/schemas/" + HEALTH_RESPONSE_SCHEMA_NAME)));
OASFactory.createMediaType()
.schema(OASFactory.createSchema().ref("#/components/schemas/" + HEALTH_RESPONSE_SCHEMA_NAME)));

final APIResponses responses = new APIResponsesImpl()
final APIResponses responses = OASFactory.createAPIResponses()
.addAPIResponse(
"200",
new APIResponseImpl().description("OK").content(content))
OASFactory.createAPIResponse().description("OK").content(content))
.addAPIResponse(
"503",
new APIResponseImpl().description("Service Unavailable").content(content))
OASFactory.createAPIResponse().description("Service Unavailable").content(content))
.addAPIResponse(
"500",
new APIResponseImpl().description("Internal Server Error").content(content));
OASFactory.createAPIResponse().description("Internal Server Error").content(content));

final Operation getOperation = new OperationImpl()
final Operation getOperation = OASFactory.createOperation()
.operationId(operationId)
.description(operationDescription)
.tags(MICROPROFILE_HEALTH_TAG)
.summary(operationSummary)
.responses(responses);

return new PathItemImpl()
return OASFactory.createPathItem()
.description(endpointDescription)
.summary(endpointSummary)
.GET(getOperation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ void testOpenApiPathAccessResource() {
.body("components.schemas.HealthCheck.type", Matchers.equalTo("object"))
.body("components.schemas.HealthCheck.properties.status.type", Matchers.equalTo("string"))
.body("components.schemas.HealthCheck.properties.name.type", Matchers.equalTo("string"))
.body("components.schemas.HealthCheck.properties.data.type", Matchers.equalTo("object"))
.body("components.schemas.HealthCheck.properties.data.nullable", Matchers.is(true));

.body("components.schemas.HealthCheck.properties.data.type", Matchers.contains("object", "null"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void testOpenApiPathAccessResource() {
.when().get(OPEN_API_PATH)
.then()
.header("Content-Type", "application/json;charset=UTF-8")

.body("paths", Matchers.hasKey("/q/health/ready"))
.body("paths", Matchers.hasKey("/q/health/live"))
.body("paths", Matchers.hasKey("/q/health/started"))
Expand All @@ -40,9 +41,7 @@ void testOpenApiPathAccessResource() {
.body("components.schemas.HealthCheck.type", Matchers.equalTo("object"))
.body("components.schemas.HealthCheck.properties.status.type", Matchers.equalTo("string"))
.body("components.schemas.HealthCheck.properties.name.type", Matchers.equalTo("string"))
.body("components.schemas.HealthCheck.properties.data.type", Matchers.equalTo("object"))
.body("components.schemas.HealthCheck.properties.data.nullable", Matchers.is(true));

.body("components.schemas.HealthCheck.properties.data.type", Matchers.contains("object", "null"));
}

}
Loading

0 comments on commit 03951ed

Please sign in to comment.