[0.10] Add JSON pointer, patch, and OpenAPI mapper #293
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.
This commit adds a new mapper to OpenAPI that allows changing specific
parts of the generated schema using JSON pointers that inject values
using semantics similar to JSON Patch. This approach is preferred over
string substitutions because it is more explicit and does not encourage
modelers to include placeholders in their models.
The JSON substitutions mapper now allows for any string to be replaced.
This is still basically just as safe since it only replaces exact string
matches (it doesn't replace inside of larger strings). Most importantly,
the use of placeholder like strings encouraged modelers to include
placeholders in their models. By opening up the string to any value, you
can now replace whatever was generated from real model values.
To achieve this, I implemented a JSON Pointer parser and abstraction
over Node values that also include methods for getting a value from a
node by a pointer, and adding/replacing a value in a node using a
pointer (with immutable return values, of course). I was able to
integrate this abstraction into SchemaDocument and Schema too. The JSON
Patch "add" operation has been implemented in two ways: 1) we do it just
like JSON patch, where attempting to traverse through missing objects
and arrays causes the change to be ignored 2) we have a mode that
creates intermediate objects when needed, allowing this to be used in
the JSON Schema SchemaDocument code for generating JSON from an
in-memory document.
Implementing this change revealed an omission in ArrayNode where bounds
were not being checked. For example, attempting to access index -1 would
raise an exception. This is now fixed and covered in a test case.
Issue #, if available:
Description of changes:
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.