|
| 1 | +/* |
| 2 | + * |
| 3 | + * * |
| 4 | + * * * |
| 5 | + * * * * |
| 6 | + * * * * * Copyright 2019-2022 the original author or authors. |
| 7 | + * * * * * |
| 8 | + * * * * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * * * * * you may not use this file except in compliance with the License. |
| 10 | + * * * * * You may obtain a copy of the License at |
| 11 | + * * * * * |
| 12 | + * * * * * https://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * * * * * |
| 14 | + * * * * * Unless required by applicable law or agreed to in writing, software |
| 15 | + * * * * * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * * * * * See the License for the specific language governing permissions and |
| 18 | + * * * * * limitations under the License. |
| 19 | + * * * * |
| 20 | + * * * |
| 21 | + * * |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +package org.springdoc.core.customizers; |
| 26 | + |
| 27 | +import io.swagger.v3.oas.models.*; |
| 28 | +import io.swagger.v3.oas.models.info.Info; |
| 29 | +import io.swagger.v3.oas.models.media.Schema; |
| 30 | +import org.apache.commons.lang3.StringUtils; |
| 31 | +import org.springframework.core.env.PropertyResolver; |
| 32 | +import org.springframework.util.CollectionUtils; |
| 33 | + |
| 34 | +import java.text.MessageFormat; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.function.Consumer; |
| 38 | + |
| 39 | +/** |
| 40 | + * Allows externalizing strings in generated openapi schema via properties that follow |
| 41 | + * conventional naming similar or identical to <a href="https://swagger.io/docs/specification/basic-structure/">openapi schema</a> |
| 42 | + * <p> |
| 43 | + * To set value of a string in schema, define an application property that matches the target node |
| 44 | + * with springdoc.specification-strings prefix. |
| 45 | + * <p> |
| 46 | + * Sample supported properties for api-info customization: |
| 47 | + * <ul> |
| 48 | + * <li>springdoc.specification-strings.info.title - to set title of api-info</li> |
| 49 | + * <li>springdoc.specification-strings.info.description - to set description of api-info</li> |
| 50 | + * <li>springdoc.specification-strings.info.version - to set version of api-info</li> |
| 51 | + * <li>springdoc.specification-strings.info.termsOfService - to set terms of service of api-info</li> |
| 52 | + * </ul> |
| 53 | + * <p> |
| 54 | + * Sample supported properties for components customization: |
| 55 | + * <ul> |
| 56 | + * <li>springdoc.specification-strings.components.User.description - to set description of User model</li> |
| 57 | + * <li>springdoc.specification-strings.components.User.properties.name.description - to set description of 'name' property</li> |
| 58 | + * <li>springdoc.specification-strings.components.User.properties.name.example - to set example of 'name' property</li> |
| 59 | + * </ul> |
| 60 | + * <p> |
| 61 | + * Sample supported properties for paths/operationIds customization: |
| 62 | + * <ul> |
| 63 | + * <li>springdoc.specification-strings.paths.{operationId}.description - to set description of {operationId}</li> |
| 64 | + * <li>springdoc.specification-strings.paths.{operationId}.summary - to set summary of {operationId}</li> |
| 65 | + * </ul> |
| 66 | + * |
| 67 | + * @author Anton Tkachenko [email protected] |
| 68 | + */ |
| 69 | +public class SpecificationStringPropertiesCustomizer implements GlobalOpenApiCustomizer { |
| 70 | + |
| 71 | + private static final String SPECIFICATION_STRINGS_PREFIX = "springdoc.specification-strings."; |
| 72 | + |
| 73 | + private final PropertyResolver propertyResolver; |
| 74 | + |
| 75 | + public SpecificationStringPropertiesCustomizer(PropertyResolver resolverUtils) { |
| 76 | + this.propertyResolver = resolverUtils; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void customise(OpenAPI openApi) { |
| 81 | + setOperationInfoProperties(openApi); |
| 82 | + setComponentsProperties(openApi); |
| 83 | + setPathsProperties(openApi); |
| 84 | + } |
| 85 | + |
| 86 | + private void setOperationInfoProperties(OpenAPI openApi) { |
| 87 | + if (openApi.getInfo() == null) { |
| 88 | + openApi.setInfo(new Info()); |
| 89 | + } |
| 90 | + Info info = openApi.getInfo(); |
| 91 | + resolveString(info::setTitle, "info.title"); |
| 92 | + resolveString(info::setDescription, "info.description"); |
| 93 | + resolveString(info::setVersion, "info.version"); |
| 94 | + resolveString(info::setTermsOfService, "info.termsOfService"); |
| 95 | + } |
| 96 | + |
| 97 | + private void setPathsProperties(OpenAPI openApi) { |
| 98 | + Paths paths = openApi.getPaths(); |
| 99 | + if (CollectionUtils.isEmpty(paths.values())) { |
| 100 | + return; |
| 101 | + } |
| 102 | + for (PathItem pathItem : paths.values()) { |
| 103 | + List<Operation> operations = pathItem.readOperations(); |
| 104 | + for (Operation operation : operations) { |
| 105 | + String operationId = operation.getOperationId(); |
| 106 | + String operationNode = MessageFormat.format("paths.{0}", operationId); |
| 107 | + resolveString(operation::setDescription, operationNode + ".description"); |
| 108 | + |
| 109 | + resolveString(operation::setSummary, operationNode + ".summary"); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void setComponentsProperties(OpenAPI openApi) { |
| 115 | + Components components = openApi.getComponents(); |
| 116 | + if (components == null || CollectionUtils.isEmpty(components.getSchemas())) { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + for (Schema componentSchema : components.getSchemas().values()) { |
| 121 | + // set component description |
| 122 | + String schemaPropertyPrefix = MessageFormat.format("components.schemas.{0}", componentSchema.getName()); |
| 123 | + resolveString(componentSchema::setDescription, schemaPropertyPrefix + ".description"); |
| 124 | + Map<String, Schema> properties = componentSchema.getProperties(); |
| 125 | + |
| 126 | + if (CollectionUtils.isEmpty(properties)) { |
| 127 | + continue; |
| 128 | + } |
| 129 | + |
| 130 | + for (Schema propSchema : properties.values()) { |
| 131 | + String propertyNode = MessageFormat.format("components.schemas.{0}.properties.{1}", |
| 132 | + componentSchema.getName(), propSchema.getName()); |
| 133 | + |
| 134 | + resolveString(propSchema::setDescription, propertyNode + ".description"); |
| 135 | + resolveString(propSchema::setExample, propertyNode + ".example"); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private void resolveString( |
| 141 | + Consumer<String> setter, String node |
| 142 | + ) { |
| 143 | + String nodeWithPrefix = SPECIFICATION_STRINGS_PREFIX + node; |
| 144 | + String value = propertyResolver.getProperty(nodeWithPrefix); |
| 145 | + if (StringUtils.isNotBlank(value)) { |
| 146 | + setter.accept(value); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments