Skip to content

[Go-server] - Support for Enums in the Path and Query#16781

Merged
wing328 merged 10 commits intoOpenAPITools:masterfrom
icubbon:go-server/enumSupport
Oct 17, 2023
Merged

[Go-server] - Support for Enums in the Path and Query#16781
wing328 merged 10 commits intoOpenAPITools:masterfrom
icubbon:go-server/enumSupport

Conversation

@icubbon
Copy link
Contributor

@icubbon icubbon commented Oct 10, 2023

Add cases to the api-controller.mustache template 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.java file to add the necessary fmt import when the model is an enum. This import is needed so that the "Constructor" can return an error.

fixes #11173

There will be conflicts that need to be resolved with #16749 as both are touching the api-controller.mustache file in similar locations.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    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.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@lwj5 @wing328

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.
Copy link
Contributor

@lwj5 lwj5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor performance issues

)

// All allowed values of {{{classname}}} enum
var Allowed{{{classname}}}EnumValues = []{{{classname}}}{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do a

map[{{classname}}]struct{}

That should work in O(1) time

Copy link
Contributor

@lwj5 lwj5 Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if you need clarifications about this

Allowed{{{classname}}}EnumValues := map[myEnum]struct{}{
	"a": {},
	"b": {},
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}}"), ",") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Contributor

@lwj5 lwj5 Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you help me understand why does this function need to return a pointer?

Could you just return a {{{classname}}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

@lwj5 lwj5 Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

  1. you are always dereferencing the return value in the controller (you always want the value)
  2. 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the change to not return a pointer from New{{classname}}FromValue.

)

// All allowed values of {{{classname}}} enum
var Allowed{{{classname}}}EnumValues = map[{{{classname}}}][]struct{}{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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))
Copy link
Contributor

@lwj5 lwj5 Oct 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we store maps.Keys(AllowedGenderEnumValues) as a package var?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 347 to 378
{{#defaultValue}}
{{paramName}}Param := "{{defaultValue}}"
if query.Has("{{baseName}}") {
{{paramName}}Param = query.Get("{{baseName}}")
{{paramName}}Param := {{^isString}}{{dataType}}( {{/isString}}query.Get("{{baseName}}"){{^isString}} ){{/isString}}
}
Copy link
Contributor

@lwj5 lwj5 Oct 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely not intended, great catch, thank you so much

Comment on lines 385 to 386
param := query.Get("{{baseName}}")
{{paramName}}Param = &param
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
param := query.Get("{{baseName}}")
{{paramName}}Param = &param
{{paramName}}Param = query.Get("{{baseName}}")

@lwj5
Copy link
Contributor

lwj5 commented Oct 13, 2023

otherwise, it looks ok

Copy link
Contributor

@lwj5 lwj5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm. @wing328 please help to merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [go-server] GET Query enum incorrectly cast

3 participants