Skip to content

fix: document error codes added by group modifiers - #1090

Open
stareezy-1 wants to merge 1 commit into
danielgtaylor:mainfrom
stareezy-1:fix/878-modifier-errors
Open

fix: document error codes added by group modifiers#1090
stareezy-1 wants to merge 1 commit into
danielgtaylor:mainfrom
stareezy-1:fix/878-modifier-errors

Conversation

@stareezy-1

Copy link
Copy Markdown

Summary

Fixes #878

Problem

When a group modifier appends to op.Errors, the generated OpenAPI document does not include the new error responses — while Tags, Security, and Middlewares changes from the same modifier are reflected correctly.

Root cause: huma.Register called defineErrors (which consumes op.Errors into op.Responses) before invoking OperationDocumenter, where group modifiers run. The other fields are read directly from the operation by AddOperation after the modifiers, which is why they worked.

Reproduction:

internalApi.UseSimpleModifier(func(op *huma.Operation) {
	op.Tags = append(op.Tags, "Internal")        // appears in the doc ✓
	op.Errors = append(op.Errors, 401)           // missing from the doc ✗
})

Fix

Move the error definition to the innermost document step — after all group modifiers have run and immediately before AddOperation:

  • Register (plain API path): call defineOperationErrors right before AddOperation.
  • Group.DocumentOperation: call it at the end of the modifier chain, before delegating or adding.
  • New defineOperationErrors helper preserves the previous 422/500 auto-append behavior, using the documented operation state (op.RequestBody, op.Parameters) as the equivalent condition.

Tests

TestGroupModifierErrorsDocumented registers an operation in a group whose modifier adds 401 and asserts:

Response Without fix With fix
401 (modifier-added) missing present
422 / 500 (auto-appended) present present
Tags from modifier present present

All existing tests continue to pass (go test -race ./...).

defineErrors ran before OperationDocumenter, so error codes appended by
group modifiers were never written into op.Responses, while Tags, Security,
and Middlewares (read directly by AddOperation) were. Move the error
definition to the innermost document step, after all modifiers have run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't set the Operation's Errors in a group modifier

1 participant