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

OpenAPI generation of JsonView annotated classes or Ignored fields does not work #1764

Closed
AlanSage11 opened this issue Mar 7, 2024 · 1 comment · Fixed by #2053
Closed
Labels
bug Something isn't working enhancement New feature or request

Comments

@AlanSage11
Copy link

The issue here shows a previous problem where OpenAPI was not respecting fields annotated with @JsonView. The applied fix seems to work for simple cases, but does not work when a class is annotated with @JsonView or if the ObjectMapper is set not to add fields to views by default. Examples are in Kotlin/Quarkus, using version 3.10.0 of smallrye-open-api via Quarkus 3.8.1.

Example 1 (works as expected):

    @JsonView(View.Public::class)
    @GET
    @Path("/public")
    fun userPublic(): User? {
        return User().also {
            it.id = 123
            it.name = "Person"
        }
    }

    class User {
        @JsonView(View.Private::class)
        var id = 0

        @JsonView(View.Public::class)
        var name: String? = null
    }

    class View {
        open class Public
        class Private : Public()
    }

Endpoint /public returns {"name":"Person"}
Generated type schema (as expected):

    User_Public:
      type: object
      properties:
        name:
          type: string
          nullable: true

Example 2 (annotate class definition with @JsonView):

    @JsonView(View.Public::class)
    @GET
    @Path("/public")
    fun userPublic(): User? {
        return User().also {
            it.id = 123
            it.name = "Person"
        }
    }

    @JsonView(View.Private::class)
    class User {
        var id = 123

        @JsonView(View.Public::class)
        var name: String? = null
    }

    class View {
        open class Public
        class Private : Public()
    }

Endpoint /public returns {"name":"Person"} (as expected)
Generated type schema (Note: id field should not be shown here):

    User_Public:
      type: object
      properties:
        id:
          format: int32
          type: integer
        name:
          type: string
          nullable: true

Example 3 (change ObjectMapper to exclude fields from @JsonView by default):

ObjectMapper can be setup as follows to exclude fields by default from @JsonView:

@Singleton
class RegisterCustomModuleCustomizer : ObjectMapperCustomizer {
    override fun customize(mapper: ObjectMapper) {
        mapper.apply {
            disable(MapperFeature.DEFAULT_VIEW_INCLUSION)
        }
    }
}

Neither the id field or the User class now need to be annotated to be ignored by Jackson:

    @JsonView(View.Public::class)
    @GET
    @Path("/public")
    fun userPublic(): User? {
        return User().also {
            it.id = 123
            it.name = "Person"
        }
    }

    class User {
        var id = 123

        @JsonView(View.Public::class)
        var name: String? = null
    }

    class View {
        open class Public
        class Private : Public()
    }

Endpoint /public returns {"name":"Person"} (as expected)
Generated type schema (Note: id field should not be shown here):

    User_Public:
      type: object
      properties:
        id:
          format: int32
          type: integer
        name:
          type: string
          nullable: true
@MikeEdgar MikeEdgar added the bug Something isn't working label Mar 9, 2024
@MikeEdgar
Copy link
Member

Thanks for the issue. I think the class-level annotation handling should be an easy fix for the next release. The ObjectMapper functionality will probably need to be handled as a configuration property since the scanner doesn't have knowledge of or a reference to the runtime mapper the application is using.

@MikeEdgar MikeEdgar added the enhancement New feature or request label Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants