javalin / javalin-openapi

Annotation processor for compile-time OpenAPI & JsonSchema, with out-of-the-box support for Javalin 5.x, Swagger & ReDoc
https://github.com/javalin/javalin-openapi/wiki
Apache License 2.0
45 stars 17 forks source link

Discussion: Ignoring properties #184

Closed sauterl closed 1 year ago

sauterl commented 1 year ago

When using javalin, one way to handle serialization / deserialization could be done by Jackson and thus, properties that should be ignored are annotated with @JsonIgnore.

Unfortunately, the openapi plugin corresponding annotation, @OpenApiIgnore is not applicable to FIELD, as @JsonIgnore is.

My thoughts on this are as follows:

Thanks in advance!

dzikoysk commented 1 year ago

Sorry for late response, I didn't notice this issue.

  1. By default, OpenApi plugin reads only getters and thus fields are ignored. Do you use @OpenApiByFields?
  2. Not really, I guess it could be possible if we'd support custom annotations, so you could create sth like:
@OpenApiIgnore
@JacksonAnnotationsInside
@JsonIgnore
annotation class IgnoreInJacksonAndOpenApi

or with some custom handler in our scripting API.

sauterl commented 1 year ago

No worries, and thanks for the reply.

No, we do not use @OpenApiByFields.

I guess then this might be a bug:

We do have the following setup:

interface IMyLittleMoreThanPojo {
 val propertyWeWantInTheApi: ISomeType
val propertyWeDontWant: IOtherType
}

data class ApiMyLittleMoreThanPojo(
 override val propertyWeWantInTheApi = ApiSomeType()
) : IMyLittleMoreThanPojo {
 @JsonIgnore
 override lateinit var propertyWeDontWant: ApiOtherType
 internal set
} : 

This setup then results in the following OpenApi JSON:

"ApiMyLittleMoreThanPojo": {
 "type": "object",
 "additionalProperties": false,
 "properties":{
    "propertyWeWantInTheApi": {
     "$ref":"#/components/schemas/ApiSomeType"
    },
   "propertyWeDontWant":{
     "$ref":"#/components/schemas/ApiOtherType"
   }
 },
 "required":["propertyWeWantInTheApi", "propertyWeDontWant"]
}

Due to our setup, we cannot use @OpenApiIgnore, since propertyWeDontWant is a member property with backing field -- it might be worthwhile to simply adjust @OpenApiIgnore ?

dzikoysk commented 1 year ago

Oh, that's a property, not a field - I thought you're using Java, not Kotlin. You should annotate this with site target, like that:

@get:OpenApiIgnore
val propertyWeDontWant: IOtherType
sauterl commented 1 year ago

Thanks for pointing this out! I wasn't aware of site targeted annotations and my apologies for not properly label this as a kotlin problem.

dzikoysk commented 1 year ago

There's a nice plugin that can simplify this btw: #171