Skip to content

Commit c94dede

Browse files
author
pixellos
committed
Missing file
1 parent 56694b5 commit c94dede

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

Comments
 (0)