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

Make response bodies more prominent than response headers #2999

Open
radeksimko opened this issue Dec 15, 2023 · 0 comments
Open

Make response bodies more prominent than response headers #2999

radeksimko opened this issue Dec 15, 2023 · 0 comments

Comments

@radeksimko
Copy link

Problem statement

This may look in conflict with #2998 but I don't think it necessarily is.

When I first used go-swagger and looked at the generated code, I was surprised about the response body being nested under Payload field.

I find it unintuitive to work with such generated code in the context of clients which interact with APIs that do not produce any headers. The consumer side code ends up littered with .Payload in many places. This may look more benign if the response format is primitive type like string but I'd assume in many cases it's going to be some more complex structure and even in the case of primitive type, the name Payload and the nesting does not add any value there.

Even for the other use cases which do involve headers, the headers seem to be surfaced far more prominently (as first class struct fields) in favour of the body structure, while it's IMO usually the body that the user ends up needing more (subject to the above) than the headers.

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": "array",
                            "items": {
                                "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_responses.go

type ListProductsOK struct {
	Payload []string
}

Then imagine any additional code leveraging the above in the form of

resp, err := client.ListProducts(...)
if err != nil {
	// deal with err
}
// deal with the list
resp.Payload

Environment

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

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

No branches or pull requests

2 participants