Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.10] Add JSON pointer, patch, and OpenAPI mapper #293

Merged
merged 1 commit into from
Feb 28, 2020

Conversation

mtdowling
Copy link
Member

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.

@mtdowling mtdowling changed the title Add JSON pointer, patch, and OpenAPI mapper [0.10] Add JSON pointer, patch, and OpenAPI mapper Feb 26, 2020
@mtdowling mtdowling merged commit aa602db into 0.10 Feb 28, 2020
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.
@mtdowling mtdowling deleted the add-json-pointer-and-patching branch March 4, 2020 19:54
@kstich kstich mentioned this pull request Apr 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants