Skip to content

Fix voluptuous-openapi LazySchema unhashable type bug#96

Merged
balloob merged 1 commit into
home-assistant-libs:masterfrom
allenporter:fix/lazyschema-unhashable-type
Jun 25, 2026
Merged

Fix voluptuous-openapi LazySchema unhashable type bug#96
balloob merged 1 commit into
home-assistant-libs:masterfrom
allenporter:fix/lazyschema-unhashable-type

Conversation

@allenporter

@allenporter allenporter commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Rationale

This PR addresses a TypeError: cannot use 'voluptuous_openapi.LazySchema' as a dict key bug that occurs when converting voluptuous schemas containing references back to OpenAPI. It also introduces proper reference denormalization and cycle-breaking mechanisms during serialization.

Proposed Changes

  1. LazySchema Hashability: Implemented a __hash__ method in LazySchema returning hash(self.ref) to satisfy Python's hash contract while keeping the mutable root_schema dictionary out of the hash calculation.
  2. Robust Callable Inspection: Updated the type hint extraction in convert() to check explicitly whether the callable is a class/routine or a custom callable instance (such as LazySchema), avoiding TypeError exceptions during type hint inspection.
  3. Reference Denormalization:
    • When converting LazySchema objects back to OpenAPI, we now resolve and fully denormalize the target schema, rather than returning an incomplete/unresolvable {"$ref": ...} block without definitions.
    • Added a private _seen_refs: set[str] parameter to convert() to track active recursion paths.
    • Circular references/cycles are broken at the recursive boundary by returning {} (representing Any, which defaults to a string type via ensure_default), preventing infinite recursion.
  4. Testing:
    • Added test_convert_schema_with_reference to verify denormalization and validation of simple references.
    • Added test_convert_schema_with_nested_reference to verify denormalization two levels deep.
    • Added test_convert_recursive_schema to verify cycle-breaking and recursion safety.
    • Added test_convert_custom_callable_class_instance to verify that custom callable class instances are converted robustly.

@allenporter allenporter marked this pull request as draft June 25, 2026 15:06
@allenporter allenporter force-pushed the fix/lazyschema-unhashable-type branch from f2112fb to bedf19d Compare June 25, 2026 15:07
@allenporter allenporter marked this pull request as ready for review June 25, 2026 15:07
@allenporter allenporter force-pushed the fix/lazyschema-unhashable-type branch 10 times, most recently from a167150 to 09023c1 Compare June 25, 2026 15:39
@allenporter allenporter marked this pull request as draft June 25, 2026 15:43
@allenporter allenporter force-pushed the fix/lazyschema-unhashable-type branch from 09023c1 to f256676 Compare June 25, 2026 15:48
@allenporter allenporter marked this pull request as ready for review June 25, 2026 15:50
@allenporter allenporter requested review from Shulyaka and balloob June 25, 2026 15:51
@balloob balloob merged commit bb53179 into home-assistant-libs:master Jun 25, 2026
1 check 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