Skip to content

Validate with x-oapi-codegen-extra-tags do not work for query params #2232

Closed

Description

@andre-marcos-perez

The generated code with x-oapi-codegen-extra-tags validation does not work for query params.

  • Version:

github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen v2.5.1

  • Definition:
paths:
  /v1/your/endpoint:
  get:
    - name: env
      in: query
      required: true
      schema:
      type: string
      enum:
        - dev
        - live
      x-oapi-codegen-extra-tags:
        validate: required,oneof=dev live
  • Generated code:
func (siw *ServerInterfaceWrapper) GetV1YourEndpoint(w http.ResponseWriter, r *http.Request) {

	// Parameter object where we will unmarshal all parameters from the context
	var params GetV1YourEndpointParams

	// ------------- Required query parameter "env" -------------

	if paramValue := r.URL.Query().Get("env"); paramValue != "" {

	} else {
		siw.ErrorHandlerFunc(w, r, &RequiredParamError{ParamName: "env"})
		return
	}

	err = runtime.BindQueryParameter("form", true, true, "env", r.URL.Query(), &params.Env)
	if err != nil {
		siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "env", Err: err})
		return
	}

	handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		siw.Handler. GetV1YourEndpointParams(w, r, scheduleId, params)
	}))

	for _, middleware := range siw.HandlerMiddlewares {
		handler = middleware(handler)
	}

	handler.ServeHTTP(w, r)
}

Activity

  1. mromaszewicz commented on Feb 28, 2026

    @mromaszewicz
    Member

    Thanks for reporting this! After investigating, x-oapi-codegen-extra-tags on query parameters does work — the extension is supported at both the parameter level and the schema level, and both are merged into the generated *Params struct fields.

    The YAML in the issue appears to have indentation issues, which would cause the extension to not be associated with the parameter correctly. Here are two correct ways to use it:

    At the parameter level:

    paths:
      /v1/your/endpoint:
        get:
          parameters:
            - name: env
              in: query
              required: true
              x-oapi-codegen-extra-tags:
                validate: "required,oneof=dev live"
              schema:
                type: string
                enum:
                  - dev
                  - live

    At the schema level (inside the parameter):

    paths:
      /v1/your/endpoint:
        get:
          parameters:
            - name: env
              in: query
              required: true
              schema:
                type: string
                enum:
                  - dev
                  - live
                x-oapi-codegen-extra-tags:
                  validate: "required,oneof=dev live"

    Both will produce a *Params struct field with the validate tag. If you're still seeing issues with correctly-indented YAML, please reopen with a minimal reproducible spec and the generated output.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions