-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
Question on nested oneOf definition #2260
Comments
What do you mean when you use the term "object"? Do you mean the "object" as defined in RFC 8259? I.e An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members) Or are you are referring to an implementation object (such as an instance of a Java class or C#), as implemented by particular client in a given programming language? Or something else? If you meant the implementation object, IMO, what matters from the perspective of OAI is how the data is serialized on the wire. The client may be implemented using any number of data structures and programming constructs, which may or may not available across programming languages. E.g. class, struct, set, list... but this internal representation is not relevant to OAI. |
Good question. I used the term |
ok, then what do you mean by the client obtain the danish_pig object directly? Or should it obtain pig object directly? {
"className": "danish_pig",
"weight": 32.1
} |
The endpoint returns a Given the payload below:
If the object is a Pig (oneOf schema with If someone defines another endpoint that returns "danish_pig", then the payload above is also valid. |
From a OAI validation perspective, a compliant validator (the client) validates whether the payload is valid or not. The validation process does not necessarily involve creating a representation of the payload in the programming language. To validate the payload, the client has to assert all JSON schema constraints, which can be done without creating "objects" in the programming language. For example this could be done using a streaming validator. Besides validation, the client may optionally decide to create a data structure in a programming language, such that the data can be manipulated by the client (and possibly sent back to the server). IMO this is completely up to the client to decide which data structure to use.
This part is an internal decision to the client. It's not visible outside the client. A client may not even have the concept of a unique per schema representation of the objects. The client could use a generic JSON document data structure. Suppose the payload below is retrieved from the server: {
"className": "danish_pig",
"weight": 32.1
} The client modifies and sends back the following payload: {
"className": "danish_pig",
"weight": 37.2
} What the client does in between to make that modification is completely up to the client. It could be as ugly as doing string replacement manipulations of the JSON payload without creating any object. It could involve parsing the payload into a JsonDocument object. Or (more sensibly) a class/struct could be instantiated. I don't think how the client represents the data in a programming language is relevant to the OAI spec. |
I think one thing I didn't make it clear in my issue description is that both API client and server are in compliance with the OAI specification (the contract between the client and server). Using
All fields are required which means the value of My understanding is that the same needs to be applied to I agree with you that OpenAPI specification does not mandate how the implementation should be done in the programming languages but still the output (implementation) needs to comply with the OpenAPI specification. Back to my original question:
If client (implementation) obtains If these constraints/validations are dropped, does the client (implementation) still comply with the OpenAPI specification? |
Please provide the exact definition of "obtains" and "directly". Concretely, what does the client do to "obtain" something? Please reformulate your question to avoid using vague and ambiguous terms. Above I pointed out the client may provide one or more of the following capabilities:
|
Let's consider the following in the famous Petstore OpenAPI specification: The client sends the Now consider replacing both
If the client doesn't validate the payload as you mentioned "client may provide one or more of the following capabilities", then the client doesn't comply with the OpenAPI specification. Now consider this particular line: https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v3.0/petstore-expanded.yaml#L66 which is the payload in the request body and it has certain properties defined. If a client (implementation) may send Are we on the same page that all the validations (including oneOf) in the |
Yes I agree a compliant validator must validate whatever is specified in a OAI document.
I still don't see how we can answer your question without reformulation. It would help if you reformulate by using words that are meaningful in the OAI spec. For example, did you mean "should the client validate the payload against the danish_pig first? Or should it validate against the pig object first?" Or something else? You should be able to reformulate the question. |
I have a couple of questions
|
Assuming the endpoint must receive NewPet: |
Yes, I agree with you on both. |
It would have helped to clarify that with an enum. For 1, even: |
First of all, I've removed the
OK. I think I got the answer I need.
From what I understand, OpenAPI specification is for describing RESTful interfaces. I think your suggestion to include enum values for the className property is more like API best practice as not all data exchange between existing REST APIs has the property "className" defined. Even if it's defined, its name can be something else such as "class_type", "object_type", etc. |
Oh yes, sure, "classname" is just one possible name for the property. Like you said it could be "class_type", "object_type", "type", or any other meaningful name for REST. Even the author uses "classname", the value of the "classname" should not be leaking programming details, for example, it would be a bad idea to set the value of |
OP commented that they got the answer they needed, so I'm closing this as resolved. |
Using the following nested oneOf definition as an example
UPDATE (JUN 12): I've removed the
classname
property to avoid confusion on the correctness of the object.The API server's endpoint returns
mammal
(not provided in the spec above). If let's say the API client (in Java for example) submits an HTTP request to the endpoint and the server returns adanish_pig
, e.g.One can further process the object returned by the server and send it back (via another endpoint expecting
mammal
in the payload). For example, switch the payload fromdanish_pig
tobasque_pig
and send it to the server, which should accepts it without issue. Switching the payload fromdanish_pig
to something else (e.g.bird
,snake
) should result in an error in the client side if the validation is performed or the server should return an error asbird
,snake
do not conform to themammal
definition.Here is the question: should the client obtain the
danish_pig
object directly? Or should it obtainpig
object directly? Or actually the return object should bemammal
object instead?Thank you for your time.
The text was updated successfully, but these errors were encountered: