Skip to content

Conversation

@mattcoulter7
Copy link
Member

@mattcoulter7 mattcoulter7 commented Feb 6, 2026

This pull request introduces several enhancements and bug fixes to the OpenAPI Python generator, focusing on improved support for discriminated unions, import handling, and client code generation. The most significant changes include the addition of named aliases for discriminated response unions, improved propagation and deduplication of import statements, better handling of nullable and map types, and updates to client templates for compatibility with Pydantic v2. Below are the most important changes grouped by theme:

Discriminated Union and Response Alias Support:

  • Added logic to generate named alias models for discriminated response unions, allowing unions of referenced models to be emitted as named types via the new generate_response_union_alias_models function in model_generator.py. This includes common suffix detection for alias naming. (Fe7c6952L333R333)
  • Enhanced client_generator.py to prefer named aliases for discriminated unions of referenced response schemas, improving type clarity in generated clients.

Import Handling Improvements:

  • Updated type_converter in model_generator.py to propagate and deduplicate all import statements from nested schemas, ensuring correct imports for unions, arrays, and map types. [1] [2]
  • Improved handling of array and map types to merge imports from nested schemas and additional properties, supporting complex dictionary types and unions.

Client Template Updates for Pydantic v2:

  • Modified both async and sync client templates to use TypeAdapter for model validation, ensuring compatibility with Pydantic v2 and simplifying response parsing logic. [1] [2]
  • Added TypeAdapter import to client templates. [1] [2]

Schema Property Enhancements:

  • Added support for single-value enums and const values, converting them to Literal[...] types with defaults in model properties for improved type safety and clarity.

Debugging and Template Configuration:

  • Improved error logging in operation_is_sse to aid debugging of response status conversion issues.
  • Updated Jinja environment configuration to use select_autoescape, improving template safety.

These changes collectively improve the robustness and clarity of generated Python clients and models, particularly for OpenAPI schemas with complex union and discriminated types.

Adds logic to detect and generate named type aliases for discriminated response unions of references, using a common suffix for alias naming. Updates model generation to emit Literal[...] types for const and single-value enums. Refactors client templates to use Pydantic's TypeAdapter for model validation, improving compatibility with Pydantic v2.
Add openapi.json to .gitignore to prevent committing the generated OpenAPI specification file and keep the repository free of build/generated artifacts.
Add a VS Code launch configuration 'Python Debugger: Current File with Arguments' that uses debugpy to launch the current file and pass command-line arguments via ${command:pickArgs}; runs in the integratedTerminal for easier interactive debugging of scripts with parameters.
When converting array schema types, collect and merge import types from the nested item schema. Add item_imports, call type_converter on schema.items (passing model_name), append the nested converted_type, and merge/dedupe any imports from the item into the parent import_types. This ensures imports from nested constructs (e.g. Union[$ref]) are preserved for array element types.
Collect all import types from sub-schemas (then dedupe) instead of only the first, ensuring referenced imports are preserved. Special-case oneOf/anyOf unary nullable unions (ref | null) and convert them to Optional[Ref] (respecting _REFERENCE_TYPE_OVERRIDES when present). Add support for object "map" shapes using additionalProperties by emitting Dict[str, <value-type>] and merging/deduping any required imports. These changes improve type accuracy and import handling in the Python model generator.
Add debug logging when response status key conversion fails in the Python client generator to aid diagnostics. Change Jinja environment autoescape to use select_autoescape(default_for_string=False) instead of hard False to better control template escaping. Refactor the Python model generator: consolidate and reorder openapi_pydantic imports (Operation/PathItem and Response types), rename an unused loop variable to _path_name to satisfy linters, and reformat _dedupe_imports calls for readability. These are non-functional cleanup and observability improvements.
Update pyproject.toml to set the package version to 2.2.4 for a new patch release.
@mattcoulter7 mattcoulter7 merged commit 53b5010 into main Feb 6, 2026
0 of 2 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.

1 participant