-
-
Notifications
You must be signed in to change notification settings - Fork 449
Cover input error messages #3537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| type Query { | ||
| user: | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| openapi: 3.0.0 | ||
| info: | ||
| title: Bad | ||
| paths: [ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"$ref": "#/definitions/Missing"} |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "type": "string", | ||
| "enum": {"one": 1} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"$ref": "missing.json"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| - not | ||
| - a | ||
| - mapping |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "definitions": { | ||
| "Name": "scalar" | ||
| }, | ||
| "$ref": "#/definitions/Name/properties/value" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| openapi: 3.0.0 | ||
| info: | ||
| title: Bad | ||
| version: '1.0' | ||
| paths: {} | ||
| components: | ||
| schemas: | ||
| Name: scalar | ||
| UseName: | ||
| $ref: '#/components/schemas/Name/properties/value' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "object", | ||
| "required": "name", | ||
| "properties": { | ||
| "name": {"type": "string"} | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"type": "object", "properties": { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "type": "object", | ||
| "properties": [] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| """Regression tests for malformed CLI input diagnostics.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| import pytest | ||
|
|
||
| from datamodel_code_generator.__main__ import Exit | ||
| from tests.main.conftest import DATA_PATH, InputFileTypeLiteral, run_main_and_assert | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| MALFORMED_DATA_PATH = DATA_PATH / "malformed" | ||
| TRACEBACK_HEADER = "Traceback (most recent call last)" | ||
|
|
||
| ERROR_CASES: tuple[tuple[str, InputFileTypeLiteral, str], ...] = ( | ||
| ("truncated_jsonschema.json", "jsonschema", TRACEBACK_HEADER), | ||
| ("bad_openapi.yaml", "openapi", TRACEBACK_HEADER), | ||
| ("bad.graphql", "graphql", TRACEBACK_HEADER), | ||
| ("non_dict_root.yaml", "openapi", TRACEBACK_HEADER), | ||
| ("pointer_through_scalar_openapi.yaml", "openapi", TRACEBACK_HEADER), | ||
| ( | ||
| "pointer_through_scalar.json", | ||
| "jsonschema", | ||
| "Error at schema path 'pointer_through_scalar.json/#/definitions/Name': ValidationError", | ||
| ), | ||
| ("wrong_type_properties.json", "jsonschema", "Error at schema path 'wrong_type_properties.json'"), | ||
| ("required_as_string.json", "jsonschema", "Error at schema path 'required_as_string.json'"), | ||
| ("enum_as_dict.json", "jsonschema", "Error at schema path 'enum_as_dict.json'"), | ||
| ("missing_external_ref.json", "jsonschema", "$ref file not found:"), | ||
| ("empty_jsonschema.json", "jsonschema", "Models not found in the input data"), | ||
| ) | ||
|
|
||
| MISSING_INPUT_CASES: tuple[tuple[InputFileTypeLiteral | None, str], ...] = ( | ||
| (None, "File not found"), | ||
| # Explicit input types currently bypass auto-detection's missing-file check. | ||
| # Keep this pinned until the diagnostic behavior is intentionally changed. | ||
| ("jsonschema", TRACEBACK_HEADER), | ||
| ) | ||
|
|
||
| TOLERATED_CASES: tuple[tuple[str, InputFileTypeLiteral], ...] = (("dangling_local_ref.json", "jsonschema"),) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("fixture_name", "input_file_type", "expected_stderr_contains"), | ||
| ERROR_CASES, | ||
| ids=[fixture_name for fixture_name, _, _ in ERROR_CASES], | ||
| ) | ||
| def test_malformed_input_error_messages( | ||
| fixture_name: str, | ||
| input_file_type: InputFileTypeLiteral, | ||
| expected_stderr_contains: str, | ||
| output_file: Path, | ||
| capsys: pytest.CaptureFixture[str], | ||
| ) -> None: | ||
| """Malformed inputs keep their current stderr signal and non-zero exit code.""" | ||
| run_main_and_assert( | ||
| input_path=MALFORMED_DATA_PATH / fixture_name, | ||
| output_path=output_file, | ||
| input_file_type=input_file_type, | ||
| expected_exit=Exit.ERROR, | ||
| capsys=capsys, | ||
| expected_stderr_contains=expected_stderr_contains, | ||
| output_should_not_exist=True, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("input_file_type", "expected_stderr_contains"), | ||
| MISSING_INPUT_CASES, | ||
| ids=[input_file_type or "auto" for input_file_type, _ in MISSING_INPUT_CASES], | ||
| ) | ||
| def test_missing_input_error_messages( | ||
| input_file_type: InputFileTypeLiteral | None, | ||
| expected_stderr_contains: str, | ||
| output_file: Path, | ||
| capsys: pytest.CaptureFixture[str], | ||
| ) -> None: | ||
| """Missing local input paths keep their current CLI diagnostics.""" | ||
| run_main_and_assert( | ||
| input_path=MALFORMED_DATA_PATH / "missing.json", | ||
| output_path=output_file, | ||
| input_file_type=input_file_type, | ||
| expected_exit=Exit.ERROR, | ||
| capsys=capsys, | ||
| expected_stderr_contains=expected_stderr_contains, | ||
| output_should_not_exist=True, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("fixture_name", "input_file_type"), | ||
| TOLERATED_CASES, | ||
| ids=[fixture_name for fixture_name, _ in TOLERATED_CASES], | ||
| ) | ||
| def test_malformed_input_tolerated_behavior( | ||
| fixture_name: str, | ||
| input_file_type: InputFileTypeLiteral, | ||
| output_file: Path, | ||
| ) -> None: | ||
| """Document malformed inputs that are intentionally tolerated until stricter diagnostics exist.""" | ||
| run_main_and_assert( | ||
| input_path=MALFORMED_DATA_PATH / fixture_name, | ||
| output_path=output_file, | ||
| input_file_type=input_file_type, | ||
| expected_exit=Exit.OK, | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.