-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: validate enum values in nested models (#163)
The Java generator for `openapi-generator` completely skips over enum validation in the `validateJsonElement` function that is generated for each model class. As a result, enum values are validated inconsistently; if I have a model `X` that has a property that is an enum, the enum values will be validated if I am deserializing a JSON blob in which the top-level object is an instance of `X`, but the enum values will _not_ be validated if I am deserializing a JSON blob that has an instance of `X` nested in it. ```js // Let's say I have a schema component `X` that has a `this_is_an` property that is a string and a `status` property that is an enum of `"active"` or `"failed"`. // Given that component, the below is not a valid `X` and would cause an error in metal-java <= v0.10.0 { "this_is_an": "X", "status": "not-a-status" } // The below is an object that has a property, `x_list` that is a list of `X` instances. The `X` in this case is still not valid, but this would not cause an error in metal-java <= v0.10.0 { "x_list": [{ "this_is_an": "X", "status": "not-a-status" }] } ``` This PR introduces custom templates so that we can add enum validation to `validateJsonElement` and ensure that enum values are validated even for nested models. We needed to modify two templates: - `libraries/okhttp-gson/pojo.mustache` had to be updated to generate code that validates model properties that are enums - `modelEnum.mustache` and `modelInnerEnum.mustache` had to be updated to generate a `validateJsonElement` function for all enums Template changes happen in this commit: ddc37db Code is updated by the bot in this commit: b0f102a This PR also includes an `example` that I used to replicate the validation issue and confirm that my tests fixed it; I intended to remove it before merge, since it's really not an example of how the SDK should be used, but I'm open to keeping it. In the meantime, at least, this PR can be poked at by checking it out locally and running these commands in the project root: ```sh # if you check out the first commit on this PR, you will need to regenerate the code so the example uses the old code make generate # recompile the code so that the example is using the code that was generated above make build_client # tell Java where to find the compiled code export ExampleClassPath="equinix-openapi-metal/target/equinix-openapi-metal-0.10.0.jar:equinix-openapi-metal/target/lib/*" # run the example (doesn't hit an API, so doesn't need a token) java -classpath $ExampleClassPath examples/VirtualCircuit.java ``` --------- Co-authored-by: equinix-labs@auto-commit-workflow <[email protected]>
- Loading branch information
Showing
55 changed files
with
1,530 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.