Skip to content

Fix: prevent duplicate case statements in response type switches#1582

Merged
ernado merged 1 commit intoogen-go:mainfrom
lanej:fix/issue-1-duplicate-type-switch-cases
Nov 24, 2025
Merged

Fix: prevent duplicate case statements in response type switches#1582
ernado merged 1 commit intoogen-go:mainfrom
lanej:fix/issue-1-duplicate-type-switch-cases

Conversation

@lanej
Copy link
Contributor

@lanej lanej commented Nov 23, 2025

Summary

Fixes duplicate case compilation errors when multiple response patterns share the same schema.

Fixes #1

Problem

When an OpenAPI spec defines multiple response patterns (2XX, 3XX) using the same schema, ogen generated duplicate case statements in type switches, causing compilation failures:

./oas_response_encoders_gen.go:42:7: duplicate case *ResponseStatusCode in type switch
	./oas_response_encoders_gen.go:17:7: previous case

Solution

Added uniqueResponseTypes() function that deduplicates response types by Type.Name before generating switch cases.

This is safe because:

  • Ogen's type caching ensures responses with the same Type.Name are identical
  • Runtime status code differences are handled via the StatusCode field, not separate cases
  • All responses with matching type names have identical encoding logic

Example

Before: Generated duplicate cases (compilation error)

switch response := response.(type) {
case *ResponseStatusCode:  // 2XX
    // ...
case *ResponseStatusCode:  // 3XX - duplicate!
    // ...
}

After: Single case handles both patterns

switch response := response.(type) {
case *ResponseStatusCode:
    code := response.StatusCode  // runtime value: 200, 201, 301, etc.
    w.WriteHeader(code)
    // ...
}

Changes

  • Added uniqueResponseTypes() deduplication function in gen/templates.go
  • Updated gen/_template/response_encode.tmpl to use deduplication
  • Added integration test reproducing the exact issue scenario

Testing

  • New integration test with 2XX/3XX patterns sharing schema
  • Test verifies generated code compiles (bug prevented compilation)
  • All existing tests pass
  • Manually verified with reproduction case from issue

@lanej lanej closed this Nov 23, 2025
@lanej lanej reopened this Nov 23, 2025
Prevents duplicate case statements when multiple response patterns
(e.g., 2XX, 3XX) share the same schema reference. Previously, this
caused Go compilation errors due to duplicate cases in type switches.

The fix introduces uniqueResponseTypes() to filter responses by
Type.Name before generating case statements. This is safe because
responses with identical type names have the same encoding logic.

Includes integration test to prevent regression.

Fixes #1
@lanej lanej force-pushed the fix/issue-1-duplicate-type-switch-cases branch from 891a270 to 2ea0f36 Compare November 23, 2025 22:43
@ernado ernado merged commit 582fd84 into ogen-go:main Nov 24, 2025
15 checks passed
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.

2 participants