papsign / Ktor-OpenAPI-Generator

Ktor OpenAPI/Swagger 3 Generator
Apache License 2.0
243 stars 42 forks source link

Setting up "Models" section #12

Closed SerVB closed 4 years ago

SerVB commented 4 years ago

Hi! Is there an opportunity to change the "Models" section?

Currently, I have the following one (on this commit):

image

I'm not an experienced Swagger user but I think the issues are:

  1. The T model should not be presented.
  2. minimum: null/maximum: null for string and boolean seem redundant.
  3. minimum: -2147483648 for id: int32 isn't appropriate, I want it to start with 0.
  4. kotlin.collections.List is strange, it's a simple JSON array...
  5. io.github.servb.eShop.util.OptionalResult<io.github.servb.eShop.route.product.v1.ProductUsable> has the data: T field, but data: io.github.servb.eShop.route.product.v1.ProductUsable is more logical here.

I've briefly checked the annotations but haven't found suitable ones.

Wicpar commented 4 years ago

Hi, Generics classes are unsupported. Reflection is used during initialization, and generic types are erased. See JVM type erasure. This is one of the biggest caveats of JVM languages... One way around it is to wrap your generic types like this:

class NotGeneric: GenericClass<Generic>()

This should allow the reflection to find the appropriate types.

Wicpar commented 4 years ago

As for the nulls, the library currently relies on ktor to serialize the model into json. The mapper.setSerializationInclusion(Include.NON_NULL) should be activated for Jackson.

Wicpar commented 4 years ago

Although thinking about it, the KType information seems to have the generic information... Maybe there is a way to handle it.

Wicpar commented 4 years ago

Also, there is a hook in the configuration that allows you to properly rename the classes to be digestible by the standard.

Wicpar commented 4 years ago

@SerVB I changed the model acquisition system in the new release. Please verify if your issue persists or not.

Wicpar commented 4 years ago

https://github.com/papsign/Ktor-OpenAPI-Generator/releases/tag/0.1-beta.1

Fixed. Marking as closed.

SerVB commented 4 years ago

@Wicpar, thank you! I see many changes, for example, T and kotlin.collections.List have disappeared.

However, there are things that I still don't understand:

3) minimum: null/maximum: null for string and boolean seem redundant.

If I do mapper.setSerializationInclusion(Include.NON_NULL), these redundant lines disappear but it's not a solution, because also my server stops sending fields with null values. I want it to continue sending objects like {"data": null}.

4) minimum: -2147483648 for id: int32 isn't appropriate, I want it to start with 0.

So is there a way to annotate it?

Wicpar commented 4 years ago

these are separate issues... Currently the model is made with data classes and serialised from it. I'll have to change the whole model system to make it work like that. This can be quick fixed with a separate object mapper that serialises it to string in the openapi.json endpoint. Also, currently there are no @min @max annotations. Though it can be added quite rapidly iirc.