Repository navigation
Report media type, encoding, header, link and scheme names that are not strings as server and client schema errors - #5281
Conversation
…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
|
No actionable comments were generated in the recent review.
|
| 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:
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
- datamodel-code-generator/datamodel-code-generator#5230: Both changes report non-string OpenAPI mapping names as located schema errors.
- datamodel-code-generator/datamodel-code-generator#5250: Both changes add schema-path diagnostics for non-string mapping names in OpenAPI processing.
- datamodel-code-generator/datamodel-code-generator#5254: Both changes report non-string OpenAPI mapping keys as located schema errors.
Merge Risk: 
Minimal · up to a3b0b
-
The key-error change appears mergeable after normal checks; no material regression was established.
Pre-merge checks: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passed-
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
#5260requires schema-path errors instead of crashes for non-string media-type, header, and encoding-property keys. The implementation validates mapping keys throughnamedand 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_typeensures 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.
Comment @coderabbitai help to get the list of available commands.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour 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. |
There was a problem hiding this comment.
💡 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".
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Breaking Change AnalysisResult: 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 |
Fixes: #5260
Summary by CodeRabbit