Skip to content

Request bodies with required: false should have an optional argument for body #2194

@jamietanna

Description

@jamietanna

For instance, given:

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Generate models
paths:
  /client:
    put:
      operationId: updateClient
      requestBody:
        required: false
        content:
          application/json:
            schema:
              type: object
              properties:
                foo:
                  type: string

oapi-codegen v2.5.0 will generate:

package client

// ...

type UpdateClientJSONBody struct {
	Foo *string `json:"foo,omitempty"`
}

// NOTE that this is not optional
type UpdateClientJSONRequestBody UpdateClientJSONBody

type ClientInterface interface {
    // NOTE that this is not an optional body
	UpdateClientWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error)

    // NOTE that this is not an optional body
	UpdateClient(ctx context.Context, body UpdateClientJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error)
}

It would be better if we detected required: false and instead i.e.

type UpdateClientJSONRequestBody *UpdateClientJSONBody

type ClientInterface interface {
    // A body can be set from raw data
	UpdateClientWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error)

    // A body can be set as its struct
	UpdateClient(ctx context.Context, body *UpdateClientJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error)
}

As a breaking change to currently generated code, this would be via an output-options.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions