Skip to content

Commit a625b0d

Browse files
Support more metadata in SwaggerConf (#58)
Adding more Open API sections to the plugin configuration. (replacing PR#53) Co-authored-by: Wanno Drijfhout <[email protected]>
1 parent ae3b567 commit a625b0d

16 files changed

+787
-28
lines changed

src/main/java/io/openapitools/swagger/OutputFormat.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package io.openapitools.swagger;
22

3+
import java.io.File;
4+
import java.io.IOException;
5+
36
import com.fasterxml.jackson.databind.ObjectMapper;
47
import com.fasterxml.jackson.databind.SerializationFeature;
8+
import io.openapitools.swagger.config.SwaggerServerVariable;
59
import io.swagger.v3.core.util.Json;
610
import io.swagger.v3.core.util.Yaml;
711
import io.swagger.v3.oas.models.OpenAPI;
8-
import java.io.File;
9-
import java.io.IOException;
12+
import io.swagger.v3.oas.models.servers.ServerVariable;
1013

1114
/**
1215
* Supported output formats.
@@ -42,6 +45,7 @@ static class JSONWriter implements SwaggerWriter {
4245
@Override
4346
public void write(OpenAPI swagger, File file, boolean prettyPrint) throws IOException {
4447
ObjectMapper mapper = Json.mapper();
48+
mapper.addMixIn(ServerVariable.class, SwaggerServerVariable.ServerVariableMixin.class);
4549
if (prettyPrint) {
4650
mapper.enable(SerializationFeature.INDENT_OUTPUT);
4751
}
@@ -56,7 +60,9 @@ static class YAMLWriter implements SwaggerWriter {
5660

5761
@Override
5862
public void write(OpenAPI swagger, File file, boolean prettyPrint) throws IOException {
59-
Yaml.mapper().writeValue(file, swagger);
63+
ObjectMapper mapper = Yaml.mapper();
64+
mapper.addMixIn(ServerVariable.class, SwaggerServerVariable.ServerVariableMixin.class);
65+
mapper.writeValue(file, swagger);
6066
}
6167
}
6268
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.openapitools.swagger.config;
2+
3+
import java.util.Map;
4+
5+
import org.apache.maven.plugins.annotations.Parameter;
6+
7+
import io.swagger.v3.oas.models.Components;
8+
9+
public class SwaggerComponents {
10+
11+
/**
12+
* Security schemes (under Comtonents)
13+
*/
14+
@Parameter
15+
private Map<String,SwaggerSecurityScheme> securitySchemes;
16+
17+
// TODO: implement schemas, responses, ... from
18+
// https://github.com/OAI/OpenAPI-Specification/blob/3.0.1/versions/3.0.1.md#componentsObject
19+
20+
public Components createComponentsModel() {
21+
Components components = new Components();
22+
23+
if (securitySchemes != null && !securitySchemes.isEmpty()) {
24+
securitySchemes.entrySet().forEach(s -> components.addSecuritySchemes(s.getKey(), s.getValue().createSecuritySchemaModel()));
25+
}
26+
27+
return components;
28+
}
29+
}
Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,88 @@
11
package io.openapitools.swagger.config;
22

3+
import io.swagger.v3.oas.annotations.ExternalDocumentation;
4+
import io.swagger.v3.oas.annotations.Parameters;
35
import io.swagger.v3.oas.models.OpenAPI;
46
import io.swagger.v3.oas.models.info.Info;
57
import java.io.File;
68
import java.io.IOException;
79
import java.nio.file.Files;
8-
import java.util.Arrays;
910
import java.util.Collections;
1011
import java.util.List;
12+
import java.util.Map;
1113
import java.util.stream.Collectors;
1214
import org.apache.maven.plugins.annotations.Parameter;
1315

1416
/**
15-
* Configuring Swagger in compliance with the way the com.github.kongchen Swagger plugin does it.
17+
* Configuring Swagger in compliance with the way the com.github.kongchen
18+
* Swagger plugin does it.
1619
*/
1720
public class SwaggerConfig {
1821

1922
/**
20-
* List of servers for the endpoint.
23+
* REQUIRED. Provides metadata about the API. The metadata MAY be used by
24+
* tooling as required.
25+
*
26+
* This might be overridden by ReaderListener or SwaggerDefinition annotation.
27+
*/
28+
@Parameter
29+
private SwaggerInfo info;
30+
31+
/**
32+
* Convenience for reading the informational description from file instead of
33+
* embedding it.
34+
*/
35+
@Parameter
36+
private File descriptionFile;
37+
38+
/**
39+
* An array of Server Objects, which provide connectivity information to a
40+
* target server. If the servers property is not provided, or is an empty array,
41+
* the default value would be a Server Object with a url value of /.
2142
*/
2243
@Parameter
2344
private List<SwaggerServer> servers = Collections.emptyList();
2445

2546
/**
26-
* Providing the OpenAPI information description. This might be overridden by ReaderListener or SwaggerDefinition annotation.
47+
* An element to hold various schemas for the specification.
2748
*/
2849
@Parameter
29-
private SwaggerInfo info;
50+
private SwaggerComponents components;
3051

3152
/**
32-
* Convenience for reading the informational description from file instead of embedding it.
53+
* A declaration of which security mechanisms can be used across the API. The
54+
* list of values includes alternative security requirement objects that can be
55+
* used. Only one of the security requirement objects need to be satisfied to
56+
* authorize a request. Individual operations can override this definition.
3357
*/
3458
@Parameter
35-
private File descriptionFile;
59+
private List<SwaggerSecurityRequirement> securityRequirements = Collections.emptyList();;
60+
61+
/**
62+
* A list of tags used by the specification with additional metadata. The order
63+
* of the tags can be used to reflect on their order by the parsing tools. Not
64+
* all tags that are used by the Operation Object must be declared. The tags
65+
* that are not declared MAY be organized randomly or based on the tools' logic.
66+
* Each tag name in the list MUST be unique.
67+
*/
68+
@Parameter
69+
private List<SwaggerTag> tags = Collections.emptyList();;
70+
71+
/**
72+
* Additional external documentation.
73+
*/
74+
@Parameter
75+
private SwaggerExternalDoc externalDoc;
76+
77+
/**
78+
* Providing extension attributes to the OpenAPI spec.
79+
*/
80+
@Parameter
81+
private Map<String, Object> extensions = Collections.emptyMap();
3682

3783
public OpenAPI createSwaggerModel() {
3884
OpenAPI spec = new OpenAPI();
3985

40-
servers.forEach(s -> spec.addServersItem(s.createServerModel()));
41-
4286
if (info != null) {
4387
spec.setInfo(info.createInfoModel());
4488
}
@@ -48,12 +92,26 @@ public OpenAPI createSwaggerModel() {
4892
spec.setInfo(new Info());
4993
}
5094
try {
51-
spec.getInfo().setDescription(Files.readAllLines(descriptionFile.toPath()).stream().collect(Collectors.joining("\n")));
95+
spec.getInfo().setDescription(
96+
Files.readAllLines(descriptionFile.toPath()).stream().collect(Collectors.joining("\n")));
5297
} catch (IOException e) {
5398
throw new RuntimeException("Unable to read descriptor file " + descriptionFile, e);
5499
}
55100
}
56101

102+
if (components != null) {
103+
spec.setComponents(components.createComponentsModel());
104+
}
105+
106+
if (externalDoc != null) {
107+
spec.setExternalDocs(externalDoc.createExternalDocModel());
108+
}
109+
110+
spec.setExtensions(extensions);
111+
servers.forEach(s -> spec.addServersItem(s.createServerModel()));
112+
securityRequirements.forEach(s -> spec.addSecurityItem(s.createSecurityModel()));
113+
tags.forEach(t -> spec.addTagsItem(t.createTagModel()));
114+
57115
return spec;
58116
}
59117
}

