Skip to content

Instantly share code, notes, and snippets.

@sz763
Last active September 21, 2024 08:33
Show Gist options
  • Save sz763/a4d262a5a9a400c878b62759fc3f4787 to your computer and use it in GitHub Desktop.
Save sz763/a4d262a5a9a400c878b62759fc3f4787 to your computer and use it in GitHub Desktop.
How to fix rounded long values in spring + swagger-ui

If you read this article, then your http response in Swagger UI contains rounded Long values, e.g.

You are expect

{
  "id": 1111111111111111111
}

But got

{
   "id": 1111111111111111200
}

To proceed with steps, you should have installed

  1. Java
  2. Maven
  3. Nodejs
  4. Git

UI part

  1. Clone repository https://github.com/swagger-api/swagger-ui and checkout required version tag.
  2. Install dependencies: npm install
  3. Install additional dependency https://github.com/sidorares/json-bigint npm install json-bigint
  4. Navigate to response-body.jsx
  5. Add import json-bigint
    import JSONbig from "json-bigint"
  6. Change the row in response-body.jsx
    body = JSON.stringify(JSON.parse(content), null, "  ")
    should be changed to this one
    body = JSONbig.stringify(JSONbig.parse(content), null, "  ")
  7. Build project npr run build - you will get compiled project dist directory.

java part

  1. Clone https://github.com/webjars/swagger-ui
  2. Create new directory in project like ui
  3. Copy dist (you have got it after build UI project) to ui directory
  4. Edit pom.xml
    <!--To avoid conflicts with original lib, change group name-->
    <groupId>com.myawesome.group.name</groupId>
    ...
    <!-- Remove SCM -->
    <scm>
      <url>http://github.com/webjars/swagger-ui</url>
      <connection>scm:git:https://github.com/webjars/swagger-ui.git</connection>
      <developerConnection>scm:git:https://github.com/webjars/swagger-ui.git</developerConnection>
      <tag>HEAD</tag>
    </scm>
    ...
    <!--Change ant command-->
    <target>
      <echo message="moving resources" />
      <move todir="${destDir}">
    		<fileset dir="./ui/dist" />
      </move>
    </target>
    ...
    <!--Remove plugin-->
    <plugin>
      <groupId>org.sonatype.plugins</groupId>
      <artifactId>nexus-staging-maven-plugin</artifactId>
      <version>1.6.13</version>
      <extensions>true</extensions>
      <configuration>
        <serverId>sonatype-nexus-staging</serverId>
        <nexusUrl>https://oss.sonatype.org/</nexusUrl>
        <autoReleaseAfterClose>true</autoReleaseAfterClose>
      </configuration>
    </plugin>
  5. Build maven project mvn install - to test localy or mvn deploy to push it to registry. (!) after build ui/dist - will be cleaned up. Copy dist directroy once again or change ant command to copy instaed of move

Next steps you shoud do in your project

  1. In your awesome project add exclusion of webjars:swagger-ui. Edit pom.xml
    <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-ui</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.webjars</groupId>
          <artifactId>swagger-ui</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  2. Instead of org.webjars:swagger-ui - add new dependecy that you build abowe
    <dependency>
      <groupId>com.myawesome.group.name</groupId>
      <artifactId>swagger-ui</artifactId>
    </dependency>

Run your project and enjoy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment