Integer enum with named items
See original GitHub issueI would like to propose a new field called enumNames
.
Example:
definitions:
weather:
type: object
required:
- type
properties:
type:
type: integer
format: int32
enum:
- 1
- 2
- 3
enumNames:
- Sunny
- Cloudy
- Rainy
This information is necessary for generating the following Java enum.
public enum Weather {
Sunny(1),
Cloudy(2),
Rainy(3);
private final int type;
private Weather(final int type) {
this.type = type;
}
public type getType() {
return type;
}
}
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. https://github.com/swagger-api/swagger-codegen/issues/2690
Issue Analytics
- State:
- Created 7 years ago
- Reactions:92
- Comments:45 (21 by maintainers)
Top Results From Across the Web
How to define enum mapping in OpenAPI? - swagger
This way you can specify both custom names ( title ) and descriptions for enum values. Severity: type: integer oneOf: - title: HIGH...
Read more >Enums
You can use the enum keyword to specify possible values of a request parameter or a model property. For example, the sort parameter...
Read more >C# Enumerations Type - Enum
In C#, an enum (or enumeration type) is used to assign constant names to a group of numeric integer values. It makes constant...
Read more >Enumeration types - C# reference
An enumeration type (or enum type) is a value type defined by a set of ... the associated constant values of enum members...
Read more >How to Use Enum in C# | LoginRadius Blog
An effective way to describe a set of named integral constants assigned to a variable is to include a ... By default, the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I like this, combined with json-schema-org/json-schema-spec#348:
If the names should be unique (probably a prerequisite for code generation) a hashmap would be more appropriate:
So
enum
could either have an array value with just the list of values, or an object value with named and potentially further annotated values.With OpenAPI v3, some tools supports a
x-enum-varnames
extension to do so:Other has started to use
x-enum-values
(see Extension Properties on OpenAPI Specs)