|
| 1 | +/* |
| 2 | + * |
| 3 | + * * |
| 4 | + * * * |
| 5 | + * * * * |
| 6 | + * * * * * |
| 7 | + * * * * * * Copyright 2019-2024 the original author or authors. |
| 8 | + * * * * * * |
| 9 | + * * * * * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * * * * * * you may not use this file except in compliance with the License. |
| 11 | + * * * * * * You may obtain a copy of the License at |
| 12 | + * * * * * * |
| 13 | + * * * * * * https://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * * * * * * |
| 15 | + * * * * * * Unless required by applicable law or agreed to in writing, software |
| 16 | + * * * * * * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * * * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * * * * * * See the License for the specific language governing permissions and |
| 19 | + * * * * * * limitations under the License. |
| 20 | + * * * * * |
| 21 | + * * * * |
| 22 | + * * * |
| 23 | + * * |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +package org.springdoc.core.customizers |
| 28 | + |
| 29 | +import com.fasterxml.jackson.databind.JavaType |
| 30 | +import io.swagger.v3.core.converter.AnnotatedType |
| 31 | +import io.swagger.v3.core.converter.ModelConverter |
| 32 | +import io.swagger.v3.core.converter.ModelConverterContext |
| 33 | +import io.swagger.v3.oas.models.Components |
| 34 | +import io.swagger.v3.oas.models.media.Schema |
| 35 | +import org.springdoc.core.providers.ObjectMapperProvider |
| 36 | +import kotlin.reflect.full.findAnnotation |
| 37 | +import kotlin.reflect.full.hasAnnotation |
| 38 | +import kotlin.reflect.full.memberProperties |
| 39 | + |
| 40 | +/** |
| 41 | + * Kotlin Deprecated PropertyCustomizer to handle Kotlin Deprecated annotations. |
| 42 | + * @author bnasslahsen |
| 43 | + */ |
| 44 | +class KotlinDeprecatedPropertyCustomizer( |
| 45 | + private val objectMapperProvider: ObjectMapperProvider |
| 46 | +) : ModelConverter { |
| 47 | + |
| 48 | + override fun resolve( |
| 49 | + type: AnnotatedType, |
| 50 | + context: ModelConverterContext, |
| 51 | + chain: Iterator<ModelConverter> |
| 52 | + ): Schema<*>? { |
| 53 | + if (!chain.hasNext()) return null |
| 54 | + // Resolve the next model in the chain |
| 55 | + val resolvedSchema = chain.next().resolve(type, context, chain) |
| 56 | + |
| 57 | + val javaType: JavaType = |
| 58 | + objectMapperProvider.jsonMapper().constructType(type.type) |
| 59 | + val kotlinClass = javaType.rawClass.kotlin |
| 60 | + |
| 61 | + // Check each property of the class |
| 62 | + for (prop in kotlinClass.memberProperties) { |
| 63 | + val deprecatedAnnotation = prop.findAnnotation<Deprecated>() |
| 64 | + prop.hasAnnotation<Deprecated>() |
| 65 | + if (deprecatedAnnotation != null) { |
| 66 | + val fieldName = prop.name |
| 67 | + if (resolvedSchema.`$ref` != null) { |
| 68 | + val schema = |
| 69 | + context.getDefinedModels()[resolvedSchema.`$ref`.substring( |
| 70 | + Components.COMPONENTS_SCHEMAS_REF.length |
| 71 | + )] |
| 72 | + schema?.properties?.get(fieldName)?.deprecated = true |
| 73 | + if (deprecatedAnnotation.message.isNotBlank()) { |
| 74 | + schema?.properties?.get(fieldName)?.description = |
| 75 | + schema?.properties?.get(fieldName)?.description?.takeIf { it.isNotBlank() } |
| 76 | + ?: deprecatedAnnotation.message |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return resolvedSchema |
| 82 | + } |
| 83 | +} |
0 commit comments