Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[go-server] Add ability to handle nullable query param #17051

Closed
wants to merge 25 commits into from

Conversation

lwj5
Copy link
Contributor

@lwj5 lwj5 commented Nov 13, 2023

Add the ability to handle nullable query param

nullable query params will now be passed as a pointer to the servicer.
non-nullable query params will still be passed as value.

There is no breaking changes in this PR

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
    
    (For Windows users, please run the script in Git BASH)
    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*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • 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 lwj5 changed the title Add ability to handle null queries [go-server] Add ability to handle null queries Nov 13, 2023
@lwj5 lwj5 changed the title [go-server] Add ability to handle null queries [go-server] Add ability to handle nullable query param Nov 13, 2023
# Conflicts:
#	samples/openapi3/server/petstore/go/go-petstore/.openapi-generator/VERSION
#	samples/server/petstore/go-api-server/.openapi-generator/VERSION
#	samples/server/petstore/go-chi-server/.openapi-generator/VERSION
@@ -23,7 +23,7 @@ func New{{classname}}Service() {{classname}}Servicer {
{{#isDeprecated}}
// Deprecated
{{/isDeprecated}}
func (s *{{classname}}Service) {{nickname}}(ctx context.Context{{#allParams}}, {{paramName}} {{dataType}}{{/allParams}}) (ImplResponse, error) {
func (s *{{classname}}Service) {{nickname}}(ctx context.Context{{#allParams}}, {{paramName}} {{#isNullable}}*{{/isNullable}}{{dataType}}{{/allParams}}) (ImplResponse, error) {
Copy link
Contributor

@icubbon icubbon Nov 13, 2023

Choose a reason for hiding this comment

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

I don't think isNullable is behaving as you expect it to here. The Pet Store Specs have many non-required params that would show this change but none of them are showing up in this diff.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

could you provide a specific example? are they marked as nullable?

Copy link
Contributor

Choose a reason for hiding this comment

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

One specific example from openapi-generator/samples/server/petstore/go-api-server/api/openapi.yaml is the booleanTest query parameter in deleteUser.

- description: boolean query parameter
        explode: true
        in: query
        name: boolean_test
        required: false
        schema:
          type: boolean
        style: form

This should result in the the function signature for deleteUser having a *bool parameter for the function signature in openapi-generator/samples/server/petstore/go-api-server/go/api_user_service.go

There are numerous query parameters in that example file that have optional query parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lwj5
Copy link
Contributor Author

lwj5 commented Nov 16, 2023

To fix other import issues with os workaround in the future. ref: #11097

@@ -23,7 +23,7 @@ func New{{classname}}Service() {{classname}}Servicer {
{{#isDeprecated}}
// Deprecated
{{/isDeprecated}}
func (s *{{classname}}Service) {{nickname}}(ctx context.Context{{#allParams}}, {{paramName}} {{dataType}}{{/allParams}}) (ImplResponse, error) {
func (s *{{classname}}Service) {{nickname}}(ctx context.Context{{#allParams}}, {{paramName}} {{#isNullable}}*{{/isNullable}}{{dataType}}{{/allParams}}) (ImplResponse, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

One specific example from openapi-generator/samples/server/petstore/go-api-server/api/openapi.yaml is the booleanTest query parameter in deleteUser.

- description: boolean query parameter
        explode: true
        in: query
        name: boolean_test
        required: false
        schema:
          type: boolean
        style: form

This should result in the the function signature for deleteUser having a *bool parameter for the function signature in openapi-generator/samples/server/petstore/go-api-server/go/api_user_service.go

There are numerous query parameters in that example file that have optional query parameters.

@lwj5
Copy link
Contributor Author

lwj5 commented Nov 16, 2023

Please be patient, I'm still fixing test issues. I'll request for a review via the "re-request review" button

@lwj5 lwj5 requested a review from icubbon November 17, 2023 03:00
@icubbon
Copy link
Contributor

icubbon commented Nov 17, 2023

I'm still researching and reading about how nullable is handled in other generators.

What is currently giving me hesitation is comparing this implementation to how the go-client handles non-required parameters. Below is how the go-client determines if a parameter will be a pointer or a value:
{{name}} {{^required}}{{^isNullable}}{{^isArray}}{{^isFreeFormObject}}*{{/isFreeFormObject}}{{/isArray}}{{/isNullable}}{{/required}}{{{dataType}}} json:"{{{baseName}}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{{baseName}}}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}
This specific line is from modules/openapi-generator/src/main/resources/go/model_simple.mustache

Copy link
Contributor

@icubbon icubbon left a comment

Choose a reason for hiding this comment

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

Examples should be added/edited to cover the new variations of nullable bools, nullable enums, nullable numbers, etc.

Comment on lines +508 to +518
} else {
{{#required}}
c.errorHandler(w, r, &RequiredError{Field: "{{baseName}}"}, nil)
return
{{/required}}
{{^required}}
{{#defaultValue}}
param := {{^isString}}{{dataType}}({{/isString}}{{defaultValue}}{{^isString}}){{/isString}}
{{paramName}}Param = {{#isNullable}}&{{/isNullable}}param
{{/defaultValue}}
{{/required}}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not a fan of having empty else clauses in code. If a parameter isn't required and doesn't have a default value, this case will create an empty else clause.

Copy link
Contributor Author

@lwj5 lwj5 Dec 5, 2023

Choose a reason for hiding this comment

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

I understand that you are not a fan of empty else block, but is purely cosmetic and does not affect the compiled code and logic. Feel free to suggest fixes or create a PR to update it

Comment on lines +275 to 283
{
param, err := ReadFormFileToTempFile(r, "file")
if err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}

fileParam = param
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This block of code is odd as it is using { to just scope a variable. I have not been of fan of these as they are not common practice and are typically used in a "clever" way to solve a problem.
I know it's a supported feature of the Go language, but it is typically abused in my experience.

Copy link
Contributor Author

@lwj5 lwj5 Dec 5, 2023

Choose a reason for hiding this comment

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

I understand that this is uncommon, but this is purely cosmetic and does not affect the semantics of the code.

This will need to be fixed with the larger problem as stated above #17051 (comment)

wing328 and others added 12 commits December 5, 2023 22:14
* fix null request body NPE

* fix typo
…ols#17057)

* Remove checks for jackson and jsonb from okhttp-gson templates

* regenerate samples
* retain deprecated in allof schema handling

* add test
* added handling for boolean content type

* adapted addition to newer version of template

* [python] updated samples
* Update base image of Dockerfile

* Make github workflow "Docker tests" manually triggerable

* Don't possibly downgrade TLS version

* Update Dockerimage in ``run-in-docker.sh``

* Use Maven's non interactive mode inside workflow

* Don't spam log

* Use java 17

because it won't compile with 21

* Removed hard memory limit

as memory should be controlled by the container

* Update hub dockerfiles
…Tools#16918)

* add socks5 proxy support (requires additional import)

* updated examples

* build samples. updated to support pydantic python option

* rename sock to socks for correct protocol name

* add proxy headers for pydantic

* fixed param changes from conflict resolution
For model classes with model_something fields, pydantic raises a warning by default:
`Field "model_something" has conflict with protected namespace "model_".`.

These warnings make no sense here, because most users of the generator have established APIs
that they cannot change to conform to pydantic's safety rules.

Pydantic will raise an error if we ever conflict with a current attribute like `model_dump`.
* [typescript-axios] Upgrade to axios@^1

* Try fixing tests
* added .net8

* change .net version in the github action

* upgrade manual sample
jonasheschl and others added 8 commits December 5, 2023 22:14
…APITools#17077)

* Add passgenau digital logo

* Add passgenau digital to users

* Add wemakeai logo

* Add we-make.ai to users
* refactor: Clean up _response_types_map formatting

It matches black's behavior of having trailing commas now.

* test: Add test to reproduce OpenAPITools#16967

* fix: deserialize responses even if no returnType

Closes OpenAPITools#16967

* refactor: Simplify ApiException subclasses

* refactor: Move exception subtype choice to ApiException

* feat: Deserialize error responses and add to exceptions

* test: Add for error responses with model
* add bearer auth API to echo-api

* run generate-samples.sh

* add resttemplate echo-api sample

* add bearer auth test

* remove @ignore
* use jdk17 images in Dockerfile

* use amazoncorretto:17.0.8-alpine3.18
@lwj5
Copy link
Contributor Author

lwj5 commented Dec 5, 2023

Moving to another PR due to bad merge #17321

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet