Search in sources :

Example 71 with PathItem

use of io.swagger.v3.oas.models.PathItem in project flow by vaadin.

the class AbstractEndpointGenerationTest method assertPath.

private void assertPath(Class<?> testEndpointClass, Method expectedEndpointMethod, PathItem actualPath, HashMap<String, Class<?>> typeArguments) {
    Operation actualOperation = actualPath.getPost();
    assertEquals("Unexpected tag in the OpenAPI spec", Collections.singletonList(testEndpointClass.getSimpleName()), actualOperation.getTags());
    assertTrue(String.format("Unexpected OpenAPI operation id: does not contain the endpoint name of the class '%s'", testEndpointClass.getSimpleName()), actualOperation.getOperationId().contains(getEndpointName(testEndpointClass)));
    assertTrue(String.format("Unexpected OpenAPI operation id: does not contain the name of the endpoint method '%s'", expectedEndpointMethod.getName()), actualOperation.getOperationId().contains(expectedEndpointMethod.getName()));
    if (expectedEndpointMethod.getParameterCount() > 0) {
        Schema requestSchema = extractSchema(actualOperation.getRequestBody().getContent());
        Type[] genericParameterTypes = expectedEndpointMethod.getGenericParameterTypes();
        Class<?>[] parameterTypes = new Class<?>[genericParameterTypes.length];
        List<HashMap<String, Class<?>>> parameterTypeArguments = new ArrayList<>();
        for (int i = 0; i < genericParameterTypes.length; i++) {
            parameterTypes[i] = applyTypeArguments(genericParameterTypes[i], typeArguments);
            parameterTypeArguments.add(i, extractTypeArguments(genericParameterTypes[i], typeArguments));
        }
        assertRequestSchema(requestSchema, parameterTypes, parameterTypeArguments, expectedEndpointMethod.getParameters());
    } else {
        assertNull(String.format("No request body should be present in path schema for endpoint method with no parameters, method: '%s'", expectedEndpointMethod), actualOperation.getRequestBody());
    }
    ApiResponses responses = actualOperation.getResponses();
    assertEquals("Every operation is expected to have a single '200' response", 1, responses.size());
    ApiResponse apiResponse = responses.get("200");
    assertNotNull("Every operation is expected to have a single '200' response", apiResponse);
    Type genericReturnType = expectedEndpointMethod.getGenericReturnType();
    Class<?> returnType = applyTypeArguments(genericReturnType, typeArguments);
    Class<?> mappedType = new EndpointTransferMapper().getTransferType(returnType);
    if (mappedType != null) {
        returnType = mappedType;
    }
    if (returnType != void.class) {
        assertSchema(extractSchema(apiResponse.getContent()), returnType, extractTypeArguments(genericReturnType, typeArguments));
    } else {
        assertNull(String.format("No response is expected to be present for void method '%s'", expectedEndpointMethod), apiResponse.getContent());
    }
    assertNotNull("Non-anonymous endpoint method should have a security data defined for it in the schema", actualOperation.getSecurity());
}
Also used : HashMap(java.util.HashMap) BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) DateSchema(io.swagger.v3.oas.models.media.DateSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) Endpoint(dev.hilla.Endpoint) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) EndpointTransferMapper(dev.hilla.endpointransfermapper.EndpointTransferMapper) GenericArrayType(java.lang.reflect.GenericArrayType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses)

Aggregations

PathItem (io.swagger.v3.oas.models.PathItem)66 Operation (io.swagger.v3.oas.models.Operation)52 OpenAPI (io.swagger.v3.oas.models.OpenAPI)50 Test (org.testng.annotations.Test)39 Paths (io.swagger.v3.oas.models.Paths)24 Map (java.util.Map)19 HashMap (java.util.HashMap)18 Schema (io.swagger.v3.oas.models.media.Schema)16 Components (io.swagger.v3.oas.models.Components)15 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)14 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)14 LinkedHashMap (java.util.LinkedHashMap)14 ArrayList (java.util.ArrayList)11 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)10 Parameter (io.swagger.v3.oas.models.parameters.Parameter)10 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)8 MediaType (io.swagger.v3.oas.models.media.MediaType)8 HttpMethod (io.swagger.models.HttpMethod)7 Callback (io.swagger.v3.oas.models.callbacks.Callback)7 Content (io.swagger.v3.oas.models.media.Content)7