oapi-codegen
oapi-codegen copied to clipboard
Feature: add possibility to create enum using oneof/const notation
This PR tries to implement enum with labels as discussed in this Stackoverflow thread without using extra field x-enum-varnames that can change between each OpenAPI consumer.
With this PR, this openapi definition:
Definition 👇
{
"info": {
"title": "My API",
"version": "development"
},
"openapi": "3.0.0",
"components": {
"schemas": {
"FindOneRequest": {
"type": "object"
},
"FindOneResponse": {
"properties": {
"user": {
"$ref": "#/components/schemas/User"
}
},
"type": "object"
},
"User": {
"properties": {
"id": {
"type": "string"
},
"status": {
"$ref": "#/components/schemas/UserStatus"
}
},
"type": "object"
},
"UserStatus": {
"format": "int32",
"oneOf": [
{
"const": 0,
"format": "integer",
"title": "NOT_SET",
"type": "const"
},
{
"const": 1,
"format": "integer",
"title": "ACTIVE",
"type": "const"
},
{
"const": 2,
"format": "integer",
"title": "INACTIVE",
"type": "const"
},
{
"const": 3,
"format": "integer",
"title": "DELETED",
"type": "const"
}
],
"type": "integer"
}
}
},
"paths": {
"/users/{ID}": {
"get": {
"operationId": "UserFindRequest",
"parameters": [
{
"in": "path",
"name": "ID",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/FindOneRequest"
}
}
}
},
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/FindOneResponse"
}
}
}
}
}
}
}
}
}
Will be generated as:
// Defines values for UserStatus.
const (
ACTIVE UserStatus = 1
DELETED UserStatus = 3
INACTIVE UserStatus = 2
NOT_SET UserStatus = 0
)
// UserStatus defines model for UserStatus.
type UserStatus = int32
I hope you find this useful ! Don't hesitate to suggest changes 👍