Skip to content

Round string digit with .0 #10385

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
ByteWither opened this issue Mar 27, 2025 · 0 comments
Open

Round string digit with .0 #10385

ByteWither opened this issue Mar 27, 2025 · 0 comments

Comments

@ByteWither
Copy link

ByteWither commented Mar 27, 2025

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:

  1. Go to any query with multipart/form-data
  2. Click on "Try it out"
  3. Set string value to 5.0
  4. See 5.0 was converted to 5

Expected behavior

Numbers with .0 will not round

Screenshots

Image Image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant