|
| 1 | +package com.papsign.ktor.openapigen.annotations.type.string.example |
| 2 | + |
| 3 | +import com.papsign.ktor.openapigen.model.schema.DataFormat |
| 4 | +import com.papsign.ktor.openapigen.model.schema.DataType |
| 5 | +import com.papsign.ktor.openapigen.model.schema.Discriminator |
| 6 | +import com.papsign.ktor.openapigen.model.schema.SchemaModel |
| 7 | +import com.papsign.ktor.openapigen.schema.processor.SchemaProcessor |
| 8 | +import com.papsign.ktor.openapigen.schema.processor.SchemaProcessorAnnotation |
| 9 | +import kotlin.reflect.KType |
| 10 | + |
| 11 | +@Target(AnnotationTarget.CLASS) |
| 12 | +@SchemaProcessorAnnotation(LegacyDiscriminatorProcessor::class) |
| 13 | +annotation class DiscriminatorAnnotation(val fieldName: String = "type") |
| 14 | + |
| 15 | +// Difference between legacy mode and current |
| 16 | +// https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/typescript.md |
| 17 | +// For non-legacy mapping is from sub-types to base-type by allOf |
| 18 | +// This implementation follow previous implementation, so we need to have |
| 19 | +// - discriminatorName in each type Parameters array |
| 20 | +// - { discriminator: { propertyName: discriminatorName } } in each type |
| 21 | +object LegacyDiscriminatorProcessor : SchemaProcessor<DiscriminatorAnnotation> { |
| 22 | + override fun process(model: SchemaModel<*>, type: KType, annotation: DiscriminatorAnnotation): SchemaModel<*> { |
| 23 | + val mapElement = (annotation.fieldName to SchemaModel.SchemaModelLitteral<String>( |
| 24 | + DataType.string, |
| 25 | + DataFormat.string, |
| 26 | + false |
| 27 | + )) |
| 28 | + |
| 29 | + if (model is SchemaModel.OneSchemaModelOf<*>) { |
| 30 | + return SchemaModel.OneSchemaModelOf( |
| 31 | + model.oneOf, |
| 32 | + mapOf(mapElement), |
| 33 | + Discriminator(annotation.fieldName) |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + if (model is SchemaModel.SchemaModelObj<*>) { |
| 38 | + |
| 39 | + return SchemaModel.SchemaModelObj( |
| 40 | + model.properties + mapElement, |
| 41 | + model.required, |
| 42 | + model.nullable, |
| 43 | + model.example, |
| 44 | + model.examples, |
| 45 | + model.type, |
| 46 | + model.description, |
| 47 | + Discriminator(annotation.fieldName) |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + return model |
| 52 | + } |
| 53 | + |
| 54 | +} |
0 commit comments