From e84019c1dc5395cea9cdd2dd5a3beaf7b8b9cedc Mon Sep 17 00:00:00 2001 From: David Date: Wed, 19 Feb 2025 18:02:40 +0100 Subject: [PATCH 1/2] add test for unevaluatedProperties reproducing #48 --- .../validation/OpenApiValidatorTest.java | 24 +++++-- .../openapi3-unevaluatedProperties.yaml | 63 +++++++++++++++++++ src/test/resources/json/person_bad_req.json | 9 +++ 3 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 src/test/resources/config/openapi3-unevaluatedProperties.yaml create mode 100644 src/test/resources/json/person_bad_req.json diff --git a/src/test/java/com/mservicetech/openapi/validation/OpenApiValidatorTest.java b/src/test/java/com/mservicetech/openapi/validation/OpenApiValidatorTest.java index 0e937b4..2843e9b 100644 --- a/src/test/java/com/mservicetech/openapi/validation/OpenApiValidatorTest.java +++ b/src/test/java/com/mservicetech/openapi/validation/OpenApiValidatorTest.java @@ -9,7 +9,7 @@ import com.networknt.status.Status; import org.junit.Assert; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; import java.io.BufferedReader; @@ -22,10 +22,10 @@ public class OpenApiValidatorTest { - static OpenApiValidator openApiValidator; + private OpenApiValidator openApiValidator; - @BeforeClass - public static void setUp() { + @Before + public void setUp() { openApiValidator = new OpenApiValidator("openapi.yaml"); } @@ -503,4 +503,20 @@ public void testResponseHeader2() { // {"statusCode":400,"code":"ERR11004","message":"VALIDATOR_SCHEMA","description":"Schema Validation Error - $: string found, integer expected","severity":"ERROR"} } + + @Test + public void testOpenApi3UnevaluatedProperties() { + openApiValidator = new OpenApiValidator("config/openapi3-unevaluatedProperties.yaml"); + InputStream in = this.getClass().getClassLoader().getResourceAsStream("json/person_bad_req.json"); + String req1 = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n")); + + RequestEntity requestEntity = new RequestEntity(); + requestEntity.setRequestBody(req1); + requestEntity.setContentType("application/json"); + Status status = openApiValidator.validateRequestPath("/pets", "post", requestEntity); + Assert.assertNotNull(status); + Assert.assertEquals(status.getDescription(), "Schema Validation Error - $: property 'invalid' is not evaluated and the schema does not allow unevaluated properties"); + } + + } diff --git a/src/test/resources/config/openapi3-unevaluatedProperties.yaml b/src/test/resources/config/openapi3-unevaluatedProperties.yaml new file mode 100644 index 0000000..064d117 --- /dev/null +++ b/src/test/resources/config/openapi3-unevaluatedProperties.yaml @@ -0,0 +1,63 @@ +openapi: 3.1.0 +info: + version: 1.0.0 + title: Swagger Petstore +paths: + /pets: + post: + summary: Create a pet + operationId: createPets + requestBody: + description: Pet to add to the store + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/Person' + responses: + '201': + description: Null response +components: + + schemas: + address: + properties: + residence: + $ref: '#/components/schemas/residence' + description: Residence details where the person lives + city: + type: string + description: City where the person lives. + street: + type: string + description: street where the person lives. + pinCode: + type: number + description: pincode of street + unevaluatedProperties: false + residence: + properties: + flatNumber: + type: string + flatName: + type: string + landmark: + type: string + unevaluatedProperties: false + Person: + properties: + firstName: + type: string + description: The person's first name. + lastName: + type: string + description: The person's last name. + age: + description: Age in years which must be equal to or greater than zero. + type: integer + minimum: 0 + address: + description: Address of the person. + $ref: '#/components/schemas/address' + unevaluatedProperties: false + diff --git a/src/test/resources/json/person_bad_req.json b/src/test/resources/json/person_bad_req.json new file mode 100644 index 0000000..7dfed88 --- /dev/null +++ b/src/test/resources/json/person_bad_req.json @@ -0,0 +1,9 @@ +{ + "firstName": "First Name", + "invalid": 18, + "lastName": "Last Name", + "address": { + "city": "Hyderabad", + "pinCode": 500025 + } +} \ No newline at end of file From a66df6849698dcfa047c7546f6983812a7ec782b Mon Sep 17 00:00:00 2001 From: David Date: Wed, 19 Feb 2025 18:07:50 +0100 Subject: [PATCH 2/2] update light-4j dependencies to 2.1.35 fixes #48 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index af4135b..4c7eb74 100644 --- a/pom.xml +++ b/pom.xml @@ -57,7 +57,7 @@ 11 UTF-8 1.3.3 - 2.1.34 + 2.1.35 2.16.1 2.2 2.0.12