citrusframework / citrus

Framework for automated integration tests with focus on messaging integration
https://citrusframework.org
Apache License 2.0
459 stars 136 forks source link

OpenApiClientAction should not require optional fields #1190

Open novarx opened 3 months ago

novarx commented 3 months ago

Citrus Version 4.2.1

Expected behavior Given an openapi endpoint And the response of the endpoint has the required fields: "category", "name", "status" And the response of the endpoint has the optional fields: "id", "photoUrls", "tags"

When a valid request is executed And the following json body is returned: { "category": {}, "name": "", "status": "sold" }

Then the test should pass green

Actual behavior The Test always fails with Number of entries is not equal in element: '$', expected '[photoUrls, name, id, category, tags, status]' but was '[name, category, status]'

Test case sample

@Test
public class OpenApiClientIT extends TestNGCitrusSpringSupport {

    private final int port = SocketUtils.findAvailableTcpPort(8080);

    @BindToRegistry
    private final HttpServer httpServer = new HttpServerBuilder()
            .port(port)
            .timeout(5000L)
            .autoStart(true)
            .defaultStatus(HttpStatus.NO_CONTENT)
            .build();

    @BindToRegistry
    private final HttpClient httpClient = new HttpClientBuilder()
            .requestUrl("http://localhost:%d".formatted(port))
            .build();

    private final OpenApiSpecification petstoreSpec = OpenApiSpecification.from(
            Resources.create("classpath:org/citrusframework/openapi/petstore/petstore-v3.json"));

    @CitrusTest
    public void BUG_should_only_validate_the_presence_of_required_properties() {
        variable("petId", "1001");

        when(openapi(petstoreSpec)
                .client(httpClient)
                .send("getPetById")
                .message()
                .accept("application/json")
                .fork(true));

        then(http().server(httpServer)
                .receive()
                .get("/pet/${petId}")
                .message()
                .accept("@contains('application/json')@"));

        then(http().server(httpServer)
                .send()
                .response(HttpStatus.OK)
                .message()
                // TODO BUG this should be valid, according to the spec-file
                .body("""
                        {
                        "category": {},
                        "name": "",
                        "status": "sold"
                        }
                        """)
                .contentType("application/json"));

        then(openapi(petstoreSpec)
                .client(httpClient)
                .receive("getPetById", HttpStatus.OK)
                .message()
        );
    }
}
novarx commented 3 months ago

The validation expressions are built in org.citrusframework.openapi.OpenApiTestDataGenerator#createValidationExpression(...)