fix: preserve string default values like '1.0' for form-urlencoded re…#10625
Open
Halcyonic-01 wants to merge 5 commits intoswagger-api:masterfrom
Open
fix: preserve string default values like '1.0' for form-urlencoded re…#10625Halcyonic-01 wants to merge 5 commits intoswagger-api:masterfrom
Halcyonic-01 wants to merge 5 commits intoswagger-api:masterfrom
Conversation
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.
Bug Fix Analysis: Issue #10610 - String Default "1.0" Converted to "1"
Bug Description
When using Swagger-UI with
application/x-www-form-urlencodedrequest bodies, string properties with default values like"1.0"were being incorrectly converted to"1"(losing the decimal point) when:This is problematic because values like version tags (e.g.,
"1.0") should remain as strings and not be converted to numbers.Root Cause
The bug occurred at multiple points in the codebase:
getDefaultRequestBodyValuefunction: When generating default values from schema,getSampleSchemamight return numeric values for string-typed properties if the default looked like a number.Initial value setting in RequestBody component: When setting initial values for form fields, the code wasn't preserving string defaults verbatim.
Request execution in actions.js: When processing the request body before execution, values weren't being properly coerced to strings for string-typed properties.
Reset functionality: When resetting form values, JSON parsing might lose string precision.
Fixes Applied
1. Fix in
getDefaultRequestBodyValue(request-body.jsx:31-39)What it does: After getting sample schema values, it explicitly converts string-typed properties with defaults back to strings using
String(propSchema.default), preserving values like"1.0".2. Fix in RequestBody Component (request-body.jsx:192-202)
What it does: When setting initial values for form fields, if the schema has a default value and the property type is string, it uses
String(schemaDefault)to preserve the exact string value.3. Fix in Request Execution (actions.js:442-453)
What it does: Before executing the request, it ensures that all string-typed properties are converted to strings. This prevents numeric coercion that could turn
"1.0"into1.4. Fix in Reset Functionality (OperationContainer.jsx:145-148)
What it does: When resetting form values, it ensures string-typed properties remain as strings after JSON parsing.
How to Test the Fix
Manual Testing Steps
Load the spec in Swagger-UI:
Test the default value display:
/testPOST operationversionTaginput field shows"1.0"(with quotes if displayed as JSON, or just1.0in the input field)1or"1"Test the curl command:
-d 'versionTag=1.0'-d 'versionTag=1'Test with user input:
"2.0")-d 'versionTag=2.0'"1.0"(not"1")Automated Test (Cypress)
Create a test file:
test/e2e-cypress/e2e/bugs/10047.cy.jsCreate the test spec:
test/e2e-cypress/static/documents/bugs/10047.yamlExpected Behavior After Fix
✅ Default value
"1.0"is displayed correctly in the form✅ The value
"1.0"is preserved when executing the request✅ The curl command shows
versionTag=1.0(notversionTag=1)✅ Resetting the form restores the value to
"1.0"✅ User can change the value and it's preserved correctly
Files Modified
src/core/plugins/oas3/components/request-body.jsx- Fixed default value generation and initial value settingsrc/core/plugins/spec/actions.js- Fixed request body processing before executionsrc/core/containers/OperationContainer.jsx- Fixed reset functionalityThe fix(Screenshot)-