Skip to content

Suggestion: separate generated structs for different param types #1115

@tomhutch

Description

@tomhutch

Hi, I think oapi-codegen might benefit from having a bit more modularity with generated parameters. Having separate structs for different parameter types (e.g. headers vs cookies etc) might allow more flexibility to use the type generation without requiring users to use the server code generation also. I'm personally more interested in ensuring accuracy of API docs than reducing the writing of boilerplate and prefer a greater level of control and brevity in my http handlers. So I'd like the option to take or leave certain parts of the generation functionality.

If we had separate structs for different parameter types, then I believe we could leverage json tags without the risk of accidentally overwriting attributes. For example:

func httpHeadersToOAPIHeaderParams(headers http.Header) (models.PostHeaderParams, error) {
	headersMap := make(map[string]string)
	for key := range headers {
		if len(headers[key]) > 0 {
			headersMap[key] = headers[key][0]
		}
	}
	headersJSON, err := json.Marshal(headersMap)
	if err != nil {
		return models.PostHeaderParams{}, fmt.Errorf("json.Marshal headers: %w", err)
	}
	var params models.PostHeaderParams
	err = json.Unmarshal(headersJSON, &params)
	if err != nil {
		return models.PostHeaderParams{}, fmt.Errorf("json.Unmarshal models.PostHeaderParams: %w", err)
	}
	return params, nil
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions