Disabling "validation.request.body.schema.required" when testing that an error returned

Issue #185 resolved
Yosi Karl created an issue

Hi,
I have some tests which just check that an error code (like 400) is returned in case of malformed header.
But - when I add .andExpect(openApi().isValid(SWAGGER_JSON_FILE)) to those tests - They fails, for instance:

com.atlassian.oai.validator.mockmvc.OpenApiMatchers$OpenApiValidationException: {
  "messages" : [ {
    "key" : "validation.request.body.schema.required",
    "level" : "ERROR",
    "message" : "Object has missing required properties ([\"client_id\"])",
    "context" : {
      "requestPath" : "/client",
      "apiRequestContentType" : "application/json",
      "location" : "REQUEST",
      "requestMethod" : "POST"
    }
  } ]
}

I understand there is mismatching to the documented header, but I want to test that the correct error code is returned.
How can I disable header checks in those tests?
If there is no way to do so - I think it will be great to add this..
I'm using swagger-request-validator-mockmvc.

Comments (3)

  1. Yosi Karl reporter

    Ok, I saw in swagger-request-validator-core the option to define a white-list. How can I set that in swagger-request-validator-mockmvc?

  2. Yosi Karl reporter

    OK, solved it:

        @BeforeClass
        public static void setup() {
            ValidationErrorsWhitelist whitelist = ValidationErrorsWhitelist.create()
                    .withRule(
                            "Ignore missing header in bad requests",
                            allOf(
                                    messageHasKey("validation.request.parameter.header.missing"),
                                    responseStatusIs(400)
                            )
                    );
            validator = OpenApiInteractionValidator.createFor(SWAGGER_JSON_FILE)
                    .withWhitelist(whitelist)
                    .build();
        }
    
        @Test
        public void testBadRequest() throws Exception {
            mockMvc.perform(post("/register")
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(registerRequestHeaderInvalid))
                    .andExpect(status().isBadRequest())
                    .andExpect(openApi().isValid(validator))
                    .andExpect(jsonPath("$").doesNotExist());
        }
    

    I think it would be useful to add info about whitelist in swagger-request-validator-mockmvc readme as well..

  3. James Navin

    Updated the docs to highlight the ability to supply a pre-configured validator. Let me know if they need more clarification.

    Cheers.

  4. Log in to comment