Skip to content

Commit ee324fe

Browse files
committed
resolve-schema-properties is not replacing tokens from properties file. Fixes #1552.
1 parent f52efa9 commit ee324fe

File tree

8 files changed

+232
-122
lines changed

8 files changed

+232
-122
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/GroupedOpenApi.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,4 +429,13 @@ public GroupedOpenApi build() {
429429
return new GroupedOpenApi(this);
430430
}
431431
}
432+
433+
/**
434+
* Add open api customiser.
435+
*
436+
* @param openApiCustomiser the open api customiser
437+
*/
438+
public void addOpenApiCustomiser(OpenApiCustomiser openApiCustomiser) {
439+
this.openApiCustomisers.add(openApiCustomiser);
440+
}
432441
}

springdoc-openapi-common/src/main/java/org/springdoc/core/OpenAPIService.java

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@
3030
import java.util.List;
3131
import java.util.Locale;
3232
import java.util.Map;
33-
import java.util.Map.Entry;
3433
import java.util.Optional;
3534
import java.util.Set;
36-
import java.util.function.Consumer;
37-
import java.util.function.Supplier;
3835
import java.util.stream.Collectors;
3936
import java.util.stream.Stream;
4037

@@ -51,10 +48,7 @@
5148
import io.swagger.v3.oas.models.OpenAPI;
5249
import io.swagger.v3.oas.models.Operation;
5350
import io.swagger.v3.oas.models.Paths;
54-
import io.swagger.v3.oas.models.info.Contact;
5551
import io.swagger.v3.oas.models.info.Info;
56-
import io.swagger.v3.oas.models.info.License;
57-
import io.swagger.v3.oas.models.media.Schema;
5852
import io.swagger.v3.oas.models.security.SecurityScheme;
5953
import io.swagger.v3.oas.models.servers.Server;
6054
import org.apache.commons.lang3.ArrayUtils;
@@ -434,32 +428,6 @@ public void buildTagsFromClass(Class<?> beanType, Set<io.swagger.v3.oas.models.t
434428
}
435429
}
436430

