Skip to content

Fix IndentationError when a subschema generates no validation code - #206

Merged
horejsek merged 1 commit into
horejsek:masterfrom
gaoflow:fix-empty-body-codegen
Jul 25, 2026
Merged

Fix IndentationError when a subschema generates no validation code#206
horejsek merged 1 commit into
horejsek:masterfrom
gaoflow:fix-empty-body-codegen

Conversation

@gaoflow

@gaoflow gaoflow commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

compile() generates invalid Python for some valid schemas:

>>> fastjsonschema.compile({"not": {"title": "x"}})
IndentationError: expected an indented block after 'try' statement on line 4

Any truthy subschema that produces no validation code triggers this: annotation-only schemas (title, description, $comment, ...), unknown keywords, or no-op keywords such as {"not": false}, {"uniqueItems": false}, or an unrecognized format (a no-op since 63e29e3). The generator emits a block header with nothing under it.

This is the same class as #204/#205, so I probed every subschema position of every generator with code-less subschemas across drafts 04/06/07/2019. Seven sites are affected:

  • not (try body) — unguarded
  • then and else of if-then-else — 263ecad guarded only the if block
  • schema-form dependencies — unguarded
  • single-schema items, additionalItems, propertyNames — guarded, but by the number of keyword functions run (the return value of generate_func_code_block), which misses keywords that run and emit nothing: {"items": {"not": false}} still crashed

The fix applies the code_len snapshot pattern from 263ecad at all seven sites, replacing the count-based guards. The remaining generators (anyOf/oneOf/contains/properties/patternProperties/additionalProperties) always emit at least one body line and were verified unaffected.

This also fixes the compile crash on the suite's optional/unknownKeyword.json — three of its cases now pass, but the file has to stay in ignored_suite_files because its other cases need $ref-to-embedded-$id resolution, which is a separate gap.

Independent of #181, which fixes a different generate_not bug (not: {} accepting falsy instances); the two compose.

@horejsek
horejsek merged commit 2a8955c into horejsek:master Jul 25, 2026
@horejsek

Copy link
Copy Markdown
Owner

Thank you!

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