Handle array schemas with empty or missing items (fix #165) - #202
Merged
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 36ab59aec6
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
### Motivation
- Avoid NullPointerExceptions and incorrect reference resolution when OpenAPI `array` schemas either omit `items` or declare an empty `items: {}` value.
- Ensure CATS can generate request/response examples for those contracts instead of crashing or producing incorrect schema refs.
### Description
- Replace direct `getItems().get$ref()` dereferences with the safe helper `CatsModelUtils.getSchemaItems(...)` when deriving request schema names and extracting response schema references in `FuzzingDataFactory`.
- Apply the same safe-item resolution in `OpenApiUtils.addToSchemas(...)` when collecting schemas from `Content`.
- Add a regression OpenAPI contract `src/test/resources/issue165.yml` that includes an array with `items: {}` and an array missing `items`, and add the unit test `shouldHandleArraySchemasWithEmptyOrMissingItems` to `FuzzingDataFactoryTest` to cover both request and response scenarios.
### Testing
- Added the JUnit test `FuzzingDataFactoryTest#shouldHandleArraySchemasWithEmptyOrMissingItems` which asserts payload generation for the `issue165.yml` contract.
- Attempted to run `mvn -Dtest=FuzzingDataFactoryTest#shouldHandleArraySchemasWithEmptyOrMissingItems test`, but the execution failed due to Maven dependency resolution problems contacting Maven Central (HTTP 403).
- Attempted offline execution with `mvn -o -Dtest=FuzzingDataFactoryTest#shouldHandleArraySchemasWithEmptyOrMissingItems test`, but it failed because required artifacts were not present in the local Maven cache.
### Motivation
- Avoid NullPointerExceptions and incorrect reference resolution when OpenAPI `array` schemas either omit `items` or declare an empty `items: {}` value.
- Ensure CATS can generate request/response examples for those contracts instead of crashing or producing incorrect schema refs.
### Description
- Replace direct `getItems().get$ref()` dereferences with the safe helper `CatsModelUtils.getSchemaItems(...)` when deriving request schema names and extracting response schema references in `FuzzingDataFactory`.
- Apply the same safe-item resolution in `OpenApiUtils.addToSchemas(...)` when collecting schemas from `Content`.
- Add a regression OpenAPI contract `src/test/resources/issue165.yml` that includes an array with `items: {}` and an array missing `items`, and add the unit test `shouldHandleArraySchemasWithEmptyOrMissingItems` to `FuzzingDataFactoryTest` to cover both request and response scenarios.
### Testing
- Added the JUnit test `FuzzingDataFactoryTest#shouldHandleArraySchemasWithEmptyOrMissingItems` which asserts payload generation for the `issue165.yml` contract.
- Attempted to run `mvn -Dtest=FuzzingDataFactoryTest#shouldHandleArraySchemasWithEmptyOrMissingItems test`, but the execution failed due to Maven dependency resolution problems contacting Maven Central (HTTP 403).
- Attempted offline execution with `mvn -o -Dtest=FuzzingDataFactoryTest#shouldHandleArraySchemasWithEmptyOrMissingItems test`, but it failed because required artifacts were not present in the local Maven cache.
…, and add test for issue 165 ### Motivation - Fix crashes and incorrect behavior when encountering array schemas that have empty or missing `items` definitions or when `items` refs are accessed directly. - Use a single utility accessor for schema `items` to avoid NPEs and inconsistent handling across the codebase. - Add a regression test and an OpenAPI example to cover the scenario described in issue 165. ### Description - Replaced direct calls to `getItems()` with the centralized helper `CatsModelUtils.getSchemaItems(...)` in `FuzzingDataFactory` and `OpenApiUtils` to safely obtain array item schemas. - Updated `extractSchemaRef` logic in `FuzzingDataFactory` to use `CatsModelUtils.getSchemaItems(...)` when resolving array response schemas. - In `OpenAPIModelGeneratorV2.createExamplesArray` added an early check to extract and use an example from the array schema (`extractExampleFromSchema`) and return immediately when present. - Added `OpenApiUtils.getExamples(OpenAPI)` helper to safely retrieve component examples. - Added a unit test `shouldHandleArraySchemasWithEmptyOrMissingItems` and a new example OpenAPI file `issue165.yml` to reproduce and validate arrays with empty or missing `items` and zero `maxItems`. ### Testing - Ran the updated unit tests including the new `FuzzingDataFactoryTest.shouldHandleArraySchemasWithEmptyOrMissingItems`, and the new test passed. - Executed the full test suite (`mvn test`), and all unit tests completed successfully. - Verified the new sample `issue165.yml` is parsed and produces the expected payload and response examples in the test.
…, and add test for issue 165 ### Motivation - Fix crashes and incorrect behavior when encountering array schemas that have empty or missing `items` definitions or when `items` refs are accessed directly. - Use a single utility accessor for schema `items` to avoid NPEs and inconsistent handling across the codebase. - Add a regression test and an OpenAPI example to cover the scenario described in issue 165. ### Description - Replaced direct calls to `getItems()` with the centralized helper `CatsModelUtils.getSchemaItems(...)` in `FuzzingDataFactory` and `OpenApiUtils` to safely obtain array item schemas. - Updated `extractSchemaRef` logic in `FuzzingDataFactory` to use `CatsModelUtils.getSchemaItems(...)` when resolving array response schemas. - In `OpenAPIModelGeneratorV2.createExamplesArray` added an early check to extract and use an example from the array schema (`extractExampleFromSchema`) and return immediately when present. - Added `OpenApiUtils.getExamples(OpenAPI)` helper to safely retrieve component examples. - Added a unit test `shouldHandleArraySchemasWithEmptyOrMissingItems` and a new example OpenAPI file `issue165.yml` to reproduce and validate arrays with empty or missing `items` and zero `maxItems`. ### Testing - Ran the updated unit tests including the new `FuzzingDataFactoryTest.shouldHandleArraySchemasWithEmptyOrMissingItems`, and the new test passed. - Executed the full test suite (`mvn test`), and all unit tests completed successfully. - Verified the new sample `issue165.yml` is parsed and produces the expected payload and response examples in the test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
arrayschemas either omititemsor declare an emptyitems: {}value.Description
getItems().get$ref()dereferences with the safe helperCatsModelUtils.getSchemaItems(...)when deriving request schema names and extracting response schema references inFuzzingDataFactory.OpenApiUtils.addToSchemas(...)when collecting schemas fromContent.src/test/resources/issue165.ymlthat includes an array withitems: {}and an array missingitems, and add the unit testshouldHandleArraySchemasWithEmptyOrMissingItemstoFuzzingDataFactoryTestto cover both request and response scenarios.Testing
FuzzingDataFactoryTest#shouldHandleArraySchemasWithEmptyOrMissingItemswhich asserts payload generation for theissue165.ymlcontract.mvn -Dtest=FuzzingDataFactoryTest#shouldHandleArraySchemasWithEmptyOrMissingItems test, but the execution failed due to Maven dependency resolution problems contacting Maven Central (HTTP 403).mvn -o -Dtest=FuzzingDataFactoryTest#shouldHandleArraySchemasWithEmptyOrMissingItems test, but it failed because required artifacts were not present in the local Maven cache.Codex Task