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] Parse int arrays and respect the 'required' property #9120

Merged
merged 7 commits into from
Apr 11, 2021

Conversation

aliakseiz
Copy link
Contributor

*Affects the Go-server only.

Integer arrays support in a form

Adds a code generation to parse form parameter fields of int32 array and int64 array types.

post:
  summary: Test operation
  operationId: postTest
  requestBody:
    description: New array
    content:
      multipart/form-data:
        schema:
          type: object
          properties:
            Test:
              type: array
              items:
                type: integer
                format: int64

Before:

test, err := parseInt64Parameter( r.FormValue("Test"))

After:

// "," - delimiter to parse an array
// false - state of `required` property
test, err := parseInt64ArrayParameter(r.FormValue("Test"), ",", false)

Required integer fields

Also parameter's required property is not ignored anymore, meaning that missing optional parameters of int32, int64, []int32 and []int64 types are initialized with 0 and empty slice respectively, instead of failing the whole operation.
Before/after below assume, that mentioned optional and required fields are missing in the payload.
Before:

// Optional field
id, err := parseInt64Parameter(params["id"])
if err != nil { // <-- err is not nil, because empty string cannot be parsed as int64. Operation fails
    w.WriteHeader(http.StatusBadRequest)
    return
}

After:

// Optional field
id, err := parseInt64Parameter(params["id"], false)
if err != nil { // <-- err is nil, id is 0. Operation continues
    w.WriteHeader(http.StatusBadRequest)
    return
}

// Required field
idReq, err := parseInt64Parameter(params["idReq"], true)
if err != nil { // <-- err is not nil, contains `errMsgRequiredMissing` message. Operation fails, as expected
    w.WriteHeader(http.StatusBadRequest)
    return
}

Multipart/form-data parsing

Replaces form parsing with more specific call for multipart/form-data. ParseForm itself doesn't initialize the Files parameters.

FileParam:
  type: object
  properties:
    Files:
      description: Some files
      type: array
      items:
        type: string
        format: binary

Before:

err := r.ParseForm()
if err != nil {
    w.WriteHeader(http.StatusBadRequest)
    return
}

After:

if err := r.ParseMultipartForm(32 << 20); err != nil {
    w.WriteHeader(http.StatusBadRequest)
    return
}

Other

Fixes minor typos and FIXMEs, which caught my eye during debugging.

Technical committee

@antihax @grokify @kemokemo @jirikuncar

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/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, 5.1.x, 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@aliakseiz
Copy link
Contributor Author

Can somebody please restart the CircleCI job?
Seems, that issue causing the build to fail is resolved already in #9122 .

…-array

# Conflicts:
#	modules/openapi-generator/src/main/resources/go-server/controller-api.mustache
#	modules/openapi-generator/src/main/resources/go-server/routers.mustache
#	samples/server/petstore/go-api-server/go/api_pet.go
#	samples/server/petstore/go-api-server/go/api_store.go
#	samples/server/petstore/go-api-server/go/api_user.go
#	samples/server/petstore/go-api-server/go/routers.go
@wing328
Copy link
Member

wing328 commented Apr 6, 2021

All tests passed. If no further feedback, I'll merge it in the next few days.

@wing328 wing328 merged commit 53e5986 into OpenAPITools:master Apr 11, 2021
@aliakseiz aliakseiz deleted the feature/parse-int-array branch April 11, 2021 16:11
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.

None yet

2 participants