[Go-server] - Support for Enums in the Path and Query#16781
[Go-server] - Support for Enums in the Path and Query#16781wing328 merged 10 commits intoOpenAPITools:masterfrom
Conversation
Supports optional Query Enums and Lists of Enums as well
Add an example endpoint that covers the added scenarios
Added an import mapping for the GoServerCodegen for "fmt"
when a model is an enum.
Expanded the Enum Model for the Go Server to have validation.
Copied this logic from the Go Client Enum Model.
| ) | ||
|
|
||
| // All allowed values of {{{classname}}} enum | ||
| var Allowed{{{classname}}}EnumValues = []{{{classname}}}{ |
There was a problem hiding this comment.
Could we do a
map[{{classname}}]struct{}
That should work in O(1) time
There was a problem hiding this comment.
Let me know if you need clarifications about this
Allowed{{{classname}}}EnumValues := map[myEnum]struct{}{
"a": {},
"b": {},
}There was a problem hiding this comment.
I changed it to be a map. I had to add in a call to the golang maps package for the error message when an invalid Enum was provided.
| {{#items.isEnumRef}} | ||
| var {{paramName}}Param []{{items.dataType}} | ||
| if query.Has("{{baseName}}") { | ||
| for _, param := range strings.Split(query.Get("{{baseName}}"), ",") { |
There was a problem hiding this comment.
Could we do strings.Split(query.Get("{{baseName}}"), ",") first then create var {{paramName}}Param with the exact size. make([]{{items.dataType}}, size)
|
|
||
| // New{{{classname}}}FromValue returns a pointer to a valid {{{classname}}} | ||
| // for the value passed as argument, or an error if the value passed is not allowed by the enum | ||
| func New{{{classname}}}FromValue(v {{{format}}}{{^format}}{{dataType}}{{/format}}) (*{{{classname}}}, error) { |
There was a problem hiding this comment.
Could you help me understand why does this function need to return a pointer?
Could you just return a {{{classname}}}
There was a problem hiding this comment.
This is the same implementation from the Go-Client Enum mustache template. By using the same implementation, a user can decide to use either the Client's models or the Server's models with only a couple minor problems to jump over.
If we want to change the signatures/implementation of the Go-Server Enum, I would also like to make the same changes to the Go-Client Enum Mustache Template.
There was a problem hiding this comment.
You don't need to modify the client as this doesn't affect the interaction between the client and server.
The changes only affect the server code internally. In this case,
- you are always dereferencing the return value in the controller (you always want the value)
- the pointer is only returned during an err, which means that the pointer will ultimately be discarded
It only makes sense to change the return type to a value since the pointer is redundant.
There was a problem hiding this comment.
I made the change to not return a pointer from New{{classname}}FromValue.
the correct size.
…nerator into go-server/enumSupport
| ) | ||
|
|
||
| // All allowed values of {{{classname}}} enum | ||
| var Allowed{{{classname}}}EnumValues = map[{{{classname}}}][]struct{}{ |
There was a problem hiding this comment.
| var Allowed{{{classname}}}EnumValues = map[{{{classname}}}][]struct{}{ | |
| var Allowed{{{classname}}}EnumValues = map[{{{classname}}}]struct{}{ |
no need extra []
| if ev.IsValid() { | ||
| return ev, nil | ||
| } else { | ||
| return "", fmt.Errorf("invalid value '%v' for Gender: valid values are %v", v, maps.Keys(AllowedGenderEnumValues)) |
There was a problem hiding this comment.
could we store maps.Keys(AllowedGenderEnumValues) as a package var?
There was a problem hiding this comment.
I'll do a privately scoped map to do validation checks on inputs and a publicly scoped slice of the enums. The map doesn't seem to be useful from a public usage of the model but the slice of valid enums did get used in my companies code base.
| {{#defaultValue}} | ||
| {{paramName}}Param := "{{defaultValue}}" | ||
| if query.Has("{{baseName}}") { | ||
| {{paramName}}Param = query.Get("{{baseName}}") | ||
| {{paramName}}Param := {{^isString}}{{dataType}}( {{/isString}}query.Get("{{baseName}}"){{^isString}} ){{/isString}} | ||
| } |
There was a problem hiding this comment.
Is this an intended change?
seems like no one can now read from {{paramName}}Param since it is declared in the if block scope
Also the default value is removed
There was a problem hiding this comment.
Definitely not intended, great catch, thank you so much
| param := query.Get("{{baseName}}") | ||
| {{paramName}}Param = ¶m |
There was a problem hiding this comment.
| param := query.Get("{{baseName}}") | |
| {{paramName}}Param = ¶m | |
| {{paramName}}Param = query.Get("{{baseName}}") |
|
otherwise, it looks ok |
Add cases to the
api-controller.mustachetemplate for Enums when in the Query and in the Path. Includes required and not required cases, as well as a list of Enums for the Query.Add logic to the Go-Server Enum Model to have a "constructor" that validates the input. This logic is duplicated from the Go Client Enum Model.
Add examples to the Go-Server test petstore.yaml for an Enum in the Path, optional enum, and required enums.
Small modification needed to the
GoServerCodegen.javafile to add the necessaryfmtimport when the model is an enum. This import is needed so that the "Constructor" can return anerror.fixes #11173
There will be conflicts that need to be resolved with #16749 as both are touching the
api-controller.mustachefile in similar locations.PR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.For Windows users, please run the script in Git BASH.
master(upcoming 7.1.0 minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)@lwj5 @wing328