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

Context as the first argument instead of parameter struct field #2997

Open
radeksimko opened this issue Dec 15, 2023 · 1 comment
Open

Context as the first argument instead of parameter struct field #2997

radeksimko opened this issue Dec 15, 2023 · 1 comment
Labels
enhancement generator runtime Relates to github.com/go-openapi/runtime

Comments

@radeksimko
Copy link

Problem statement

As you may be aware, it is a documented best practice in Go for context.Context to be passed as a first argument to a function, rather than a field in a struct.

The current implementation does not follow this practice, as shown below.

I appreciate that the project may have a long history which predates this practice and maybe even wider context adoption where the current implementation was a way of making context optional - which is all totally fair! I don't think that in most "modern" libraries context is optional anymore. If the library is making a request to an API, the consumer needs to have a way of cancelling it because it rarely makes sense to wait forever or for some internal default timeout.

I also understand that there is cost of making such a change now as it implies breaking stuff for existing users. However, I wonder if this could possibly be gated behind a feature (CLI) flag of some kind and/or rolled out eventually as default in v1 in the future, where users would more likely expect breaking changes?

Swagger specification

{
    "info": {
        "title": "Example",
        "version": "0.0.1"
    },
    "swagger": "2.0",
    "host": "example.com",
    "paths": {
        "/v1/products": {
            "get": {
                "summary": "List product names",
                "operationId": "listProducts",
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    }
}

Steps to reproduce

Generate client code

go run github.com/go-swagger/go-swagger/cmd/swagger generate client

Open ./client/operations/operations_client.go

// ...
func (a *Client) ListProducts(params *ListProductsParams, opts ...ClientOption) (*ListProductsOK, error) {
// ...

Open ./client/operations/list_products_parameters.go

type ListProductsParams struct {
	timeout    time.Duration
	Context    context.Context
	HTTPClient *http.Client
}

Environment

swagger version: v0.30.5
go version: go1.21.5
OS: darwin/arm64

@fredbi fredbi added enhancement generator runtime Relates to github.com/go-openapi/runtime labels Dec 23, 2023
@VyrCossont
Copy link

To add to this, Context is a perfectly legal parameter name (as are Timeout and HTTPClient). Putting anything in the parameter struct other than the actual parameters to the operation can result in name conflicts with both parameters and their accessor functions, and in fact I just ran into that problem: I have client code that won't compile because the API defines a context param, and thus Context, WithContext, and SetContext are all defined twice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement generator runtime Relates to github.com/go-openapi/runtime
Projects
None yet
Development

No branches or pull requests

3 participants