437-
/**
438-
* Resolve properties schema.
439-
*
440-
* @param schema the schema
441-
* @param locale the locale
442-
* @return the schema
443-
*/
444-
@SuppressWarnings("unchecked")
445-
public Schema resolveProperties(Schema schema, Locale locale) {
446-
resolveProperty(schema::getName, schema::name, propertyResolverUtils, locale);
447-
resolveProperty(schema::getTitle, schema::title, propertyResolverUtils, locale);
448-
resolveProperty(schema::getDescription, schema::description, propertyResolverUtils, locale);
449-
450-
Map<String, Schema> properties = schema.getProperties();
451-
if (!CollectionUtils.isEmpty(properties)) {
452-
LinkedHashMap<String, Schema> resolvedSchemas = properties.entrySet().stream().map(es -> {
453-
es.setValue(resolveProperties(es.getValue(), locale));
454-
return es;
455-
}).collect(Collectors.toMap(Entry::getKey, Entry::getValue, (e1, e2) -> e2,
456-
LinkedHashMap::new));
457-
schema.setProperties(resolvedSchemas);
458-
}
459-
460-
return schema;
461-
}
462-
463431
/**
464432
* Sets server base url.
465433
*
@@ -510,7 +478,7 @@ private Optional<OpenAPIDefinition> getOpenAPIDefinition() {
510478
*/
511479
private void buildOpenAPIWithOpenAPIDefinition(OpenAPI openAPI, OpenAPIDefinition apiDef, Locale locale) {
512480
// info
513-
AnnotationsUtils.getInfo(apiDef.info()).map(info -> resolveProperties(info, locale)).ifPresent(openAPI::setInfo);
481+
AnnotationsUtils.getInfo(apiDef.info()).map(info -> propertyResolverUtils.resolveProperties(info, locale)).ifPresent(openAPI::setInfo);
514482
// OpenApiDefinition security requirements
515483
securityParser.getSecurityRequirements(apiDef.security()).ifPresent(openAPI::setSecurity);
516484
// OpenApiDefinition external docs
@@ -519,7 +487,7 @@ private void buildOpenAPIWithOpenAPIDefinition(OpenAPI openAPI, OpenAPIDefinitio
519487
AnnotationsUtils.getTags(apiDef.tags(), false).ifPresent(tags -> openAPI.setTags(new ArrayList<>(tags)));
520488
// OpenApiDefinition servers
521489
Optional<List<Server>> optionalServers = AnnotationsUtils.getServers(apiDef.servers());
522-
optionalServers.map(servers -> resolveProperties(servers, locale)).ifPresent(servers -> {
490+
optionalServers.map(servers -> propertyResolverUtils.resolveProperties(servers, locale)).ifPresent(servers -> {
523491
this.isServersPresent = true;
524492
openAPI.servers(servers);
525493
}
@@ -530,67 +498,6 @@ private void buildOpenAPIWithOpenAPIDefinition(OpenAPI openAPI, OpenAPIDefinitio
530498
}
531499
}
532500

533-
/**
534-
* Resolve properties info.
535-
*
536-
* @param servers the servers
537-
* @param locale the locale
538-
* @return the servers
539-
*/
540-
private List<Server> resolveProperties(List<Server> servers, Locale locale) {
541-
servers.forEach(server -> {
542-
resolveProperty(server::getUrl, server::url, propertyResolverUtils, locale);
543-
resolveProperty(server::getDescription, server::description, propertyResolverUtils, locale);
544-
if (CollectionUtils.isEmpty(server.getVariables()))
545-
server.setVariables(null);
546-
});
547-
return servers;
548-
}
549-
550-
/**
551-
* Resolve properties info.
552-
*
553-
* @param info the info
554-
* @param locale the locale
555-
* @return the info
556-
*/
557-
private Info resolveProperties(Info info, Locale locale) {
558-
resolveProperty(info::getTitle, info::title, propertyResolverUtils, locale);
559-
resolveProperty(info::getDescription, info::description, propertyResolverUtils, locale);
560-
resolveProperty(info::getVersion, info::version, propertyResolverUtils, locale);
561-
resolveProperty(info::getTermsOfService, info::termsOfService, propertyResolverUtils, locale);
562-
563-
License license = info.getLicense();
564-
if (license != null) {
565-
resolveProperty(license::getName, license::name, propertyResolverUtils, locale);
566-
resolveProperty(license::getUrl, license::url, propertyResolverUtils, locale);
567-
}
568-
569-
Contact contact = info.getContact();
570-
if (contact != null) {
571-
resolveProperty(contact::getName, contact::name, propertyResolverUtils, locale);
572-
resolveProperty(contact::getEmail, contact::email, propertyResolverUtils, locale);
573-
resolveProperty(contact::getUrl, contact::url, propertyResolverUtils, locale);
574-
}
575-
return info;
576-
}
577-
578-
/**
579-
* Resolve property.
580-
*
581-
* @param getProperty the get property
582-
* @param setProperty the set property
583-
* @param propertyResolverUtils the property resolver utils
584-
* @param locale the locale
585-
*/
586-
private void resolveProperty(Supplier<String> getProperty, Consumer<String> setProperty,
587-
PropertyResolverUtils propertyResolverUtils, Locale locale) {
588-
String value = getProperty.get();
589-
if (StringUtils.isNotBlank(value)) {
590-
setProperty.accept(propertyResolverUtils.resolve(value, locale));
591-
}
592-
}
593-
594501
/**
595502
* Calculate security schemes.
596503
*

springdoc-openapi-common/src/main/java/org/springdoc/core/PropertyResolverUtils.java

Lines changed: 117 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,28 @@
2020

2121
package org.springdoc.core;
2222

23+
import java.util.LinkedHashMap;
24+
import java.util.List;
2325
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;
2431

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;
2538
import org.slf4j.Logger;
2639
import org.slf4j.LoggerFactory;
2740

2841
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
2942
import org.springframework.context.MessageSource;
3043
import org.springframework.context.NoSuchMessageException;
44+
import org.springframework.util.CollectionUtils;
3145

3246
/**
3347
* The type Property resolver utils.
@@ -76,20 +90,111 @@ public PropertyResolverUtils(ConfigurableBeanFactory factory, MessageSource mess
7690
* @return the string
7791
*/
7892
public String resolve(String parameterProperty, Locale locale) {
79-
if (!springDocConfigProperties.isDisableI18n())
80-
try {
81-
return messageSource.getMessage(parameterProperty, null, locale);
82-
}
83-
catch (NoSuchMessageException ex) {
84-
LOGGER.trace(ex.getMessage());
85-
}
86-
try {
87-
return factory.resolveEmbeddedValue(parameterProperty);
93+
String result = parameterProperty;
94+
if (parameterProperty != null) {
95+
if (!springDocConfigProperties.isDisableI18n())
96+
try {
97+
result = messageSource.getMessage(parameterProperty, null, locale);
98+
}
99+
catch (NoSuchMessageException ex) {
100+
LOGGER.trace(ex.getMessage());
101+
}
102+
if (parameterProperty.equals(result))
103+
try {
104+
result = factory.resolveEmbeddedValue(parameterProperty);
105+
}
106+
catch (IllegalArgumentException ex) {
107+
LOGGER.warn(ex.getMessage());
108+
}
88109
}
89-
catch (IllegalArgumentException ex) {
90-
LOGGER.warn(ex.getMessage());
110+
return result;
111+
}
112+
113+
/**
114+
* Resolve properties info.
115+
*
116+
* @param servers the servers
117+
* @param locale the locale
118+
* @return the servers
119+
*/
120+
public List<Server> resolveProperties(List<Server> servers, Locale locale) {
121+
servers.forEach(server -> {
122+
resolveProperty(server::getUrl, server::url, this, locale);
123+
resolveProperty(server::getDescription, server::description, this, locale);
124+
if (CollectionUtils.isEmpty(server.getVariables()))
125+
server.setVariables(null);
126+
});
127+
return servers;
128+
}
129+
130+
/**
131+
* Resolve properties info.
132+
*
133+
* @param info the info
134+
* @param locale the locale
135+
* @return the info
136+
*/
137+
public Info resolveProperties(Info info, Locale locale) {
138+
resolveProperty(info::getTitle, info::title, this, locale);
139+
resolveProperty(info::getDescription, info::description, this, locale);
140+
resolveProperty(info::getVersion, info::version, this, locale);
141+
resolveProperty(info::getTermsOfService, info::termsOfService, this, locale);
142+
143+
License license = info.getLicense();
144+
if (license != null) {
145+
resolveProperty(license::getName, license::name, this, locale);
146+
resolveProperty(license::getUrl, license::url, this, locale);
147+
}
148+
149+
Contact contact = info.getContact();
150+
if (contact != null) {
151+
resolveProperty(contact::getName, contact::name, this, locale);
152+
resolveProperty(contact::getEmail, contact::email, this, locale);
153+
resolveProperty(contact::getUrl, contact::url, this, locale);
154+
}
155+
return info;
156+
}
157+
158+
/**
159+
* Resolve properties schema.
160+
*
161+
* @param schema the schema
162+
* @param locale the locale
163+
* @return the schema
164+
*/
165+
@SuppressWarnings("unchecked")
166+
public Schema resolveProperties(Schema schema, Locale locale) {
167+
resolveProperty(schema::getName, schema::name, this, locale);
168+
resolveProperty(schema::getTitle, schema::title, this, locale);
169+
resolveProperty(schema::getDescription, schema::description, this, locale);
170+
171+
Map<String, Schema> properties = schema.getProperties();
172+
if (!CollectionUtils.isEmpty(properties)) {
173+
LinkedHashMap<String, Schema> resolvedSchemas = properties.entrySet().stream().map(es -> {
174+
es.setValue(resolveProperties(es.getValue(), locale));
175+
return es;
176+
}).collect(Collectors.toMap(Entry::getKey, Entry::getValue, (e1, e2) -> e2,
177+
LinkedHashMap::new));
178+
schema.setProperties(resolvedSchemas);
179+
}
180+
181+
return schema;
182+
}
183+
184+
/**
185+
* Resolve property.
186+
*
187+
* @param getProperty the get property
188+
* @param setProperty the set property
189+
* @param propertyResolverUtils the property resolver utils
190+
* @param locale the locale
191+
*/
192+
private void resolveProperty(Supplier<String> getProperty, Consumer<String> setProperty,
193+
PropertyResolverUtils propertyResolverUtils, Locale locale) {
194+
String value = getProperty.get();
195+
if (StringUtils.isNotBlank(value)) {
196+
setProperty.accept(propertyResolverUtils.resolve(value, locale));
91197
}
92-
return parameterProperty;
93198
}
94199

95200
/**

springdoc-openapi-common/src/main/java/org/springdoc/core/SpringDocConfiguration.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,18 +333,18 @@ GenericParameterService parameterBuilder(PropertyResolverUtils propertyResolverU
333333
/**
334334
* Properties resolver for schema open api customiser.
335335
*
336-
* @param openAPIService the open api builder
336+
* @param propertyResolverUtils the open api property Resolver
337337
* @return the open api customiser
338338
*/
339339
@Bean
340340
@ConditionalOnProperty(SPRINGDOC_SCHEMA_RESOLVE_PROPERTIES)
341341
@Lazy(false)
342-
OpenApiCustomiser propertiesResolverForSchema(OpenAPIService openAPIService) {
342+
OpenApiCustomiser propertiesResolverForSchema(PropertyResolverUtils propertyResolverUtils) {
343343
return openApi -> {
344344
Components components = openApi.getComponents();
345345
Map<String, Schema> schemas = components.getSchemas();
346346
if (!CollectionUtils.isEmpty(schemas))
347-
schemas.values().forEach(schema -> openAPIService.resolveProperties(schema, Locale.getDefault()));
347+
schemas.values().forEach(schema -> propertyResolverUtils.resolveProperties(schema, Locale.getDefault()));
348348
};
349349
}
350350

@@ -357,8 +357,8 @@ OpenApiCustomiser propertiesResolverForSchema(OpenAPIService openAPIService) {
357357
@Conditional(CacheOrGroupedOpenApiCondition.class)
358358
@ConditionalOnClass(name = BINDRESULT_CLASS)
359359
@Lazy(false)
360-
static BeanFactoryPostProcessor springdocBeanFactoryPostProcessor() {
361-
return new SpringdocBeanFactoryConfigurer();
360+
static BeanFactoryPostProcessor springdocBeanFactoryPostProcessor(List<GroupedOpenApi> groupedOpenApis) {
361+
return new SpringdocBeanFactoryConfigurer(groupedOpenApis);
362362
}
363363

364364
/**

springdoc-openapi-common/src/main/java/org/springdoc/core/SpringdocActuatorBeanFactoryConfigurer.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,13 @@
4747
*/
4848
public class SpringdocActuatorBeanFactoryConfigurer extends SpringdocBeanFactoryConfigurer{
4949

50-
/**
51-
* The Grouped open apis.
52-
*/
53-
private List<GroupedOpenApi> groupedOpenApis;
54-
5550
/**
5651
* Instantiates a new Springdoc actuator bean factory configurer.
5752
*
5853
* @param groupedOpenApis the grouped open apis
5954
*/
6055
public SpringdocActuatorBeanFactoryConfigurer(List<GroupedOpenApi> groupedOpenApis) {
61-
this.groupedOpenApis = groupedOpenApis;
56+
super(groupedOpenApis);
6257
}
6358

6459
@Override

0 commit comments

Comments
 (0)