Replies: 1 comment 1 reply
-
Thank you for opening this discussion! You make a great point that a request that satisfies the input schema is not guaranteed to succeed. When a request fails to satisfy the input schema, it might be desirable for these to fail with a 400 status code, but I don't think we could ever require this, as I think that would violate the "Open World principle" that I believe is a foundation of OpenAPI. Which is why I tried to frame the discussion in the TDC today around "expectations". If it is okay for an operation to succeed, or partially succeed, even when it fails to satisfy the input schema, then I think the main value of the schema (to users of the API) is to convey how users should expect the operation to behave when validation succeeds or fails. |
Beta Was this translation helpful? Give feedback.
-
This topic spun out of a conversation with @mikekistler concerning the proposal for
itemSchema
in support of streaming media types.Mike asked what I thought was a good question. If I have a
schema
and anitemSchema
, theitemSchema
will tell me in isolation whether an individual item in a stream is valid; but if I also include aschema
does it necessarily imply that if, after the stream ends, theschema
indicates that the collective content is not valid, does that mean that the service should not have processed any of the items? (Or maybe, should roll back any changes.)I am not sure what the answer ought to be, but I think there's a broader question here that's interesting.
As a best practice, I would guide API authors/implementors to define and implement request schemas (for content, parameters, headers, whatever) such that if input is considered invalid by a schema, the request will fail with
4xx
and without any side effects.But I would also say that as a matter of pragmatism, it will likely be impossible for them to guarantee the converse: that if all the input validates against the corresponding schemas, the request will succeed with a
2xx
status code (aside from unpredictable5xx
failures). For the APIs I work in every day, there are usually validation aspects that cannot be defined in the schema. Compatibility with existing resource state, for example. A basic example of this would be usingIf-Match
where the success of the request is not determined by the schema of the value provided for the header, but by how it relates to the state on the server.But I don't think OpenAPI actually says how a service implementation ought to behave with respect to request data validating or not validating against a schema. I think it just says "this is the schema for the input" and leaves the rest to the reader.
A similar question might be asked about whether OpenAPI specifically says what response schemas mean with respect to behavior.
I anticipate that the right thing to do here is to leave some flexibility for an author/implementor to define the specifics of how a schema is used, but I think we'd do better to define which parts are left to the author/implementor. I would also be supportive of nailing down some aspects of this with respect to what a schema validation failure means even if we can't nail down anything about what schema validation success means.
But someone might, even outside of a streaming use case, might say that they want to define a correct schema but they also want to have a service which does its best to fulfill a request that has invalid data. I don't like the idea but I can certainly imagine the argument and the desirability of keeping that use case under the OpenAPI tent.
Beta Was this translation helpful? Give feedback.
All reactions