-
Notifications
You must be signed in to change notification settings - Fork 35
fix: schema generation with type keyword (#84) #87
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
Open
v1ack
wants to merge
2
commits into
smagafurov:master
Choose a base branch
from
v1ack:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
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
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 |
|---|---|---|
|
|
@@ -647,7 +647,7 @@ def probe( | |
| unique_param_name=web_param_name, | ||
| ), | ||
| ) | ||
| return api_dir | ||
| return request.param | ||
|
|
||
|
|
||
| def test_component_name_isolated_by_their_path(pytester, api_package): | ||
|
|
@@ -677,25 +677,30 @@ def test_no_collide(app_client): | |
|
|
||
| paths = resp_json['paths'] | ||
| schemas = resp_json['components']['schemas'] | ||
|
|
||
| mobile_path = '/api/v1/mobile/jsonrpc/probe' | ||
| web_path = '/api/v1/web/jsonrpc/probe' | ||
|
|
||
| for path in ( | ||
| '/api/v1/mobile/jsonrpc/probe', | ||
| '/api/v1/web/jsonrpc/probe', | ||
| ): | ||
| for path in (mobile_path, web_path): | ||
| assert path in paths | ||
|
|
||
| # Response model the same and deduplicated | ||
| assert '_Response[probe]' in schemas | ||
|
|
||
| if '_Params[probe]' not in schemas: | ||
| for component_name in ( | ||
| 'api.mobile._Params[probe]', | ||
| 'api.mobile._Request[probe]', | ||
| 'api.web._Params[probe]', | ||
| 'api.web._Request[probe]', | ||
| ): | ||
| assert component_name in schemas | ||
| ''') | ||
| web_response_ref = paths[web_path]['post']['responses']['200']['content']['application/json']['schema']['$ref'] | ||
| mobile_response_ref = paths[mobile_path]['post']['responses']['200']['content']['application/json']['schema']['$ref'] | ||
|
|
||
| assert web_response_ref == mobile_response_ref | ||
|
|
||
| web_request_ref = paths[web_path]['post']['requestBody']['content']['application/json']['schema']['$ref'] | ||
| mobile_request_ref = paths[mobile_path]['post']['requestBody']['content']['application/json']['schema']['$ref'] | ||
|
|
||
| if '{package_type}' == 'same-sig': | ||
| assert web_request_ref == mobile_request_ref | ||
| assert web_request_ref.split('/')[-1] in schemas | ||
| else: | ||
| assert web_request_ref != mobile_request_ref | ||
| assert web_request_ref.split('/')[-1] in schemas | ||
| assert mobile_request_ref.split('/')[-1] in schemas | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we bring back hard-coded values to make the test clearer about which values are expected? |
||
| '''.format(package_type=api_package)) | ||
|
|
||
| # force reload module to drop component cache | ||
| # it's more efficient than use pytest.runpytest_subprocess() | ||
|
|
@@ -732,4 +737,4 @@ def test_no_entrypoints__ok(fastapi_jsonrpc_components_fine_names): | |
| app_client = TestClient(app) | ||
| resp = app_client.get('/openapi.json') | ||
| resp.raise_for_status() | ||
| assert resp.status_code == 200 | ||
| assert resp.status_code == 200 | ||
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import sys | ||
| from typing import Dict, List, Optional | ||
|
|
||
| import pytest | ||
|
|
||
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,82 @@ | ||||||
| from typing import List | ||||||
|
|
||||||
| from pydantic import BaseModel | ||||||
|
|
||||||
|
|
||||||
| def test_type_keyword_field(ep, app, app_client): | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| class Model1(BaseModel): | ||||||
| x: int | ||||||
|
|
||||||
| class Model2(BaseModel): | ||||||
| y: int | ||||||
|
|
||||||
| type Model = Model1 | Model2 | ||||||
|
|
||||||
| class Input(BaseModel): | ||||||
| data: Model | ||||||
|
|
||||||
| class Output(BaseModel): | ||||||
| result: List[int] | ||||||
|
|
||||||
| @ep.method() | ||||||
| def my_method_type_keyword(inp: Input) -> Output: | ||||||
| if isinstance(inp.data, Model1): | ||||||
| return Output(result=[inp.data.x]) | ||||||
| return Output(result=[inp.data.y]) | ||||||
|
|
||||||
| app.bind_entrypoint(ep) | ||||||
|
|
||||||
| resp = app_client.get('/openrpc.json') | ||||||
| schema = resp.json() | ||||||
|
|
||||||
| assert len(schema['methods']) == 1 | ||||||
| assert schema['methods'][0]['params'] == [ | ||||||
| { | ||||||
| 'name': 'inp', | ||||||
| 'schema': {'$ref': '#/components/schemas/Input'}, | ||||||
| 'required': True, | ||||||
| } | ||||||
| ] | ||||||
| assert schema['methods'][0]['result'] == { | ||||||
| 'name': 'my_method_type_keyword_Result', | ||||||
| 'schema': {'$ref': '#/components/schemas/Output'}, | ||||||
| } | ||||||
|
|
||||||
| assert schema['components']['schemas'] == { | ||||||
| 'Input': { | ||||||
| 'properties': {'data': {'$ref': '#/components/schemas/Model'}}, | ||||||
| 'required': ['data'], | ||||||
| 'title': 'Input', | ||||||
| 'type': 'object', | ||||||
| }, | ||||||
| 'Model': { | ||||||
| 'anyOf': [ | ||||||
| {'$ref': '#/components/schemas/Model1'}, | ||||||
| {'$ref': '#/components/schemas/Model2'}, | ||||||
| ] | ||||||
| }, | ||||||
| 'Model1': { | ||||||
| 'properties': {'x': {'title': 'X', 'type': 'integer'}}, | ||||||
| 'required': ['x'], | ||||||
| 'title': 'Model1', | ||||||
| 'type': 'object', | ||||||
| }, | ||||||
| 'Model2': { | ||||||
| 'properties': {'y': {'title': 'Y', 'type': 'integer'}}, | ||||||
| 'required': ['y'], | ||||||
| 'title': 'Model2', | ||||||
| 'type': 'object', | ||||||
| }, | ||||||
| 'Output': { | ||||||
| 'properties': { | ||||||
| 'result': { | ||||||
| 'items': {'type': 'integer'}, | ||||||
| 'title': 'Result', | ||||||
| 'type': 'array', | ||||||
| } | ||||||
| }, | ||||||
| 'required': ['result'], | ||||||
| 'title': 'Output', | ||||||
| 'type': 'object', | ||||||
| }, | ||||||
| } | ||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not change the fixture return type.
If you need access to some of the fixture params, you can follow one of the two ways:
I would prefer the second method for this case: