Skip to content

Suggestion: separate generated structs for different param types #1115

Open

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
}

Activity

  1. added
    enhancement
    on Jul 31, 2023
  2. added this to the v2.0.0 milestone on Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions