-
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
Defining constant value in response #1313
Comments
If an API had an exact response then it would seem rather redundant to call it. Why even have an API if it returns the same value every time? |
The above example shows a response which is returned only if the targeted element doesn't exist, that is, with http status code = 404. In other cases it replies something different. The different kinds of errors can produce different responses. In the project I am working in, the JSON response always has a key "status" which can be either "OK" or "ERROR" and in case of "ERROR" there must exist an "error_code" key as well (and potentially others). The fact that the response contains the error code as well, it makes the client development easier because the client doesn't have to interpret all the http status codes. In case of 404 of a particular API endpoint the value of the error_code is always the same but in case of 200 it is varying. |
@halmai Taking my OAI hat off for the moment, because this is purely a personal opinion. Also, this comment isn't directed specifically at you, it is just that your assertion reflects a common belief that I feel needs to be addressed. You said,
I believe that this is incorrect. Moving the error description into the response body "for consistency" does help map HTTP APIs to a client side RPC signature. It allows HTTP APIs to be presented in a way that client developers are more familiar with. However, it is definitely not easier than using the HTTP status codes as they were intended to be used. In your example, you already know that the response is a 404. Having a client understand that HTTP Status code 404 means "not found" is not only simple, it is consistent across all HTTP API implementations. Requiring a client developer to have to read a payload of some media type, that has some custom error payload structure, that then has some custom enum that repeats the error "not found" is not easier. Client libraries do not have to interpret ALL of the HTTP status codes. It needs to understand 200, 300, 400, and 500 plus optionally any extra codes that the client wants to do special handling for. The HTTP spec says that if you receive a status code that you don't understand, round it down to the nearest hundred and treat it as that. It can be useful to have payloads for response bodies that have additional details. However, there is a standard for that https://tools.ietf.org/html/rfc7807 which has existing implementations. These things should not be reinvented over and over again. Finally, your original request was the ability to define a schema for a constant value. OpenAPI bases it's schema on JSON Schema, and JSON schema doesn't support what you are trying to do. If you want JSON Schema to change, then I would suggest talking to them, we don't have that authority. |
That practice is very common. I am facing the same problem than the original user. Being able to model that constant property would enable us to property document an existing API and feed the API Manager with it accordingly. |
@darrelmiller I think I have a similar use case. My API returns its version number in the payload which is currently fixed ( |
I've just started using OpenAPI, so I may be lacking a lot of context, but is this json-schema-org ticket an indication that the work on the JSON Schema side has already been done? I.e. the released Pre-preview 2017-03-09 already contains this value, which is visible in the source here. So is this functionality now blocked by an official release or something of JSON Schema? Is it something OpenAPI can adopt while still in draft? Specifically:
|
@darrelmiller @tobymurray @halmai JSON Schema introduced The implementation delta between draft-06 and draft-07 is smaller than that from older drafts to draft-06. And some of it (e.g. moving So at this point it depends on if/when OpenAPI chooses to move to a newer draft. There are now draft-06 implementations in the wild in JavaScript, Java, Go, and .NET (and hopefully soon Python). |
We will review updating our support for newer versions of JSON Schema when we are considering the features that will be going into 3.1. |
I'd like to register my interest in what @halmai originally requested. My use case is that in our responses there are some generic properties for making the response a bit more self-describing. For example, every response has a "kind" property that allows identifying what kind of stuff it contains. We currently follow what @halmai found, namely to define an enum with one value. For example, the definition of an error response states that "kind" must be "error" as follows:
In the Swagger 3.x editor, the Model view of the response shows that property as:
When opening the
That is quite inconvenient, and a const definition for such a fixed value would allow the editor to show the value right away. Plus, stating a required fixed value via a construct such as "const" is semantically really much more to the point of what the interface wants to express in such a case., compared to defining an enum with one value. I'm looking forward to see OpenAPI 3.1 embrace this feature from the JSON schema. |
@andy-maier specifically what you're describing is more of a tooling issue than a spec issue. |
I have meanwhile found the same behavior also in the Python client generated by the Swagger 2.0 tooling, and have come to the conclusion that the tooling has no chance (!!) to convert an enum with one value into a const. The reason for that is that it is possible that the enum list becomes longer in a future version of the API YAML file, so the tooling has to present that faithfully as specified, in order not to cause an incompatible change for its users in the future (e.g. a Python program using the API provided by the generated Python client module). As long as the API YAML file does not distinguish between an enum with one value (that might become more than one value in the future) and a fixed value, the tooling must behave that way. So the spec format is the root cause for the behavior of the tooling, and that is not just a flaw in the tooling. |
@andy-maier that's a really interesting point that I didn't even think of when we added |
I agree with @andy-maier: We have the same use case with the difference, that we call it We took the same approach with using enum with one element. During the time, there were misinterpretations, where developer would mistakenly assume, that this property is an array, while using the swagger ui. |
We are interested by OpenAPI supporting the notion of a constant in the schema definition. For legacy reasons on the backend, the current API I am working on requires in each object a constant to be defined. Its more of less represent the precise type of the object and helps the server to perform the deserialisation correctly. This means that each JSON Object sent by the client needs to set the information, which is the same for all objects corresponding to a specific schema. Example:
Right now we are using the enum approach described in this issue:
It looks well in the documentation (swagger online editor): In the client (java code in this example), need explicitly to set the type each time an object is instantiated:
With a constant, I assume that the line |
Having a constant could be interesting for the Inheritance and Polymorphism case (when a
In this case, |
I'd love to see this implemented as I'm having difficulty with geojson_swagger
Please see my original issue at swagger-api/swagger-codegen#7756 (comment) |
@jmini on the JSON Schema side, we're hoping to add a code generation vocabulary with the OpenAPI community being one of the main drivers. This would provide a standardized way to disambiguate JSON Schema validation constructs that were never intended to be used without an instance. e.g. an I'm rather hoping that We definitely have found in draft-06 schemas that |
Is there any update on this ? If you want a clear use-case, just think json:api : GET /articles/ response:
The current workaround (enum with one value) is so hacky I don't understand how it is sufficient/reasonable for everyone. |
@gvdmarck JSON Schema 2019-09 (formerly known as draft-08) has now been published. PR #1977 in this repository updates OpenAPI's Schema Object for OAS 3.1 to use JSON Schema 2019-09, which includes the |
Any progress on this? I'm personally interested in it from the polymorphism perspective @jmini pointed out. It's very powerful in TypeScript, since that supports constant string values, and can use it as descriminators. |
This was fixed in #1977 so we can close this. |
**What** - Generate a Go `const` if the enum values has exactly one element - See OAI/OpenAPI-Specification#1313 and OAI/OpenAPI-Specification#1620 as examples where the openapi team blessed the "singleton enum" example Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
**What** - Generate a Go `const` if the enum values has exactly one element - See OAI/OpenAPI-Specification#1313 and OAI/OpenAPI-Specification#1620 as examples where the openapi team blessed the "singleton enum" example Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
But this approach doesn't seem to be right. Just take a look at https://softwareengineering.stackexchange.com/a/305294/244734 |
Did anyone ever use |
@uniqueg It works just like two of the proposed solutions in the original comment, but with You can use it for individual properties: schema:
type: object
properties:
result:
const: "ERROR"
error_code:
const: "ERROR__PET_NOT_FOUND" Or as the entire schema: schema:
const:
result: "ERROR"
error_code: "ERROR__PET_NOT_FOUND" There was another syntax proposed in the initial comment that put |
Thank you so much for taking the time to post that example - this is awesome! 🚀 Let's see if this is also supported by our tooling 🙃 |
In order to specify that the response for an API call is always a certain given value, I would like to create this this feature request.
If the client wants to get the details of a non-existing pet of a pet store then the server should say
The best way I found for describing this is to use enums with only one element, like this:
Instead of this, an exact value should be defined with an implicit type detection. So,
should mean the same as the previous declaration.
The following scalar types should be auto-detected:
Moreover, the new
value
declaration should work one level above as well:This specification should mean a structure that has the two fields with these two constant values.
One more step would be the following notation:
This would determine the type to be an objects and the properties as above.
This notation would be much more dense and easier to both write and understand.
The text was updated successfully, but these errors were encountered: