|
1 | 1 | /*
|
2 | 2 | *
|
3 | 3 | * *
|
| 4 | + * * * Copyright 2019-2020 the original author or authors. |
4 | 5 | * * *
|
5 |
| - * * * * Copyright 2019-2022 the original author or authors. |
6 |
| - * * * * |
7 |
| - * * * * Licensed under the Apache License, Version 2.0 (the "License"); |
8 |
| - * * * * you may not use this file except in compliance with the License. |
9 |
| - * * * * You may obtain a copy of the License at |
10 |
| - * * * * |
11 |
| - * * * * https://www.apache.org/licenses/LICENSE-2.0 |
12 |
| - * * * * |
13 |
| - * * * * Unless required by applicable law or agreed to in writing, software |
14 |
| - * * * * distributed under the License is distributed on an "AS IS" BASIS, |
15 |
| - * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 |
| - * * * * See the License for the specific language governing permissions and |
17 |
| - * * * * limitations under the License. |
| 6 | + * * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * * * you may not use this file except in compliance with the License. |
| 8 | + * * * You may obtain a copy of the License at |
18 | 9 | * * *
|
| 10 | + * * * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * * * |
| 12 | + * * * Unless required by applicable law or agreed to in writing, software |
| 13 | + * * * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * * * See the License for the specific language governing permissions and |
| 16 | + * * * limitations under the License. |
19 | 17 | * *
|
20 | 18 | *
|
21 | 19 | */
|
22 | 20 |
|
23 | 21 | package org.springdoc.core.utils;
|
24 | 22 |
|
| 23 | +import java.util.LinkedHashMap; |
| 24 | +import java.util.List; |
25 | 25 | import java.util.Locale;
|
| 26 | +import java.util.Map; |
| 27 | +import java.util.Map.Entry; |
| 28 | +import java.util.function.Consumer; |
| 29 | +import java.util.function.Supplier; |
| 30 | +import java.util.stream.Collectors; |
26 | 31 |
|
| 32 | +import io.swagger.v3.oas.models.info.Contact; |
| 33 | +import io.swagger.v3.oas.models.info.Info; |
| 34 | +import io.swagger.v3.oas.models.info.License; |
| 35 | +import io.swagger.v3.oas.models.media.Schema; |
| 36 | +import io.swagger.v3.oas.models.servers.Server; |
| 37 | +import org.apache.commons.lang3.StringUtils; |
27 | 38 | import org.slf4j.Logger;
|
28 | 39 | import org.slf4j.LoggerFactory;
|
29 | 40 | import org.springdoc.core.properties.SpringDocConfigProperties;
|
30 | 41 |
|
31 | 42 | import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
32 | 43 | import org.springframework.context.MessageSource;
|
33 | 44 | import org.springframework.context.NoSuchMessageException;
|
| 45 | +import org.springframework.util.CollectionUtils; |
34 | 46 |
|
35 | 47 | /**
|
36 | 48 | * The type Property resolver utils.
|
@@ -79,20 +91,111 @@ public PropertyResolverUtils(ConfigurableBeanFactory factory, MessageSource mess
|
79 | 91 | * @return the string
|
80 | 92 | */
|
81 | 93 | public String resolve(String parameterProperty, Locale locale) {
|
82 |
| - if (!springDocConfigProperties.isDisableI18n()) |
83 |
| - try { |
84 |
| - return messageSource.getMessage(parameterProperty, null, locale); |
85 |
| - } |
86 |
| - catch (NoSuchMessageException ex) { |
87 |
| - LOGGER.trace(ex.getMessage()); |
88 |
| - } |
89 |
| - try { |
90 |
| - return factory.resolveEmbeddedValue(parameterProperty); |
| 94 | + String result = parameterProperty; |
| 95 | + if (parameterProperty != null) { |
| 96 | + if (!springDocConfigProperties.isDisableI18n()) |
| 97 | + try { |
| 98 | + result = messageSource.getMessage(parameterProperty, null, locale); |
| 99 | + } |
| 100 | + catch (NoSuchMessageException ex) { |
| 101 | + LOGGER.trace(ex.getMessage()); |
| 102 | + } |
| 103 | + if (parameterProperty.equals(result)) |
| 104 | + try { |
| 105 | + result = factory.resolveEmbeddedValue(parameterProperty); |
| 106 | + } |
| 107 | + catch (IllegalArgumentException ex) { |
| 108 | + LOGGER.warn(ex.getMessage()); |
| 109 | + } |
| 110 | + } |
| 111 | + return result; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Resolve properties info. |
| 116 | + * |
| 117 | + * @param servers the servers |
| 118 | + * @param locale the locale |
| 119 | + * @return the servers |
| 120 | + */ |
| 121 | + public List<Server> resolveProperties(List<Server> servers, Locale locale) { |
| 122 | + servers.forEach(server -> { |
| 123 | + resolveProperty(server::getUrl, server::url, this, locale); |
| 124 | + resolveProperty(server::getDescription, server::description, this, locale); |
| 125 | + if (CollectionUtils.isEmpty(server.getVariables())) |
| 126 | + server.setVariables(null); |
| 127 | + }); |
| 128 | + return servers; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Resolve properties info. |
| 133 | + * |
| 134 | + * @param info the info |
| 135 | + * @param locale the locale |
| 136 | + * @return the info |
| 137 | + */ |
| 138 | + public Info resolveProperties(Info info, Locale locale) { |
| 139 | + resolveProperty(info::getTitle, info::title, this, locale); |
| 140 | + resolveProperty(info::getDescription, info::description, this, locale); |
| 141 | + resolveProperty(info::getVersion, info::version, this, locale); |
| 142 | + resolveProperty(info::getTermsOfService, info::termsOfService, this, locale); |
| 143 | + |
| 144 | + License license = info.getLicense(); |
| 145 | + if (license != null) { |
| 146 | + resolveProperty(license::getName, license::name, this, locale); |
| 147 | + resolveProperty(license::getUrl, license::url, this, locale); |
| 148 | + } |
| 149 | + |
| 150 | + Contact contact = info.getContact(); |
| 151 | + if (contact != null) { |
| 152 | + resolveProperty(contact::getName, contact::name, this, locale); |
| 153 | + resolveProperty(contact::getEmail, contact::email, this, locale); |
| 154 | + resolveProperty(contact::getUrl, contact::url, this, locale); |
| 155 | + } |
| 156 | + return info; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Resolve properties schema. |
| 161 | + * |
| 162 | + * @param schema the schema |
| 163 | + * @param locale the locale |
| 164 | + * @return the schema |
| 165 | + */ |
| 166 | + @SuppressWarnings("unchecked") |
| 167 | + public Schema resolveProperties(Schema schema, Locale locale) { |
| 168 | + resolveProperty(schema::getName, schema::name, this, locale); |
| 169 | + resolveProperty(schema::getTitle, schema::title, this, locale); |
| 170 | + resolveProperty(schema::getDescription, schema::description, this, locale); |
| 171 | + |
| 172 | + Map<String, Schema> properties = schema.getProperties(); |
| 173 | + if (!CollectionUtils.isEmpty(properties)) { |
| 174 | + LinkedHashMap<String, Schema> resolvedSchemas = properties.entrySet().stream().map(es -> { |
| 175 | + es.setValue(resolveProperties(es.getValue(), locale)); |
| 176 | + return es; |
| 177 | + }).collect(Collectors.toMap(Entry::getKey, Entry::getValue, (e1, e2) -> e2, |
| 178 | + LinkedHashMap::new)); |
| 179 | + schema.setProperties(resolvedSchemas); |
91 | 180 | }
|
92 |
| - catch (IllegalArgumentException ex) { |
93 |
| - LOGGER.warn(ex.getMessage()); |
| 181 | + |
| 182 | + return schema; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Resolve property. |
| 187 | + * |
| 188 | + * @param getProperty the get property |
| 189 | + * @param setProperty the set property |
| 190 | + * @param propertyResolverUtils the property resolver utils |
| 191 | + * @param locale the locale |
| 192 | + */ |
| 193 | + private void resolveProperty(Supplier<String> getProperty, Consumer<String> setProperty, |
| 194 | + PropertyResolverUtils propertyResolverUtils, Locale locale) { |
| 195 | + String value = getProperty.get(); |
| 196 | + if (StringUtils.isNotBlank(value)) { |
| 197 | + setProperty.accept(propertyResolverUtils.resolve(value, locale)); |
94 | 198 | }
|
95 |
| - return parameterProperty; |
96 | 199 | }
|
97 | 200 |
|
98 | 201 | /**
|
|
0 commit comments