Context property annotations #56
Replies: 4 comments 6 replies
-
I have mixed feelings about this to be honest. On the one hand.. I agree that it would be very helpful to understand on a per property level how it could be used. II think along the lines of the variety of tools that will consume this document and do something with it. Doc tools are now going to need to add logic to handle each of these additions and what it means for the generated documentation. Then you have the variety of code gen tools, mock tools, etc.. are ALL of them by every vendor/author going to utilize these in the same way so that it's guaranteed consistent across tooling? Doubtful. But then you can take it further.. what about adding properties for things like RBAC? Maybe I want my "owner" to be mutable if the user is an Admin.. but if it's a normal user.. they don't even get this property. Similar to the idea that we have individual objects for different operations based on the role of the API consumer. Admin gets the full shabang.. every property.. full control. But a manager role only gets some of the properties.. and a standard user gets even less.. read only.. with only their name/email being able to be changed. How do you account for all those? Granted you could have RBAC specific endpoints instead.. but you're still going to likely end up having duplicate definitions or overlap to cover the different scenarios that could exist. |
Beta Was this translation helpful? Give feedback.
-
writeOnly is already defined as an annotation in Json Schema 2020-12. |
Beta Was this translation helpful? Give feedback.
-
Here is an edited-down version of a similar idea I described on the JSON Schema Slack, and fortunately saved. I've put a few things in bold italics for folks who just want to skim to the interesting bits. As noted below, this was very much off the top of my head, so it's not so much a serious alternative proposal as a sketch of some things to consider. It overlaps with @darrelmiller's proposal, but takes into account and explains the mechanics of JSON Schema validation and annotation interactions, which requires a different keyword design. Annotations are how an application (including what we call "downstream specifications" like OAS) can build additional functionality on top of JSON Schema's instance-driven and context-unaware behavior. So let's look at the HTTP context cases here:
These new annotations would, like So, with a good vocabulary that expressed the above semantics you would do this:
Annotation results include a JSON Pointer for the instance value, so you can check the value for further processing if needed. In this approach, you'd never use {
"properties": {
"id": {
"type": "integer",
"minimum": 1
},
"lastModified": {
"type": "string",
"format": "date-time"
}
},
"httpConstraints": {
"id": {
"required": ["GET", "PUT"],
"forbidden": ["POST"],
"unmodified": ["PUT", "PATCH"]
},
"lastModified": {
"required": ["GET"],
"serverManaged": true
}
}
}
You would have a separate HTTP-aware validation tool that would consume the annotation output from JSON Schema validation and perform additional validation. Annotation vocabularies are easy to implement (and some implementations treat unknown keywords as annotations by default, as the 2020-12 spec recommends, meaning they do not require any custom code). Because annotation output includes JSON Pointers to the schema and instance locations, this 2nd HTTP-aware validation pass would be much simpler to implement than JSON Schema. But it would correctly keep the instance data concerns and HTTP context concerns separate. |
Beta Was this translation helpful? Give feedback.
-
Fully agree, we are generating three schemas of The generator is controlled by model annotations that are very similarly named to Darrel's proposal 😄 The four four keywords above cover the most common cases. There's a fifth keyword one whose necessity I initially doubted and then learned better:
|
Beta Was this translation helpful? Give feedback.
-
JSON Schema is not aware of the context of usage of the schemas that it defines. It has no notion that a schema is being used to create a resource, update it, or read it.
This leads people today to create multiple schemas so that there can be an accurate schema for each request or response content. However, this introduces duplication or requires splitting the schema in order to reuse parts of the schema that don't change between responses. This gets hard for documentation and code generators to process.
I am proposing that we introduce four new keywords that are boolean flags into the OAS JSON Schema vocabulary:
Example:
While this is a proposal aimed at Moonwalk, it would be possible to introduce these in OpenAPI 3.1.
Beta Was this translation helpful? Give feedback.
All reactions