src/main/java/io/openapitools/swagger/config/SwaggerContact.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
package io.openapitools.swagger.config;
22

33
import io.swagger.v3.oas.models.info.Contact;
4+
5+
import java.util.Collections;
6+
import java.util.Map;
7+
48
import org.apache.maven.plugins.annotations.Parameter;
59

610
/**
711
* Configuring Swagger contact properties.
812
*/
913
public class SwaggerContact {
1014

15+
/**
16+
* The identifying name of the contact person/organization.
17+
*/
1118
@Parameter
1219
private String name;
1320

21+
/**
22+
* The URL pointing to the contact information. MUST be in the format of a URL.
23+
*/
1424
@Parameter
1525
private String url;
1626

27+
/**
28+
* The email address of the contact person/organization. MUST be in the format of an email address.
29+
*/
1730
@Parameter
1831
private String email;
1932

33+
@Parameter
34+
private Map<String, Object> extensions = Collections.emptyMap();
35+
2036
public Contact createContactModel() {
2137
Contact contact = new Contact();
2238

@@ -32,6 +48,8 @@ public Contact createContactModel() {
3248
contact.setEmail(email);
3349
}
3450

51+
contact.setExtensions(extensions);
52+
3553
return contact;
3654
}
3755
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.openapitools.swagger.config;
2+
3+
import java.util.Collections;
4+
import java.util.Map;
5+
6+
import org.apache.maven.plugins.annotations.Parameter;
7+
8+
import io.swagger.v3.oas.models.ExternalDocumentation;
9+
10+
public class SwaggerExternalDoc {
11+
12+
/**
13+
* A short description of the target documentation. CommonMark syntax MAY be
14+
* used for rich text representation.
15+
*/
16+
@Parameter
17+
private String description;
18+
19+
/**
20+
* REQUIRED. The URL for the target documentation. Value MUST be in the format
21+
* of a URL.
22+
*/
23+
@Parameter(required = true)
24+
private String url;
25+
26+
@Parameter
27+
private Map<String, Object> extensions = Collections.emptyMap();
28+
29+
public ExternalDocumentation createExternalDocModel() {
30+
ExternalDocumentation externalDoc = new ExternalDocumentation();
31+
32+
if (description != null) {
33+
externalDoc.setDescription(description);
34+
}
35+
36+
if (url != null) {
37+
externalDoc.setUrl(url);
38+
}
39+
40+
externalDoc.setExtensions(extensions);
41+
42+
return externalDoc;
43+
}
44+
45+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package io.openapitools.swagger.config;
2+
3+
import java.util.Collections;
4+
import java.util.Map;
5+
6+
import io.swagger.v3.oas.annotations.Parameter;
7+
import io.swagger.v3.oas.models.security.OAuthFlow;
8+
import io.swagger.v3.oas.models.security.OAuthFlows;
9+
import io.swagger.v3.oas.models.security.Scopes;
10+
11+
public class SwaggerFlows {
12+
13+
/**
14+
* Configuration for the OAuth Implicit flow
15+
*/
16+
@Parameter
17+
private Entry implicit;
18+
19+
/**
20+
* Configuration for the OAuth Resource Owner Password flow
21+
*/
22+
@Parameter
23+
private Entry password;
24+
25+
/**
26+
* Configuration for the OAuth Client Credentials flow. Previously called
27+
* application in OpenAPI 2.0.
28+
*/
29+
@Parameter
30+
private Entry clientCredentials;
31+
32+
/**
33+
* Configuration for the OAuth Authorization Code flow. Previously called
34+
* accessCode in OpenAPI 2.0.
35+
*/
36+
@Parameter
37+
private Entry authorizationCode;
38+
39+
@Parameter
40+
private Map<String, Object> extensions = Collections.emptyMap();
41+
42+
public OAuthFlows createOAuthFlowsModel() {
43+
OAuthFlows flows = new OAuthFlows();
44+
if (implicit != null) {
45+
flows.setImplicit(implicit.toOAuthFlowModel());
46+
}
47+
if (password != null) {
48+
flows.setPassword(password.toOAuthFlowModel());
49+
}
50+
if (clientCredentials != null) {
51+
flows.setClientCredentials(clientCredentials.toOAuthFlowModel());
52+
}
53+
if (authorizationCode != null) {
54+
flows.setAuthorizationCode(authorizationCode.toOAuthFlowModel());
55+
}
56+
flows.setExtensions(extensions);
57+
return flows;
58+
}
59+
60+
public static class Entry {
61+
/**
62+
* For implicit/authorizationCode flows: REQUIRED. The authorization URL to be used for this flow. This MUST be in the form of a URL.
63+
*/
64+
@Parameter
65+
private String authorizationUrl;
66+
67+
/**
68+
* For password/clientCredentials/AuthorizationCode flows: REQUIRED. The token URL to be used for this flow. This MUST be in the form of a URL.
69+
*/
70+
@Parameter
71+
private String tokenUrl;
72+
73+
/**
74+
* The URL to be used for obtaining refresh tokens. This MUST be in the form of a URL.
75+
*/
76+
@Parameter
77+
private String refreshUrl;
78+
79+
/**
80+
* REQUIRED. The available scopes for the OAuth2 security scheme. A map between the scope name and a short description for it.
81+
*/
82+
@Parameter(required = true)
83+
private Map<String, String> scopes = Collections.emptyMap();
84+
85+
@Parameter
86+
private Map<String, Object> extensions = Collections.emptyMap();
87+
88+
public OAuthFlow toOAuthFlowModel() {
89+
OAuthFlow flow = new OAuthFlow();
90+
91+
flow.setAuthorizationUrl(authorizationUrl);
92+
flow.setTokenUrl(tokenUrl);
93+
flow.setRefreshUrl(refreshUrl);
94+
if (scopes != null && !scopes.isEmpty()) {
95+
Scopes ss = new Scopes();
96+
scopes.entrySet().forEach(s -> ss.addString(s.getKey(), s.getValue()));
97+
flow.setScopes(ss);
98+
}
99+
100+
if (extensions != null && !extensions.isEmpty()) {
101+
flow.setExtensions(extensions);
102+
}
103+
return flow;
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)