From 09d776660de26956c5245a794d86296ab68ec2af Mon Sep 17 00:00:00 2001 From: Ben Yarger Date: Tue, 9 Sep 2025 15:19:19 -0700 Subject: [PATCH] Fix for nullable transformers on nullable array item object properties. --- .../jsonschema/EbaySchemaTransformer.java | 14 +++--- .../jsonschema/OpenApiToJsonSchema.java | 4 +- .../OpenApiSchemaValidatorTest.java | 24 +++++++++ .../json/validResponseNullableProperties.json | 9 ++++ ...dResponseNullablePropertiesWithValues.json | 9 ++++ .../schema/nullableProperties.yaml | 49 +++++++++++++++++++ NSTTutorials/pom.xml | 2 +- pom.xml | 2 +- 8 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 NST/src/test/resources/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/json/validResponseNullableProperties.json create mode 100644 NST/src/test/resources/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/json/validResponseNullablePropertiesWithValues.json create mode 100644 NST/src/test/resources/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/schema/nullableProperties.yaml diff --git a/NST/src/main/java/com/ebay/openapi/export/jsonschema/EbaySchemaTransformer.java b/NST/src/main/java/com/ebay/openapi/export/jsonschema/EbaySchemaTransformer.java index e97e1d7..1097743 100644 --- a/NST/src/main/java/com/ebay/openapi/export/jsonschema/EbaySchemaTransformer.java +++ b/NST/src/main/java/com/ebay/openapi/export/jsonschema/EbaySchemaTransformer.java @@ -78,15 +78,15 @@ public abstract class EbaySchemaTransformer { static void applyToChildSchemas(final JsonNode schemaObject, final Consumer consumer) { if (isArrayDefinition(schemaObject)) { consumer.accept(itemsDefinition(schemaObject)); - return; + } else { + properties(schemaObject).forEachRemaining(consumer); + allOf(schemaObject).forEachRemaining(consumer); + anyOf(schemaObject).forEachRemaining(consumer); + oneOf(schemaObject).forEachRemaining(consumer); + schemaComponents(schemaObject).forEachRemaining(consumer); + additionalProperties(schemaObject).forEachRemaining(consumer); } - properties(schemaObject).forEachRemaining(consumer); - allOf(schemaObject).forEachRemaining(consumer); - anyOf(schemaObject).forEachRemaining(consumer); - oneOf(schemaObject).forEachRemaining(consumer); - schemaComponents(schemaObject).forEachRemaining(consumer); definitions(schemaObject).forEachRemaining(consumer); - additionalProperties(schemaObject).forEachRemaining(consumer); } protected static boolean hasAllOfField(final JsonNode n) { diff --git a/NST/src/main/java/com/ebay/openapi/export/jsonschema/OpenApiToJsonSchema.java b/NST/src/main/java/com/ebay/openapi/export/jsonschema/OpenApiToJsonSchema.java index bd27e1e..5f82357 100644 --- a/NST/src/main/java/com/ebay/openapi/export/jsonschema/OpenApiToJsonSchema.java +++ b/NST/src/main/java/com/ebay/openapi/export/jsonschema/OpenApiToJsonSchema.java @@ -188,8 +188,8 @@ public void convert() { throw new IllegalStateException(String.format("Status code [%s] is not present for request method [%s] and path [%s] in file: %s", statusCode, requestMethod, requestPath, openApiSpecFilePath)); } - //added below check to fix NPE - if(response.getContent() != null) { + //added below check to fix NPE + if(response.getContent() != null) { MediaType mediaType = response.getContent().get(MEDIA_TYPE); if (mediaType == null) { throw new IllegalStateException( diff --git a/NST/src/test/java/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest.java b/NST/src/test/java/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest.java index aa5e60b..e6326a2 100644 --- a/NST/src/test/java/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest.java +++ b/NST/src/test/java/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest.java @@ -109,4 +109,28 @@ public void schemaValidatePassNullableArrayItem() throws Exception { validator.validate(testResponsePayload); } + + @Test + public void schemaValidateNullableProperties() throws Exception { + + OpenApiSchemaValidator validator = new OpenApiSchemaValidator.Builder("/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/schema/nullableProperties.yaml", "/test", NstRequestType.GET) + .allowAdditionalProperties(OpenApiSchemaValidator.AllowAdditionalProperties.NO) + .build(); + + String testResponsePayload = ResourceParser.readInResourceFile("/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/json/validResponseNullableProperties.json"); + + validator.validate(testResponsePayload); + } + + @Test + public void schemaValidateNullablePropertiesWithValues() throws Exception { + + OpenApiSchemaValidator validator = new OpenApiSchemaValidator.Builder("/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/schema/nullableProperties.yaml", "/test", NstRequestType.GET) + .allowAdditionalProperties(OpenApiSchemaValidator.AllowAdditionalProperties.NO) + .build(); + + String testResponsePayload = ResourceParser.readInResourceFile("/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/json/validResponseNullablePropertiesWithValues.json"); + + validator.validate(testResponsePayload); + } } diff --git a/NST/src/test/resources/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/json/validResponseNullableProperties.json b/NST/src/test/resources/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/json/validResponseNullableProperties.json new file mode 100644 index 0000000..6b582b0 --- /dev/null +++ b/NST/src/test/resources/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/json/validResponseNullableProperties.json @@ -0,0 +1,9 @@ +[ + { + "rank": null, + "price": 1, + "name": null, + "rate": null, + "url": null + } +] diff --git a/NST/src/test/resources/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/json/validResponseNullablePropertiesWithValues.json b/NST/src/test/resources/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/json/validResponseNullablePropertiesWithValues.json new file mode 100644 index 0000000..5776b86 --- /dev/null +++ b/NST/src/test/resources/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/json/validResponseNullablePropertiesWithValues.json @@ -0,0 +1,9 @@ +[ + { + "rank": 1, + "price": 1, + "name": "bob", + "rate": 2, + "url": "https://www.ebay.com" + } +] diff --git a/NST/src/test/resources/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/schema/nullableProperties.yaml b/NST/src/test/resources/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/schema/nullableProperties.yaml new file mode 100644 index 0000000..a550e15 --- /dev/null +++ b/NST/src/test/resources/com/ebay/nst/schema/validation/OpenApiSchemaValidatorTest/schema/nullableProperties.yaml @@ -0,0 +1,49 @@ +openapi: 3.0.0 +info: + title: Type Definitions + description: >- + Type Definitions + version: 1.0.0 +servers: + - url: 'http://ebay.com' + description: QATE +paths: + /test: + get: + summary: placeholder summary + responses: + '200': + description: Success + content: + application/json: + schema: + $ref: '#/components/schemas/NullablePropertyResponse' +components: + schemas: + NullablePropertyResponse: + type: array + items: + $ref: '#/components/schemas/NullablePropertyObject' + NullablePropertyObject: + type: object + additionalProperties: true + required: + - rank + - price + properties: + rank: + type: integer + nullable: true + price: + type: number + minimum: 0 + name: + type: string + nullable: true + rate: + type: number + nullable: true + url: + type: string + format: uri + nullable: true diff --git a/NSTTutorials/pom.xml b/NSTTutorials/pom.xml index c2c3cc5..3c35fa8 100644 --- a/NSTTutorials/pom.xml +++ b/NSTTutorials/pom.xml @@ -41,7 +41,7 @@ 3.6.0 2.9 UTF-8 - 1.3.1 + 1.3.6 0.0.1-SNAPSHOT 7.5 true diff --git a/pom.xml b/pom.xml index 34ec018..5a04dcd 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ 3.6.0 2.9 UTF-8 - 1.3.5 + 1.3.6 7.5 true