swagger-api / swagger-api/swagger-ui
Round string digit with .0
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 29k
- Forks
- 9.3k
- Avg merge
- 2d 23h
- Merged PRs (30d)
- 25
Description
Q&A (please complete the following information)
- OS: any
- Browser: chrome
- Version: 132.0.6834.84
- Method of installation: npm
- Swagger-UI version: ^5.20.1
- Swagger/OpenAPI version: OpenAPI 3.1.0
Content & configuration
Example Swagger/OpenAPI definition:
"post" : {
"tags" : [ "Test" ],
"summary" : "Test",
"operationId" : "upload",
"parameters" : [ {
"name" : "Test",
"in" : "query",
"description" : "Test",
"required" : true,
"schema" : {
"type" : "string",
"description" : "Test"
},
"example" : "maven-test"
} ],
"requestBody" : {
"description" : "Test",
"content" : {
"multipart/form-data" : {
"schema" : {
"type" : "object",
"additionalProperties" : false,
"description" : "Test",
"properties" : {
"maven2.groupId" : {
"type" : "string",
"description" : "maven2 Group ID",
"example" : "groupId_test"
},
"maven2.artifactId" : {
"type" : "string",
"description" : "maven2 Artifact ID",
"example" : "artifactId_test"
},
"maven2.version" : {
"type" : "string",
"description" : "maven2 Version",
"example" : "version_test"
},
"maven2.generate-pom" : {
"type" : "boolean",
"description" : "maven2 Generate a POM file with these coordinates",
"example" : false
},
"maven2.packaging" : {
"type" : "string",
"description" : "maven2 Packaging",
"example" : "packaging_test"
},
"maven2.tag" : {
"type" : "string",
"description" : "maven2 Tag",
"example" : "tag_test"
},
"maven2.asset1" : {
"type" : "string",
"format" : "binary",
"description" : "maven2 Asset 1",
"example" : "file_name.extension"
},
"maven2.asset1.extension" : {
"type" : "string",
"description" : "maven2 Asset 1 Extension",
"example" : "extension"
},
}
},
}
},
"required" : true
},
Swagger-UI configuration options:
function requestInterceptor(request: any) {
// @ts-ignore
request.headers['ANTI-CSRF-TOKEN'] = (document.cookie.match(
'(^|; )ANTI-CSRF-TOKEN=([^;]*)'
) || 0)[2]
return request
}
function responseInterceptor(response: any) {
let data = response.data
if (typeof response.data === 'string') {
try {
data = JSON.parse(response.data)
} catch (e) {
console.error(e)
}
}
if (data.tags) {
const tags = data.tags.sort((a: any, b: any) => a.name.localeCompare(b.name))
response.body.tags = tags
response.data = { ...data, tags }
try {
const text = JSON.parse(response.text)
response.text = JSON.stringify({ ...text, tags })
} catch (e) {
console.error(e)
}
}
return response
}
export function Api() {
return (
<SwaggerUI
url={ExtJS.urlOf('/service/rest/swagger.json')}
requestInterceptor={requestInterceptor}
responseInterceptor={responseInterceptor}
defaultModelsExpandDepth={-1}
/>
)
}
Describe the bug you're encountering
Hi! We have a bad case. When a user in Try it out mode enters the value “5.0” into a field of type string, it is rounded to “5”. If you enter “5.1” or “5.0.0” everything works as expected.
It is reproduced only in requests with payload of multipart/form-data type. There is no such problem in JSON.
Judging by your code, you run each value in FormData through JSON.parse, which turns the string “5.0” into “5”.
Is there anything I can do on my end to fix this, and if not, perhaps you can? Or I can send the PR to you, just tell me how to do it in your space.
To reproduce...
Steps to reproduce the behavior:
- Go to any query with multipart/form-data
- Click on "Try it out"
- Set string value to 5.0
- See 5.0 was converted to 5
Expected behavior
Numbers with .0 will not round
Screenshots
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue in Swagger UI's Try it out flow with a multipart/form-data request and a string value of 5.0. Trace the FormData handling where values are passed through JSON.parse, then add coverage for preserving the string and verify that 5.1, 5.0.0, and JSON request payloads remain correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 66/100