quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.53k stars 2.61k forks source link

Integer fields are not marked as required by quarkus-smallrye-openapi when using Kotlin #33778

Open requiel20 opened 1 year ago

requiel20 commented 1 year ago

Describe the bug

I have a simple data class being returned by one of my routes.

Data class:

data class Fruit(val id: Long, val name: String)

Route:

@Path("/fruit")
class FruitRoutes {

    @GET
    fun getAllFruit(): Multi<Fruit> {
        return Fruit.findAll()
    }
}

Expected behavior

The schema generated by the quarkus-smallrye-openapi extension has the id field as required:

components:
  schemas:
    Fruit:
      required:
      - name
      - id
      type: object
      properties:
        id:
          format: int64
          type: integer
        name:
          type: string

Actual behavior

The schema generated by the quarkus-smallrye-openapi extension does not have the id as required:

components:
  schemas:
    Fruit:
      required:
      - name
      type: object
      properties:
        id:
          format: int64
          type: integer
        name:
          type: string

How to Reproduce?

No response

Output of uname -a or ver

Darwin lbattist-macOS 21.6.0 Darwin Kernel Version 21.6.0: Thu Mar 9 20:12:21 PST 2023; root:xnu-8020.240.18.700.8~1/RELEASE_ARM64_T6000 arm64

Output of java -version

openjdk version "17.0.5" 2022-10-18 OpenJDK Runtime Environment GraalVM CE 22.3.0 (build 17.0.5+8-jvmci-22.3-b08) OpenJDK 64-Bit Server VM GraalVM CE 22.3.0 (build 17.0.5+8-jvmci-22.3-b08, mixed mode, sharing)

GraalVM version (if different from Java)

No response

Quarkus version or git rev

3.0.4.Final

Build tool (ie. output of mvnw --version or gradlew --version)

Gradle 8.1.1

Additional information

This can be fixed by adding @Schema(requiredProperties = ["id", "name", "createdAt"]) to the class, but it shouldn't be necessary.

quarkus-bot[bot] commented 1 year ago

/cc @EricWittmann (openapi), @Ladicek (smallrye), @MikeEdgar (openapi), @evanchooly (kotlin), @geoand (kotlin), @jmartisk (smallrye), @phillip-kruger (openapi,smallrye), @radcortez (smallrye)

MikeEdgar commented 1 year ago

@requiel20 , the Long type is represented in the byte-code as a primitive long when it is non-nullable [1]. The scanner sees this and assumes it will be defaulted to 0 when missing.

You can work around this by adding @field:Schema(required = true) to the property.

[1] https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/

geoand commented 1 year ago

@MikeEdgar is there anything we can do on our end? If not, would a note in the docs make sense?

MikeEdgar commented 1 year ago

As far as I can tell there's nothing we can do. It amounts to loss of information from the Kotlin source to the bytecode used by Jandex. A note would probably make sense since it's not exactly intuitive (to me).

Which doc would it make the most sense to be included?

geoand commented 1 year ago

We have a guide for OpenAPI in Quarkus don't we? I'm thinking of a section on Kotlin that we can use to add such workarounds

MikeEdgar commented 1 year ago

Yeah, that makes sense. I was thinking it would be in the Kotlin guide (maybe just a link to the Kotlin section of the OpenAPI guide)

geoand commented 1 year ago

Works for me!