OpenAPITools / OpenAPITools/openapi-generator
[REQ][Go] Validate enums
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Is your feature request related to a problem? Please describe.
The Go models generated by openapi-generator currently don't contain any kind of input validation code.
Using the x-go-custom-tag: validate:"regexp=..." syntax, it is possible to at least add some validation tags for external validation modules (this example is intended for gopkg.in/validator.v2).
This will not work when referencing enums - in this case, the go generator will create a separate model source, and anything that should be attached to the containing struct (such as tags) will get dropped.
Describe the solution you'd like
The Go generator should at least generate some validation functions on the enum type that can be called after deserialization.
Or even better, it should generate an UnmarshalJSON function that validates, so it will be called automatically by the JSON decoder.
Describe alternatives you've considered
The only alternative I can use at the moment is using inline enums - in that case, x-go-custom-tag will work as expected.
Additional context
Reference example
components:
schemas:
Status:
type: string
enum:
- ok
- error
x-go-custom-tag: validate:"regexp=^ok|error$"
Test:
type: object
properties:
Status:
$ref: '#/components/schemas/Status'
generates
type Status string
const (
STATUS_OK Status = "ok"
STATUS_ERROR Status = "error"
)
// no validation code for Status
type Test struct {
// x-go-custom-tag is ignored
Status Status `json:"Status"`
}
Inline example
components:
schemas:
Status:
Test:
type: object
properties:
Status:
type: string
enum:
- ok
- error
x-go-custom-tag: validate:"regexp=^ok|error$"
generates
// no enum type
type Test struct {
// x-go-custom-tag is added
Status string `json:"Status" validate:"regexp=^ok|error$"`
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Go generator's handling of referenced enum schemas and compare it with inline enums using the supplied OpenAPI examples. Clarify whether generated enum validation functions or automatic UnmarshalJSON validation is the intended scope. Done should include validation for referenced enum values without dropping the custom tag behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100