Can't generate an enum with value "null" or "NULL"
The problem
If you provide an openAPI spec:
openapi: "3.0.0"
info:
version: 1.0.0
title: some title
paths:
/some-path:
get:
responses:
'200':
description: some description
content:
application/json:
schema:
$ref: '#/components/schemas/NullEnum'
components:
schemas:
NullEnum:
type: object
properties:
someEnum:
type: string
enum:
- null
- NULL
- parses_correctly
with a config file:
# yaml-language-server: $schema=https://raw.githubusercontent.com/deepmap/oapi-codegen/HEAD/configuration-schema.json
package: example
output: api.gen.go
generate:
models: true
Then the generated code looks like:
// Package example provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT.
package example
// Defines values for NullEnumSomeEnum.
const (
LessThannil NullEnumSomeEnum = "<nil>"
ParsesCorrectly NullEnumSomeEnum = "parses_correctly"
)
// NullEnum defines model for NullEnum.
type NullEnum struct {
SomeEnum *NullEnumSomeEnum `json:"someEnum,omitempty"`
}
// NullEnumSomeEnum defines model for NullEnum.SomeEnum.
type NullEnumSomeEnum string
So as you can see, the enum:
type: string
enum:
- null
- NULL
- parses_correctly
Is rendered as:
// Defines values for NullEnumSomeEnum.
const (
LessThannil NullEnumSomeEnum = "<nil>"
ParsesCorrectly NullEnumSomeEnum = "parses_correctly"
)
Which is not what I would expect.
My setup
go version: 1.24.0
oapi-codegen version: v2.4.1
generate command: go:generate go tool github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml api.yaml
As a workaround you can wrap the string null in double quotes. So:
someEnum:
type: string
enum:
- "null"
Generates a reasonable type. But the fact that you need to wrap this value in double-quotes, but don't need to for other values is less then ideal
I've been experiencing this exact issue since moving from deepmap/[email protected] to oapi-codegen/[email protected].
If I include an enum in my spec for a property that is nullable, and I follow the instructions from OpenAPI on this scenario to include a null entry in the enum listing, oapi-codegen produces variable names that appear to be matching on the < symbol used as the value "<nil>" in the const definition.
Example generated code output:
// Defines values for SomeEnum.
const (
SomeEnumExample1 SomeEnum = "accredited-investor"
SomeEnumLessThannil SomeEnum = "<nil>"
)
As another workaround, you can specify an x-enum-varnames to override the automatically generated variable names for your enum values.
someEnum:
type: string
nullable: true
enum:
- example-1
- null
x-enum-varnames:
- Example1
- Nil
the fact that you need to wrap this value in double-quotes, but don't need to for other values is less then ideal
This seems like it's a YAML issue, rather than oapi-codegen 🤔
That being said, if it's then generating <nil> then yes that's likely to be a bug!
the fact that you need to wrap this value in double-quotes, but don't need to for other values is less then ideal
This seems like it's a YAML issue, rather than
oapi-codegen🤔That being said, if it's then generating
<nil>then yes that's likely to be a bug!
@jamietanna Given that if you wrap the string null in quotes (i.e. "null" > null) it generates a reasonable type. Could the solution be:
- If a user provides an enum with value
nulloapi-codegen fails with a message saying that this is not a valid type and they need to usenullinstead OR - oapi-codegen implicitly wraps the enum value
null(case-insensitive) in quotes ?