Skip to content

[Go] [Bug] Check for required parameters is invalid #3459

@neilotoole

Description

@neilotoole
Description

The generated code for API calls with required parameters is largely invalid. There's a few (bad) different things going on. Let's take a look. Here's a generated snippet:

func (a ProjectApi) CreateProject(project Project) (*Project, *APIResponse, error) {

    var httpMethod = "Post"
    // create path and map variables
    path := a.Configuration.BasePath + "/projects"

    // verify the required parameter 'project' is set
    if &project == nil {
        return new(Project), nil, errors.New("Missing required parameter 'project' when calling ProjectApi->CreateProject")
    }

Note that structs are passed by value, not reference (otherwise it would be CreateProject(project *Project). Therefore the check if &project == nil { is meaningless... the supplied project parameter will, at a minimum, be the zero value for that struct type, therefore &project can never evaluate to nil.

Let's take another example:

func (a ArtifactApi) GetArtifacts(executionId int32) ([]Artifact, *APIResponse, error) {

    var httpMethod = "Get"
    // create path and map variables
    path := a.Configuration.BasePath + "/artifacts"

    // verify the required parameter 'executionId' is set
    if &executionId == nil {
        return new([]Artifact), nil, errors.New("Missing required parameter 'executionId' when calling ArtifactApi->GetArtifacts")
    }

In this case, this code is invalid Go: if &executionId == nil {.

Additionally, this is invalid: return new([]Artifact) ..., as it returns a pointer to a slice (*[]Artifact), instead of a slice .

Swagger-codegen version

master, but I suspect it's been around for a while.

Swagger declaration file content or url

Unfortunately I can't share the specific swagger, but at a minimum, this should happen with any swagger operation that takes a required parameter that is an int, or a struct, or returns an array.

Suggest a Fix

Validating required parameters is non-trivial in Go, due to the nature of the language. The basic outline of a solution is to use pointers everywhere, or to use interfaces. Both of these would require significant rework and any such changes should be planned and designed carefully.

Currently the generator is completely broken: we need to remove the section of code that checks for required parameters, and revisit the whole topic. I create a PR shortly.

Also, we need to have automated testing of generated Go clients. The PRs that introduced these breaking changes should have been caught (just by the Go compiler alone).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions