Skip to content

TypeError: cannot use 'list' as a set element when rendering OpenAPI 3.1 schemas with array-valued type #62

@dcode

Description

@dcode

Description

Rendering any OpenAPI 3.1 schema that uses an array-valued type field (the 3.1-native way to express nullable fields) crashes with:

TypeError: cannot use 'list' as a set element (unhashable type: 'list')

Root cause

OpenAPI 3.1 replaced the nullable: true extension with multi-type arrays:

  { "type": ["string", "null"] }   // 3.1 — nullable string
  { "type": "string", "nullable": true }  // 3.0 equivalent

The Jinja2 template partial/schema-repr.html (line 17) does:

  {%- with type_name = schema["type"] -%}
  ...
  {%- if type_name in scalar_types -%}

scalar_types is a Python set({"string", "integer", "boolean", "number"}). When type_name is a list, Python's in operator attempts to hash it for set membership, raising TypeError. The same pattern exists in all four view variants (views_mkdocs, views_markdown,
views_plantuml_api, views_plantuml_schemas).

Minimal reproducer

  import json, tempfile
  from pathlib import Path
  from openapidocs.mk.v3 import OpenAPIV3DocumentationHandler
  from openapidocs.utils.source import read_from_source

  schema = {
      "openapi": "3.1.0",
      "info": {"title": "Repro", "version": "0.0.1"},
      "paths": {
          "/example": {
              "get": {
                  "summary": "Get example",
                  "responses": {
                      "200": {
                          "description": "OK",
                          "content": {
                              "application/json": {
                                  "schema": {"$ref": "#/components/schemas/Example"}
                              }
                          },
                      }
                  },
              }
          }
      },
      "components": {
          "schemas": {
              "Example": {
                  "type": "object",
                  "properties": {
                      "name": {"type": ["string", "null"]}  # OpenAPI 3.1 nullable
                  },
              }
          }
      },
  }

  with tempfile.NamedTemporaryFile(suffix=".json", mode="w", delete=False) as f:
      json.dump(schema, f)
      path = f.name

  data = read_from_source(path, Path(path).parent)
  handler = OpenAPIV3DocumentationHandler(data, source=path)
  handler.write()  # TypeError: cannot use 'list' as a set element (unhashable type: 'list')

Environment

  • essentials-openapi 1.3.0
  • Python 3.14

Suggested fix

In each schema-repr.html template, guard type_name before the set membership check, e.g.:

  {%- if type_name is string and type_name in scalar_types -%}

Or normalise type_name to a list first and iterate — which would also enable proper rendering of multi-type unions rather than silently skipping them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions