Skip to content

Primitive-based types are not reused in the generated OAS. #560

@superstas

Description

@superstas

Hey there!

I've got the following types

var _ huma.SchemaTransformer = CustomHeader("")

type CustomHeader string

func (h CustomHeader) TransformSchema(r huma.Registry, s *huma.Schema) *huma.Schema {
	s.Type = "string"
	s.Description = "Custom header Description. The CustomHeader schema should be reused across all endpoints that use it."
	return s
}

type CreateUser struct {
	Body struct {
		Name string `json:"name" doc:"The name."`
	}

	CustomHeader CustomHeader `header:"X-Custom-Header"`
}

type UpdateUser struct {
	Body struct {
		Name string `json:"name" doc:"The name."`
	}

	CustomHeader CustomHeader `header:"X-Custom-Header"`
}

type Output struct {
	Body struct {
		Message string `json:"message" doc:"The message."`
	}
}

With the following routes

	huma.Post(api, "/user", func(ctx context.Context, req *CreateUser) (*Output, error) {
		resp := &Output{}
		resp.Body.Message = "It works!"
		return resp, nil
	})

	huma.Patch(api, "/user/{id}", func(ctx context.Context, req *UpdateUser) (*Output, error) {
		resp := &Output{}
		resp.Body.Message = "It works!"
		return resp, nil
	}}

I expect that CustomHeader would be re-used via $ref in the generated OAS.

Instead the CustomHeader schema is embedded into all the endpoints

paths:
  /user:
    post:
      operationId: post-user
      parameters:
        - description: Custom header Description. The CustomHeader schema should be reused across all endpoints that use it.
          in: header
          name: X-Custom-Header
          schema:
            description: Custom header Description. The CustomHeader schema should be reused across all endpoints that use it.
            type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/CreateUserBody"
        required: true
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/OutputBody"
          description: OK
        default:
          content:
            application/problem+json:
              schema:
                $ref: "#/components/schemas/ErrorModel"
          description: Error
      summary: Post user
  /user/{id}:
    patch:
      operationId: patch-user-by-id
      parameters:
        - description: Custom header Description. The CustomHeader schema should be reused across all endpoints that use it.
          in: header
          name: X-Custom-Header
          schema:
            description: Custom header Description. The CustomHeader schema should be reused across all endpoints that use it.
            type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/UpdateUserBody"
        required: true
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/OutputBody"
          description: OK
        default:
          content:
            application/problem+json:
              schema:
                $ref: "#/components/schemas/ErrorModel"
          description: Error
      summary: Patch user by ID

Can an operation be registered to force Huma to reuse shared schemas?

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions