forked from smoketurner/dropwizard-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwaggerBundleConfiguration.java
364 lines (301 loc) · 9.62 KB
/
SwaggerBundleConfiguration.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* Copyright © 2014 Federico Recio (N/A)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright (C) 2014 Federico Recio
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.federecio.dropwizard.swagger;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Strings;
import io.swagger.v3.oas.integration.SwaggerConfiguration;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import java.util.Arrays;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import javax.validation.constraints.NotEmpty;
/**
* For the meaning of all these properties please refer to Swagger documentation or {@link
* SwaggerConfiguration}
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class SwaggerBundleConfiguration implements Cloneable {
/**
* This is the only property that is required for Swagger to work correctly.
*
* <p>It is a comma separated list of the all the packages that contain the {@link
* io.swagger.v3.oas.annotations.OpenAPIDefinition} annotated resources
*/
@NotEmpty private String resourcePackage = "";
@Nullable private String title;
@Nullable private String version;
@Nullable private String description;
@Nullable private String termsOfServiceUrl;
@Nullable private String contact;
@Nullable private String contactEmail;
@Nullable private String contactUrl;
@Nullable private String license;
@Nullable private String licenseUrl;
@Nullable private String customJavascript;
private SwaggerViewConfiguration swaggerViewConfiguration = new SwaggerViewConfiguration();
private SwaggerOAuth2Configuration swaggerOAuth2Configuration = new SwaggerOAuth2Configuration();
private boolean prettyPrint = true;
@Nullable private String host;
private String contextRoot = "/";
private String[] schemes = new String[] {};
private boolean enabled = true;
private boolean includeSwaggerResource = true;
private boolean readAllResources = true;
/**
* For most of the scenarios this property is not needed.
*
* <p>This is not a property for Swagger but for bundle to set up Swagger UI correctly. It only
* needs to be used of the root path or the context path is set programmatically and therefore
* cannot be derived correctly. The problem arises in that if you set the root path or context
* path in the run() method in your Application subclass the bundle has already been initialized
* by that time and so does not know you set the path programmatically.
*/
@Nullable private String uriPrefix;
@JsonProperty
public String getResourcePackage() {
return resourcePackage;
}
@JsonProperty
public void setResourcePackage(String resourcePackage) {
this.resourcePackage = resourcePackage;
}
@Nullable
@JsonProperty
public String getTitle() {
return title;
}
@JsonProperty
public void setTitle(@Nullable String title) {
this.title = title;
}
@Nullable
@JsonProperty
public String getVersion() {
return version;
}
@JsonProperty
public void setVersion(@Nullable String version) {
this.version = version;
}
@Nullable
@JsonProperty
public String getDescription() {
return description;
}
@JsonProperty
public void setDescription(@Nullable String description) {
this.description = description;
}
@Nullable
@JsonProperty
public String getTermsOfServiceUrl() {
return termsOfServiceUrl;
}
@JsonProperty
public void setTermsOfServiceUrl(@Nullable String termsOfServiceUrl) {
this.termsOfServiceUrl = termsOfServiceUrl;
}
@Nullable
@JsonProperty
public String getContact() {
return contact;
}
@JsonProperty
public void setContact(@Nullable String contact) {
this.contact = contact;
}
@Nullable
@JsonProperty
public String getContactEmail() {
return contactEmail;
}
@JsonProperty
public void setContactEmail(@Nullable String contactEmail) {
this.contactEmail = contactEmail;
}
@Nullable
@JsonProperty
public String getContactUrl() {
return contactUrl;
}
@JsonProperty
public void setContactUrl(@Nullable String contactUrl) {
this.contactUrl = contactUrl;
}
@Nullable
@JsonProperty
public String getLicense() {
return license;
}
@JsonProperty
public void setLicense(@Nullable String license) {
this.license = license;
}
@Nullable
@JsonProperty
public String getLicenseUrl() {
return licenseUrl;
}
@JsonProperty
public void setLicenseUrl(@Nullable String licenseUrl) {
this.licenseUrl = licenseUrl;
}
@Nullable
@JsonProperty
public String getUriPrefix() {
return uriPrefix;
}
@JsonProperty
public void setUriPrefix(@Nullable String uriPrefix) {
this.uriPrefix = uriPrefix;
}
@JsonProperty
public SwaggerViewConfiguration getSwaggerViewConfiguration() {
return swaggerViewConfiguration.clone();
}
@JsonProperty
public void setSwaggerViewConfiguration(final SwaggerViewConfiguration swaggerViewConfiguration) {
this.swaggerViewConfiguration = swaggerViewConfiguration.clone();
}
@JsonProperty
public SwaggerOAuth2Configuration getSwaggerOAuth2Configuration() {
return swaggerOAuth2Configuration.clone();
}
@JsonProperty("oauth2")
public void setSwaggerOAuth2Configuration(
final SwaggerOAuth2Configuration swaggerOAuth2Configuration) {
this.swaggerOAuth2Configuration = swaggerOAuth2Configuration.clone();
}
@JsonProperty
public boolean isPrettyPrint() {
return prettyPrint;
}
@JsonProperty
public void setIsPrettyPrint(final boolean isPrettyPrint) {
this.prettyPrint = isPrettyPrint;
}
@Nullable
@JsonProperty
public String getHost() {
return host;
}
@JsonProperty
public void setHost(@Nullable String host) {
this.host = host;
}
@JsonProperty
public String getContextRoot() {
return contextRoot;
}
@JsonProperty
public void setContextRoot(String contextRoot) {
this.contextRoot = contextRoot;
}
@JsonProperty
public String[] getSchemes() {
return Arrays.copyOf(schemes, schemes.length);
}
@JsonProperty
public void setSchemes(String[] schemes) {
this.schemes = Arrays.copyOf(schemes, schemes.length);
}
@JsonProperty
public boolean isEnabled() {
return enabled;
}
@JsonProperty
public void setIsEnabled(final boolean isEnabled) {
this.enabled = isEnabled;
}
@JsonProperty
public boolean isIncludeSwaggerResource() {
return includeSwaggerResource;
}
@JsonProperty
public void setIncludeSwaggerResource(final boolean include) {
this.includeSwaggerResource = include;
}
@Nullable
@JsonProperty
public String getCustomJavascript() {
return customJavascript;
}
@JsonProperty
public void setCustomJavascript(@Nullable String customJavascript) {
this.customJavascript = customJavascript;
}
@JsonProperty
public boolean isReadAllResources() {
return readAllResources;
}
@JsonProperty
public void setReadAllResources(final boolean include) {
this.readAllResources = include;
}
@JsonIgnore
public SwaggerConfiguration build() {
if (Strings.isNullOrEmpty(resourcePackage)) {
throw new IllegalStateException(
"Resource package needs to be specified"
+ " for Swagger to correctly detect annotated resources");
}
OpenAPI oas = new OpenAPI();
final Info info =
new Info()
.title(title)
.version(version)
.description(description)
.contact(new Contact().email(contactEmail).name(contact).url(contactUrl))
.license(new License().name(license).url(licenseUrl))
.termsOfService(termsOfServiceUrl);
final String[] exclusions = {SwaggerResource.PATH};
return new SwaggerConfiguration()
.openAPI(oas.info(info))
.prettyPrint(prettyPrint)
.readAllResources(readAllResources)
.ignoredRoutes(Arrays.stream(exclusions).collect(Collectors.toSet()))
.resourcePackages(Arrays.stream(resourcePackage.split(",")).collect(Collectors.toSet()));
}
@Override
public SwaggerBundleConfiguration clone() {
try {
SwaggerBundleConfiguration clone = (SwaggerBundleConfiguration) super.clone();
clone.setSwaggerViewConfiguration(this.swaggerViewConfiguration);
clone.setSwaggerOAuth2Configuration(this.swaggerOAuth2Configuration);
clone.schemes = this.schemes.clone();
return clone;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}