mservicetech / openapi-schema-validation

Apache License 2.0
15 stars 9 forks source link

fix #27 is not working #29

Closed vijayseid closed 1 year ago

vijayseid commented 1 year ago

@GavinChenYan @stevehu

Even though if pass the request body as empty or null, still it's not validating properly, I saw the change & made the same change on custom lib as well.

Passing as EMPTY :

image

Passing as NULL:

image

Can you please confirm you fix?

Originally posted by @vijayseid in https://github.com/mservicetech/openapi-schema-validation/issues/27#issuecomment-1587034064

GavinChenYan commented 1 year ago

@vijayseid I tried with API, it works fine for me. Can you please refer the test cases for your method call?:

@Test
public void testRequestBodyEmpty() {
    String req = "";
    RequestEntity requestEntity = new RequestEntity();
    requestEntity.setRequestBody(req);
    requestEntity.setContentType("application/json");
    Status status = openApiValidator.validateRequestPath("/pets", "post", requestEntity);
    Assert.assertNotNull(status);
    Assert.assertEquals( status.getCode(), "ERR11013");
    System.out.println(status.getDescription());
    //{"statusCode":400,"code":"ERR11013","message":"VALIDATOR_REQUEST_BODY_UNEXPECTED","No request body is expected for %s on path %s.":"ERROR"}
}

@Test
public void testRequestBodyNull() {
    String req = null;
    RequestEntity requestEntity = new RequestEntity();
    requestEntity.setRequestBody(req);
    requestEntity.setContentType("application/json");
    Status status = openApiValidator.validateRequestPath("/pets", "post", requestEntity);
    Assert.assertNotNull(status);
    Assert.assertEquals( status.getCode(), "ERR11014");
    //{"statusCode":400,"code":"ERR11004","message":"VALIDATOR_REQUEST_BODY_MISSING","description":"Method post on path /pets requires a request body. None found.","severity":"ERROR"}
}
vijayseid commented 1 year ago

@GavinChenYan
Test case : testRequestBodyEmpty()

if you pass request body as "" empty, it should throw as required fields right which we missed, but it says No request body is expected for %s on path %s.":"ERROR". But our case need an request body with required field.

Test Case : testRequestBodyNull()

I have tested by passing null, but it passed without any error

image

GavinChenYan commented 1 year ago

@vijayseid Sorry for the delay. I will take a look the status code for the error.

For second senario, can you take a look your code implementation? Do you set the request body ? requestEntity.setRequestBody(req);

vijayseid commented 1 year ago

Yeah @GavinChenYan I'm setting the request body

schemaValidateRequest.setRequestBody(requestBody);

GavinChenYan commented 1 year ago

That's weird, my case works fine

GavinChenYan commented 1 year ago

@vijayseid there is logic in the code: if ((requestEntity.getContentType()==null || requestEntity.getContentType().startsWith("application/json"))) {

what content type you are using? can you debug to find it? only in case it is null or application/json , it trigger body validation

vijayseid commented 1 year ago

@GavinChenYan Confirmed, content type is application/json & my request body is null. Still it is not throwing the error for me

requestEntity.setContentType(MediaType.APPLICATION_JSON_VALUE);

Request body is null - >

image

Status is null ->

image

if I pass the body, it will say expected key is required & it is missing

vijayseid commented 1 year ago

Also @GavinChenYan when new release will happen? any approx date?

GavinChenYan commented 1 year ago

version 2.0.8 released:

Uploaded to ossrh: https://oss.sonatype.org:443/service/local/staging/deployByRepositoryId/commservicetech-1013/com/mservicetech/openapi-schema-validation/2.0.8/openapi-schema-validation-2.0.8.jar.asc.asc (673 B at 7.9 kB/s)