-
Notifications
You must be signed in to change notification settings - Fork 11
/
OpenApiConfig.java
79 lines (60 loc) · 2.21 KB
/
OpenApiConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package sample.config;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.springdoc.core.customizers.OpenApiCustomiser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.converter.ResolvedSchema;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.media.Schema;
@Configuration
class OpenApiConfig {
@Autowired
TypesCapturer typesCapturer;
@Bean
public OpenAPI customOpenAPI(@Value("${application-description}") String appDesciption, @Value("${application-version}") String appVersion) {
return new OpenAPI()
.info(new Info()
.title("sample application API")
.version(appVersion)
.description(appDesciption)
.termsOfService("http://swagger.io/terms/")
.license(new License().name("Apache 2.0").url("http://springdoc.org")));
}
/**
* In this we are trying to enforce resolved schemas than schemas from
* io.swagger.v3.core.converter.ModelConverterContextImpl.getDefinedModels()
* @return
*/
@Bean
@Lazy(false)
OpenApiCustomiser resolvedSchemaEnforcer() {
return new OpenApiCustomiser() {
@Override
public void customise(OpenAPI openApi) {
Map<String, Type> schemaNameToTypeMap = typesCapturer.getSchemaNameToTypeMap();
Map<String, Schema> schemas = openApi.getComponents().getSchemas();
Set<Entry<String, Schema>> entrySet = schemas.entrySet();
for (Entry<String, Schema> entry : entrySet) {
String key = entry.getKey();
Type type = schemaNameToTypeMap.get(key);
if(type!=null)
{
ResolvedSchema resolvedSchema = ModelConverters.getInstance().readAllAsResolvedSchema(type);
if(resolvedSchema!=null && resolvedSchema.schema!=null)
{
schemas.put(key, resolvedSchema.schema);
}
}
}
}};
}
}