Skip to content
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

Missing contextual annotation on optional List< java.math.BigDecimal> #2959

Closed
tswanson-flow opened this issue Mar 21, 2025 · 4 comments
Closed

Comments

@tswanson-flow
Copy link

Describe the bug

I am working on producing types from schema files using an openapi generator with the kotlinx_serialization library. This works flawlessly for 60ish types / data classes. But when serializing a Polygon schema with a Point inside, the kotlinx serializer does not add an @contextual tag to allow for BigDecimal serialization. I believe these schema's come from geojson, but I could be wrong. In the end, I end up with the following compiler error:

Serializer has not been found for type 'java.math.BigDecimal'. To use context serializer as fallback, explicitly annotate type or property with @Contextual

To fix this error, I have to hand edit the file to add the annotation before it's allowed to compile.

Its possible this is actually an openapi bug report, so please let me know if this is not within y'all's domain.

To Reproduce
Json file to produce data class from

{
  "$ref": "base.json",
  "properties": {
    "data": {
      "properties": {
        "locationWgs84": {
          "type": "object",
          "required": [
            "latitude",
            "longitude"
          ],
          "properties": {
            "latitude": {
              "description": "latitude in degrees north",
              "type": "number",
              "minimum": -90,
              "maximum": 90
            },
            "longitude": {
              "description": "longitude in degrees east",
              "type": "number",
              "minimum": -180,
              "maximum": 180
            },
            "altitude": {
              "description": "altitude in meters",
              "type": "number"
            }
          }
        },
        "dimensionsMeters": {
          "type": "object",
          "required": [
            "height",
            "width",
            "depth"
          ],
          "properties": {
            "height": {
              "description": "height in meters",
              "type": "number"
            },
            "width": {
              "description": "width in meters",
              "type": "number"
            },
            "depth": {
              "description": "depth in meters",
              "type": "number"
            }
          }
        }
      }
    }
  }
}

Generator code

tasks.register<org.openapitools.generator.gradle.plugin.tasks.GenerateTask>("generateTypes") {
    dependsOn("copySchema")

    if (!openApiSpec.exists()) {
        error("OpenAPI spec not found at: ${openApiSpec.path}")
    }

    println("${layout.buildDirectory}")
    inputSpec.set(openApiSpec.absolutePath)

    outputDir.set(outputDirectory.absolutePath)

    generatorName.set("kotlin")

    configOptions.set(
        mapOf(
            "serializationLibrary" to "kotlinx_serialization",
            "useKotlinDataClasses" to "true",
            "mapType" to "Map",
            "generateSupportingFiles" to "false",
            "generateTestFiles" to "false",
        ),
    )
}

Expected behavior

I would expect that the serialization library would just add the contextual annotation, thus saving me from hand editing generated files. One file seems okay to manipulate by hand, but I worry about maintenance.

Environment

  • IntelliJ IDE (And its associated kotlin version) : 2024.3.4.1
  • Library version: Unsure, openapi version 7.11.0
  • Kotlin platforms: JVM
  • Gradle version: 8.10
@pdvrieze
Copy link
Contributor

This appears to be a bug in the schema to kotlin library. Possibly because the library does not offer a standard serializer for BigDecimal

@tswanson-flow
Copy link
Author

Is this bug filed in the right place for that to be handled?

@pdvrieze
Copy link
Contributor

@tswanson-flow It is a bug in the openapi generator, not the serialization library. So this would not be the right place.

@sandwwraith
Copy link
Member

So, the Kotlin files are generated by openAPI generator? Then file this issue to it, please.

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

No branches or pull requests

3 participants