|
| 1 | +/* |
| 2 | + * |
| 3 | + * * |
| 4 | + * * * |
| 5 | + * * * * |
| 6 | + * * * * * |
| 7 | + * * * * * * Copyright 2019-2025 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.converters; |
| 28 | + |
| 29 | + |
| 30 | +import java.util.Iterator; |
| 31 | + |
| 32 | +import com.fasterxml.jackson.databind.JavaType; |
| 33 | +import io.swagger.v3.core.converter.ModelConverter; |
| 34 | +import io.swagger.v3.core.converter.ModelConverterContext; |
| 35 | +import io.swagger.v3.core.util.AnnotationsUtils; |
| 36 | +import io.swagger.v3.oas.models.Components; |
| 37 | +import io.swagger.v3.oas.models.media.ArraySchema; |
| 38 | +import io.swagger.v3.oas.models.media.JsonSchema; |
| 39 | +import io.swagger.v3.oas.models.media.Schema; |
| 40 | +import org.springdoc.core.providers.ObjectMapperProvider; |
| 41 | + |
| 42 | +import org.springframework.hateoas.RepresentationModel; |
| 43 | + |
| 44 | +/** |
| 45 | + * The type Hateoas links converter. |
| 46 | + * |
| 47 | + * @author bnasslahsen |
| 48 | + */ |
| 49 | +public class HateoasLinksConverter implements ModelConverter { |
| 50 | + |
| 51 | + /** |
| 52 | + * The Spring doc object mapper. |
| 53 | + */ |
| 54 | + private final ObjectMapperProvider springDocObjectMapper; |
| 55 | + |
| 56 | + /** |
| 57 | + * Instantiates a new Hateoas links converter. |
| 58 | + * |
| 59 | + * @param springDocObjectMapper the spring doc object mapper |
| 60 | + */ |
| 61 | + public HateoasLinksConverter(ObjectMapperProvider springDocObjectMapper) { |
| 62 | + this.springDocObjectMapper = springDocObjectMapper; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Schema<?> resolve( |
| 67 | + io.swagger.v3.core.converter.AnnotatedType type, |
| 68 | + ModelConverterContext context, |
| 69 | + Iterator<ModelConverter> chain |
| 70 | + ) { |
| 71 | + JavaType javaType = springDocObjectMapper.jsonMapper().constructType(type.getType()); |
| 72 | + if (javaType != null) { |
| 73 | + if (RepresentationModel.class.isAssignableFrom(javaType.getRawClass())) { |
| 74 | + Schema<?> schema = chain.next().resolve(type, context, chain); |
| 75 | + String schemaName = schema.get$ref().substring(Components.COMPONENTS_SCHEMAS_REF.length()); |
| 76 | + Schema original = context.getDefinedModels().get(schemaName); |
| 77 | + Object links = original.getProperties().get("_links"); |
| 78 | + if(links instanceof JsonSchema jsonSchema) { |
| 79 | + jsonSchema.set$ref(AnnotationsUtils.COMPONENTS_REF + "Links"); |
| 80 | + jsonSchema.setType(null); |
| 81 | + jsonSchema.setItems(null); |
| 82 | + jsonSchema.setTypes(null); |
| 83 | + } else if (links instanceof ArraySchema arraySchema){ |
| 84 | + arraySchema.set$ref(AnnotationsUtils.COMPONENTS_REF + "Links"); |
| 85 | + } |
| 86 | + return schema; |
| 87 | + } |
| 88 | + } |
| 89 | + return chain.hasNext() ? chain.next().resolve(type, context, chain) : null; |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments