fix(openapi): handle non-numeric OpenAPI response keys in return-doc generation#6484
Open
Diwak4r wants to merge 1 commit into
Open
fix(openapi): handle non-numeric OpenAPI response keys in return-doc generation#6484Diwak4r wants to merge 1 commit into
Diwak4r wants to merge 1 commit into
Conversation
sorted() used int(item[0]) as the sort key over the Responses Object,
which raised ValueError for valid non-numeric keys such as 'default' or
range codes ('2XX'). These are common in real specs, so loading any
OpenAPI toolset whose operation declares a 'default' response crashed.
Non-numeric keys are now ordered after numeric status codes.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
PydocHelper.generate_return_docsorts an operation's OpenAPI Responses Object withint(item[0])as the sort key:The OpenAPI 3.x spec allows the Responses Object to be keyed by non-numeric values in addition to numeric status codes: the
defaultcatch-all (very common in real specs) and range codes (1XX,2XX,3XX,4XX,5XX). For any of these keys,int(...)raisesValueError.This is reachable from normal usage:
OperationParser.get_pydoc_string()passesoperation.responsesstraight through, so building anOpenAPIToolsetfrom any spec whose operation declares adefaultresponse crashes with an opaqueValueError: invalid literal for int() with base 10: 'default'instead of parsing the tool.Fix
Sort numeric status codes as before and place non-numeric keys after them:
The existing
r[0].startswith('2')content filter still selects the smallest 2xx response with content, so behavior for numeric keys is unchanged.How verified
defaultresponse key.test_generate_return_doc_non_numeric_status_keysalongside the existingtest_generate_return_doc_*cases.pytest tests/unittests/tools/openapi_tool/common/test_common.py→ 43 passed (was 42).