-
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
Integer enum with named items #681
Comments
Parent-Issue: json-schema-org/json-schema-spec#579, related: json-schema-org/json-schema-spec#348. |
It's not related to json-schema-org/json-schema-spec#348, it's a duplicate for it. |
@webron |
Fair point, missed that. |
I like this, combined with json-schema-org/json-schema-spec#348: type: integer
format: int32
enum:
- value: 1
name: Sunny
description: Blue sky
- value: 2
name: Cloudy
description: Slightly overcast
- value: 3
name: Rainy
description: Take an umbrella with you If the names should be unique (probably a prerequisite for code generation) a hashmap would be more appropriate: type: integer
format: int32
enum:
Sunny:
value: 1
description: Blue sky
Cloudy:
value: 2
description: Slightly overcast
Rainy:
value: 3
description: Take an umbrella with you So |
Well, either the However, where does it end? Just a single value? Technically in Java (referenced in the original comment) allows setting any number of properties for enum values. What about types? There's no indication it's an int, it could be a string a string as well. Should that be inferred? Are we supposed to introduce a big construct to support all these options? Also, I have no idea what other languages support and how it would affect them. And last but not least, if I just want to define a basic enough, would I have to end up with something like: enum:
Sunny: {}
Cloudy: {}
Rainy: {} That's... |
This feels like a leaking of implementation details in the API, seemingly no different then describing every possible combination of values for a proper object's properties. I suspect it is the result of attempting to use the standard to automatically create an sdk for functional calls, in which enums are being semantically defined and maintained here. To me, enums are an object-type not a value-type and thus shouldn't get any special treatment. |
Now I think @ralfhandl's former format is best.
For webron's comments…
I think both should be unique and should be written in the specs.
I don't feel the number needs to be limited.
Value types of enums are specified since Swagger 2.0, like this:
(Looks like only strings were allowed in 1.0) For wparad's comments… I consider "naming enum items" is a missing spec in Swagger 2.0. In general, for an enum to be an enum, each item should have an name.
|
@webron The name (if there is one) needs to be unique in the target programming language. The value needs to be unique "on the wire". But the value is not necessarily a string, it can be anything (string, number, boolean, object, array). Therefore using the value as a keys in a map / property names in an object doesn't really work. |
I wasn't advocating the map option, just picked one of the two. I'd like to clarify that the limitations for the enum declarations are derived from JSON Schema - we did not introduce any special restriction there. Also, unlike what @kenjitayama mentioned, the type is not derived from the type mentioned in the encompassing definition - again, that's the way JSON Schema works. type: string
enum:
- 1
- 2
- 3 doesn't mean that There are two problems to breaking away from the current structure:
|
I'm not sure I understood what webron said about types, but I found that Swagger 2.0 uses JSON Schema Draft 4 (the latest) and enum spec is derived from it. Next, I found these proposals for JSON Scheme v5
I wonder if one of this would be accepted, and if OpenAPI v3 will support JSON Schema v5. And when it would happen. Other reference: |
While we've yet to reach that point, it's unlikely draft 5 will be supported because it's not finalized. Even if finalized before finalization of 3.0, it's unlikely they'd be enough tooling around it for us to claim support of it. That said, if it's finalized before, it's easier to derive the structure from it and use it as well. |
I see there's a long way to go. |
Is there any recommendation for how to define these possible values in the existing swagger specifications until this becomes supported? |
Tackling PR: json-schema-org/json-schema-spec#741 |
+1 |
Is this still going on? Because it would help A LOT |
Because this breaks away from JSON Schema, we've not included support for it in 3.0. |
I saw some great suggestions on using something like:
Along with using the common method. Is this valid yet? |
Things haven't changed in the last 6 hours ;) |
This is a problem for us too. Are there any hacky work arounds to make this work in the meantime? |
@athornz Microsoft tooling uses the extension x-ms-enum |
It's been over a year since the last comments, has anything changed on this front? All proposed options would work for me. |
This is also an issue for C#. NSwag is currently generating the following for an enum because their is no support for name. Generated Example: Generated Code should like the following: |
Face with same issue on NSwag client generation. |
@BlackGad @Kevweir @rlabrecque @peol etc. OpenAPI 3.1 is likely to move to JSON Schema draft 2019-09 (formerly known as draft-08) per PR #1977. That would allow the solution I showed above and allows for creating new re-usable JSON Schema vocabularies (sets of keywords) for specific purposes such as code generation. These can be formally identified so that different implementations can recognize and implement them (which was previously not possible in JSON Schema, or at least not in any practical way). So: Here's an example with a totally fictional codeGen: namedEnum
oneOf:
- const: 1
title: Sunny
description: Blue sky
- const: 2
title: Cloudy
description: Slightly overcast
- const: 3
title: Rainy
description: Take an umbrella with you In this example, the This way, we do not need to add more keywords that have an identical validation effect, which adds to the burden of validator implementations. Validators can safely ignore the unknown-to-them This is just one of many ways this (and other code generation problems) could be solved. Hopefully once OAS 3.1 is out (assuming draft 2019-09 JSON Schema support is indeed included) the OpenAPI community will use the new features to try out different ideas and converge on a standard code generation vocabulary. The advantage of OAS 3.1 + 2019-09 JSON Schema is that this process of producing a new vocabulary can happen independent of both the JSON Schema and OAS specification processes. |
@MikeRalphson @webron @darrelmiller OAS 3.1 was released,any update on this issue? should we use |
@xiaoxiangmoe I made up It will obviously take a bit for JSON Schema vendors to add support for vocabularies (meaning, add the annotation output support and enable folks to write plugins to handle previously unknown vocabularies rather than just hardcoding all keyword support) and for OAS vendors to make use of that (meaning make use of annotations from JSON Schema to recognize codegen use cases). So the solution is now possible in theory, but there are several things that need to be done before it's usable in practice. |
Note that if you take the json of an OpenAPI modal directly, without any modifications, and use it as the basis of your JSON Schema Form, you instantly have a nice looking form using all the information from the OpenAPI (title, description, format etc.) where the user can submit data. This way I make the latest developments of an API quickly available to my colleagues. If this proposal is accepted, that would enable dropdowns where the enumNames are displayed but the underlying enums are in the payload. My point is that the JSON Schema Form specification is exactly the same as OpenAPI except for this one thing and therefore, I fully support this proposal. |
@MarkRosemaker parallel arrays are horribly error-prone which is why the proposal as initially shown here has never drawn serious support within JSON Schema. The |
This is similar. I have several linked fields that could be each in a separate enum, but they go together (OK, system could be static, but code and display need to be linked):
Is there a way to link two or more enum values like this? It doesn't give an error, but the enum values don't render in schema output. Also, the field names are fixed, I can't use "title" and "description". |
I've tried the following, but the enum still doesn't render:
Yes, I know you could say this is an implementation issue with the tools, but I would just like to know how to link two or more value sets so that they are consistent rather than having separate enum sets for "code" and "display" strings. |
@Tasselhoff the answer is still the same as in earlier comments. Since this is fundamentally a JSON Schema concern, there is a JSON Schema issue tracking the |
I've made a suggestiion in swagger-ui that integrates both linked fields and parent-child relationships, but it straddles the rendering and the OpenAPI Specification implementation swagger-api/swagger-ui#7260 Edit: as you can no doubt see, I'm pretty new at this and my desire for certain functionality far exceeds my technical domain knowledge... |
We've decided to close this issue, following @handrews's proposal. For reference: codeGen: namedEnum
oneOf:
- const: 1
title: Sunny
description: Blue sky
- const: 2
title: Cloudy
description: Slightly overcast
- const: 3
title: Rainy
description: Take an umbrella with you The Regarding the concerns about code-generation - we feel code generators can detect such structure and convert it to enum stucts if needed. As proposed by @handrews, there can be a codegen vocabulary that adds annotations to give hints to such tools (see: Since now we're fully aligned with JSON Schema, any changes to how schemas are defined in OpenAPI would be addressed by the JSON Schema team and not the OpenAPI. |
@webron I can't find any documentation for the |
@xiaoxiangmoe |
I would like to propose a new field called
enumNames
.Example:
This information is necessary for generating the following Java enum.
Some web APIs require clients to send/retrieve integer enum items while knowing what they mean. So it is useful to be able to generate this kind of enum.
I had been seeking advice in the following swagger-codegen issue, and got a conclusion that we need a vendor extension (which corresponds to this proposal) using Swagger 2.0 spec.
swagger-api/swagger-codegen#2690
The text was updated successfully, but these errors were encountered: