Skip to content

Report media type, encoding, header, link and scheme names that are not strings as server and client schema errors - #5281

Merged
Koudai Aono (koxudaxi) merged 1 commit into
review-fix-8from
fix-5260-target-key-errors
Oct 24, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
review-fix-8from
fix-5260-target-key-errors

Conversation

@koxudaxi

@koxudaxi Koudai Aono (koxudaxi) commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

Fixes: #5260

Summary by CodeRabbit

  • Bug Fixes
    • Schema generation now reads media schemas regardless of media type, improving handling of OpenAPI definitions with less common media types.
    • Invalid non-text keys in encoding properties, headers, response links, and security schemes now produce clearer errors that identify their location in the schema.
    • Generation stops without writing output when these malformed keys are detected, for both client and server targets.

…ot strings as server and client schema errors

A YAML key such as `1` loads as a number. Server and client generation locate every medium by its media type, every
property of an encoding that applies and every header it declares, every link and every security scheme by their
keys, so such a key crashed with a TypeError or AttributeError there even where model generation keeps its output: a
media type whose schema is a plain reference or that has no schema, an encoding property without headers, a header of
a form encoding, a link name or a security scheme name. These runs now stop with "Error at schema path
'<doc>#/paths/~1pets/post/requestBody/content/1': media type 1 is not a string" ("encoding property name", "header
name", "link name", "component name"), as model generation reports such keys. Model output, and server and client
output of documents whose keys are strings, stay the same.

Fixes: #5260
@coderabbitai

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between afc8734 and a3b0b66.

Files ignored due to path filters: 5
  • tests/data/malformed/integer_encoding_property_name_without_headers.yaml is excluded by !tests/data/**/*.yaml and included by none
  • tests/data/malformed/integer_form_encoding_header_name.yaml is excluded by !tests/data/**/*.yaml and included by none
  • tests/data/malformed/integer_link_name.yaml is excluded by !tests/data/**/*.yaml and included by none
  • tests/data/malformed/integer_media_type_reference.yaml is excluded by !tests/data/**/*.yaml and included by none
  • tests/data/malformed/integer_security_scheme_name.yaml is excluded by !tests/data/**/*.yaml and included by none
Files selected for processing: 3
  • src/datamodel_code_generator/_target_binding.py
  • tests/api_generation/test_generate_targets.py
  • tests/api_generation/test_generate_targets.refs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.

Walkthrough

Target binding now reads media schemas for all media types and validates selected metadata mapping keys. Tests cover malformed keys in server and client generation and verify that generation writes no output.

Changes: OpenAPI target key validation

Layer / File(s) Summary
Target binding key handling
src/datamodel_code_generator/_target_binding.py
Target binding reads every media type. It validates encoding property, header, response link, and component security-scheme keys, reporting non-string keys with their holder location.
Server and client error coverage
tests/api_generation/test_generate_targets.py, tests/api_generation/test_generate_targets.refs
Parameterized tests cover malformed media type and metadata keys. They check the schema-path error and confirm that generation writes no output.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • Numeric header, media-type, and encoding keys in OpenAPI API processing are reported to cause Python crashes. This change adds schema errors for non-string keys in target binding.

Possibly related PRs

Merge Risk: Minimal · up to a3b0b

The key-error change appears mergeable after normal checks; no material regression was established.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 76.92% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 13 functions across 2 files. (1 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title states the main change: media type, encoding, header, link, and security scheme names that are not strings now produce server and client schema errors instead of crashing. The raw summary co…
Linked Issues check Passed Issue #5260 requires schema-path errors instead of crashes for non-string media-type, header, and encoding-property keys. The implementation validates mapping keys through named and reports the pars…
Out of Scope Changes check Passed The changes stay within the linked issue scope. _reads_media_type ensures media schemas are read so invalid media-type keys are reported. The added tests verify the server and client error behavior.…
Full details: Title check

Explanation

The title states the main change: media type, encoding, header, link, and security scheme names that are not strings now produce server and client schema errors instead of crashing. The raw summary confirms this behavior, including the key validation through named, the new key-error path, and the added tests for five malformed fixtures across server and client targets. The title also matches the linked issue #5260, which asks for an Error at schema path message for non-string keys. The title is long at 114 characters, but it stays specific and on-topic.

Full details: Linked Issues check

Explanation

Issue #5260 requires schema-path errors instead of crashes for non-string media-type, header, and encoding-property keys. The implementation validates mapping keys through named and reports the parser key error with the holder location. The same validation covers response links and component security-scheme names described in the PR objectives. The parameterized server and client tests cover five malformed fixtures and verify schema-path errors with no generated output.

Full details: Out of Scope Changes check

Explanation

The changes stay within the linked issue scope. _reads_media_type ensures media schemas are read so invalid media-type keys are reported. The added tests verify the server and client error behavior. No unrelated production behavior or unrelated tests are identified.

Full details: Docstring Coverage

Explanation

Docstring coverage is 76.92% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 13 functions across 2 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-24T02:17:38.042468Z a3b0b66 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a3b0b66bd9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

@github-actions

Copy link
Copy Markdown
Contributor

@codspeed

codspeed Bot commented Oct 24, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

✅ 50 untouched benchmarks
⏩ 98 skipped benchmarks1


Comparing fix-5260-target-key-errors (a3b0b66) with review-fix-8 (afc8734)

Open in CodSpeed

Footnotes

  1. 98 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@codecov

codecov Bot commented Oct 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@koxudaxi
Koudai Aono (koxudaxi) merged commit 92a338f into main Oct 24, 2026
131 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the fix-5260-target-key-errors branch October 24, 2026 07:54
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: The PR only changes the private module _target_binding.py and adds tests and malformed fixtures. It adds a helper named that raises a schema Error when a non-string key such as a YAML integer key is used as an encoding property name, header name, link name, or security scheme name in server or client target generation. It also overrides _reads_media_type so that non-string media type keys are rejected in the same way. These inputs are malformed OpenAPI documents, because the spec requires string keys, and the test docstring says model generation already refuses such keys. So this brings the target bindings in line with existing validation and turns a crash or undefined behavior into a clear error. No public CLI options, Python API, templates, defaults, or generated output for valid schemas change, and the deleted lines only swap direct mapping iteration for the new validating helper, so there are no breaking changes.


This analysis was produced by the release draft workflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Server/client generation crashes on numeric media-type keys, header names and encoding property names

1 participant