Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added edge cases for OpenAPI validation #688

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ protected void validateBodyAccordingToMediaType(ValidationContext ctx, String me
// Use the value of the OpenAPI spec for comparison, so it can not
// be influenced from the outside.
if ( APPLICATION_JSON_CONTENT_TYPE.match(mediaType)) {
// MediaType but no schema. => No schema therefore no schema validation => ok
if (mediaTypeObj.getSchema() == null) {
return;
}
if (mediaTypeObj.getSchema().get$ref() != null) {
ctx.schemaType(mediaTypeObj.getSchema().get$ref());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2023 predic8 GmbH, www.predic8.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.predic8.membrane.core.openapi.validators;

import com.predic8.membrane.core.openapi.model.*;
import org.junit.jupiter.api.*;

import static org.junit.jupiter.api.Assertions.*;


public class EdgeCasesTest extends AbstractValidatorTest {

@Override
String getOpenAPIFileName() {
return "/openapi/specs/edge-cases.yml";
}


@Test
void noContent() {
ValidationErrors errors = validator.validateResponse(Request.get().path("/no-content"), Response.statusCode(200).json().body("{}"));
System.out.println("errors = " + errors);
assertEquals(1,errors.size());
ValidationError error = errors.get(0);
assertTrue(error.getMessage().contains("Response shouldn't have a body"));
}

@Test
void contentNoSchema() {
ValidationErrors errors = validator.validateResponse(Request.get().path("/content-no-schema"), Response.statusCode(200).json().body("{}"));
System.out.println("errors = " + errors);
assertEquals(0,errors.size());
}

@Test
void contentMediatypeNoSchema(){
ValidationErrors errors = validator.validateResponse(Request.get().path("/content-mediatype-no-schema"), Response.statusCode(200).json().body("{}"));
System.out.println("errors = " + errors);
assertEquals(0,errors.size());
}

@Test
void contentEmptySchema(){
ValidationErrors errors = validator.validateResponse(Request.get().path("/content-empty-schema"), Response.statusCode(200).json().body("{}"));
System.out.println("errors = " + errors);
assertEquals(0,errors.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ String getOpenAPIFileName() {
}

@Test
public void validCustomerResponse() throws ParseException {
public void validCustomerResponse() {

ValidationErrors errors = validator.validateResponse(Request.put().path("/customers"), Response.statusCode(200).mediaType(APPLICATION_JSON).body(getResourceAsStream("/openapi/messages/customer.json")));
ValidationErrors errors = validator.validateResponse(Request.put().path("/customers"), Response.statusCode(200).json().body(getResourceAsStream("/openapi/messages/customer.json")));
// System.out.println("errors = " + errors);
assertEquals(0,errors.size());
}

@Test
public void invalidCustomerResponse() throws ParseException {
public void invalidCustomerResponse() {

ValidationErrors errors = validator.validateResponse(Request.put().path("/customers"), Response.statusCode(200).mediaType(APPLICATION_JSON).body(getResourceAsStream("/openapi/messages/invalid-customer.json")));
ValidationErrors errors = validator.validateResponse(Request.put().path("/customers"), Response.statusCode(200).json().body(getResourceAsStream("/openapi/messages/invalid-customer.json")));

// System.out.println("errors = " + errors);

Expand Down Expand Up @@ -93,9 +93,9 @@ public void wrongMediaTypeResponse() throws ParseException {
* The OpenAPI does not specify a content for a response, but der backend is sending a body.
*/
@Test
public void noContentInResponseSendPayload() throws ParseException {
public void noContentInResponseSendPayload() {

ValidationErrors errors = validator.validateResponse(Request.post().path("/customers").mediaType(APPLICATION_JSON).body(getResourceAsStream("/openapi/messages/customer.json")), Response.statusCode(200).mediaType(APPLICATION_JSON).body("{ }"));
ValidationErrors errors = validator.validateResponse(Request.post().path("/customers").json().body(getResourceAsStream("/openapi/messages/customer.json")), Response.statusCode(200).json().body("{ }"));
// System.out.println("errors = " + errors);
assertEquals(1,errors.size());
ValidationError e = errors.get(0);
Expand All @@ -108,7 +108,7 @@ public void noContentInResponseSendPayload() throws ParseException {
@Test
public void statusCodeNotInResponse() throws ParseException {

ValidationErrors errors = validator.validateResponse(Request.post().path("/customers").mediaType(APPLICATION_JSON).body(getResourceAsStream("/openapi/messages/customer.json")), Response.statusCode(202).mediaType(APPLICATION_JSON).body("{ }"));
ValidationErrors errors = validator.validateResponse(Request.post().path("/customers").json().body(getResourceAsStream("/openapi/messages/customer.json")), Response.statusCode(202).mediaType(APPLICATION_JSON).body("{ }"));
// System.out.println("errors = " + errors);
assertEquals(1,errors.size());
ValidationError e = errors.get(0);
Expand Down
32 changes: 32 additions & 0 deletions core/src/test/resources/openapi/specs/edge-cases.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
openapi: '3.0.2'
info:
title: Edge Cases Test API
version: '1.0'
paths:
/no-content:
get:
responses:
'200':
description: OK
/content-no-schema:
get:
responses:
'200':
description: OK
content: {}
/content-mediatype-no-schema:
get:
responses:
'200':
description: OK
content:
application/json: {}
/content-empty-schema:
get:
responses:
'200':
description: OK
content:
application/json:
schema: {}