Skip to content

feat: add sum type parameter support#1581

Merged
ernado merged 3 commits intoogen-go:mainfrom
lanej:feature/sum-type-parameters
Nov 25, 2025
Merged

feat: add sum type parameter support#1581
ernado merged 3 commits intoogen-go:mainfrom
lanej:feature/sum-type-parameters

Conversation

@lanej
Copy link
Contributor

@lanej lanej commented Nov 23, 2025

Summary

Adds support for OpenAPI 3.x oneOf and anyOf sum types in parameters - path, query, header, and cookie. Previously sum types only worked in request/response bodies.

Note: allOf is for schema merging (intersection), not sum types, and already worked.

Fixes lanej#3

Changes

  • Enable sum type validation for parameters in gen_parameters.go
  • Add URI encoding using type discriminator switch
  • Add URI decoding using fallback strategy (try each variant in order)
  • Add test coverage for path and query parameters

Example

Before: Generation failed with "sum type parameter not implemented"

After: Works seamlessly

parameters:
  - name: id
    in: path
    schema:
      anyOf:
        - type: string
          format: uuid
        - type: integer

Generated code:

// Decoding tries each variant in order
var failures []error
if err := decodeAsUUID(); err == nil {
    return nil
} else {
    failures = append(failures, err)
}
if err := decodeAsInt(); err == nil {
    return nil
} else {
    failures = append(failures, err)
}
return errors.Join(failures...)

Testing

  • Path parameter with anyOf: [uuid, integer]
  • Query parameter with anyOf: [string, integer]
  • All existing tests pass

@ernado
Copy link
Member

ernado commented Nov 24, 2025

Please rebase and re-generate

Implement support for oneOf, anyOf, and allOf sum types in path,
query, header, and cookie parameters. Previously sum types only
worked in request/response bodies.

Changes:
- gen_parameters.go: Enable sum type validation in isParamAllowed()
  by recursively validating each variant instead of returning
  ErrNotImplemented
- uri/encode.tmpl: Add encoding via discriminator-based type switch
- uri/decode.tmpl: Add decoding via fallback strategy that tries
  each variant in order until one succeeds

Closes #3
Add comprehensive test coverage for sum type parameter support:
- Update gen_test.go to remove 'sum type parameter' from unimplemented
  features list (now implemented)
- Add basic generation test with path/query parameter examples
- Add integration test with default error responses

Test scenarios:
- Path parameter with anyOf: [uuid, integer]
- Query parameter with anyOf: [string, integer]
- Both required and optional parameter cases
@lanej lanej force-pushed the feature/sum-type-parameters branch from c0b41c3 to ef36d29 Compare November 24, 2025 19:19
The GitHub OpenAPI spec generated slightly different validation code.
This is expected when rebasing onto a newer base with template changes.
@ernado ernado merged commit d688263 into ogen-go:main Nov 25, 2025
15 checks passed
lanej added a commit to lanej/ogen that referenced this pull request Nov 25, 2025
These files were incorrectly deleted - they test sum type parameters
(feature from PR ogen-go#1581), not sum type discrimination (this PR).

Restored files:
- _testdata/positive/sum_type_params.yaml
- internal/integration/test_sum_params/test_sum_params.yaml
lanej added a commit to lanej/ogen that referenced this pull request Nov 25, 2025
These test files were incorrectly restored. They belong to PR ogen-go#1581 (sum type
parameters) which hasn't been fully merged into this branch yet. Removing
them to fix test failures.
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.

Support OpenAPI 3.x sum types (oneOf, anyOf, allOf)

2 participants