papsign / Ktor-OpenAPI-Generator

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

serialized name annotation #47

Closed y9san9 closed 4 years ago

y9san9 commented 4 years ago

image Is there possibility to rename parameters as in Gson library ? image I want generate parameters with different name in Kotlin and Json

Wicpar commented 4 years ago

I'll see if you can add it as a module or not, if not i'll try to add functionality to allow the handling of such cases.

y9san9 commented 4 years ago

Ok, waiting for reply

Wicpar commented 4 years ago

you can use replaceModule in the config, and replace this default module https://github.com/papsign/Ktor-OpenAPI-Generator/blob/master/src/main/kotlin/com/papsign/ktor/openapigen/schema/builder/provider/DefaultObjectSchemaProvider.kt with one that takes in account the name from the annotation, this is the relevant part:

SchemaModel.SchemaModelObj<Any?>(
    props.associate {
        // change name here if it is annotated
        Pair(it.name, builder.build(it.type, it.source.annotations))
    },
    props.filter {
        !it.type.isMarkedNullable
    }.map { it.name } // make sure to have the right name here as well
)

in the config:

replaceModule(DefaultObjectSchemaProvider, CustomObjectSchemaProvider)

Allowing a more generic modification requires quite a bit more work, probably a system like the validators but the module detects the annotation instead of the other way around.

If you want such an extension system feel free to open another issue.

y9san9 commented 4 years ago

should I extend my class from it? It is final type as I see

Wicpar commented 4 years ago

no, you create a new one, and replace the default module with yours using replaceModule in the config. make sure you implement SchemaBuilderProviderModule, it is the required interface for it to work

y9san9 commented 4 years ago

Can I do same with parameters ? I wanna name header 'token' in Kotlin and 'Authorization' in request (without Auth)

Wicpar commented 4 years ago

Yes normally you should be able to, look which module instanciates the header's model. All openapi structures are defined in the models package, it should be 1:1 to the spec, look what module instanciates the model you wish to customize, and replace the module with a custom one.

y9san9 commented 4 years ago

Ok thanks