Search in sources :

Example 1 with EndpointTransferMapper

use of dev.hilla.endpointransfermapper.EndpointTransferMapper 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

Endpoint (dev.hilla.Endpoint)1 EndpointTransferMapper (dev.hilla.endpointransfermapper.EndpointTransferMapper)1 Operation (io.swagger.v3.oas.models.Operation)1 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)1 BooleanSchema (io.swagger.v3.oas.models.media.BooleanSchema)1 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)1 DateSchema (io.swagger.v3.oas.models.media.DateSchema)1 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)1 MapSchema (io.swagger.v3.oas.models.media.MapSchema)1 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)1 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)1 Schema (io.swagger.v3.oas.models.media.Schema)1 StringSchema (io.swagger.v3.oas.models.media.StringSchema)1 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)1 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)1 GenericArrayType (java.lang.reflect.GenericArrayType)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1