Skip to content

Commit d7b4f58

Browse files
committed
Merge pull request #1411 from wing328/master
Add support for top-level consumes and produces
2 parents a2fda60 + e8fbdaa commit d7b4f58

File tree

6 files changed

+229
-11
lines changed

6 files changed

+229
-11
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public interface CodegenConfig {
6767

6868
CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions);
6969

70+
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger);
71+
7072
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions);
7173

7274
List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes);

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,12 @@ private Response findMethodResponse(Map<String, Response> responses) {
867867
}
868868
return responses.get(code);
869869
}
870-
870+
871871
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
872+
return fromOperation(path, httpMethod, operation, definitions, null);
873+
}
874+
875+
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
872876
CodegenOperation op = CodegenModelFactory.newInstance(CodegenModelType.OPERATION);
873877
Set<String> imports = new HashSet<String>();
874878
op.vendorExtensions = operation.getVendorExtensions();
@@ -904,15 +908,32 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
904908
op.summary = escapeText(operation.getSummary());
905909
op.notes = escapeText(operation.getDescription());
906910
op.tags = operation.getTags();
907-
908-
if (operation.getConsumes() != null && operation.getConsumes().size() > 0) {
911+
op.hasConsumes = false;
912+
op.hasProduces = false;
913+
914+
List<String> consumes = new ArrayList<String>();
915+
if (operation.getConsumes() != null) {
916+
if (operation.getConsumes().size() > 0) {
917+
// use consumes defined in the operation
918+
consumes = operation.getConsumes();
919+
} else {
920+
// empty list, do nothing to override global setting
921+
}
922+
} else if (swagger != null && swagger.getConsumes() != null && swagger.getConsumes().size() > 0) {
923+
// use consumes defined globally
924+
consumes = swagger.getConsumes();
925+
LOGGER.debug("No consumes defined in operation. Using global consumes (" + swagger.getConsumes() + ") for " + op.operationId);
926+
}
927+
928+
// if "consumes" is defined (per operation or using global definition)
929+
if (consumes != null && consumes.size() > 0) {
909930
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
910931
int count = 0;
911-
for (String key : operation.getConsumes()) {
932+
for (String key : consumes) {
912933
Map<String, String> mediaType = new HashMap<String, String>();
913934
mediaType.put("mediaType", key);
914935
count += 1;
915-
if (count < operation.getConsumes().size()) {
936+
if (count < consumes.size()) {
916937
mediaType.put("hasMore", "true");
917938
} else {
918939
mediaType.put("hasMore", null);
@@ -923,14 +944,29 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
923944
op.hasConsumes = true;
924945
}
925946

926-
if (operation.getProduces() != null && operation.getProduces().size() > 0) {
947+
List<String> produces = new ArrayList<String>();
948+
if (operation.getProduces() != null) {
949+
if (operation.getProduces().size() > 0) {
950+
// use produces defined in the operation
951+
produces = operation.getProduces();
952+
} else {
953+
// empty list, do nothing to override global setting
954+
}
955+
} else if (swagger != null && swagger.getProduces() != null && swagger.getProduces().size() > 0) {
956+
// use produces defined globally
957+
produces = swagger.getProduces();
958+
LOGGER.debug("No produces defined in operation. Using global produces (" + swagger.getProduces() + ") for " + op.operationId);
959+
}
960+
961+
// if "produces" is defined (per operation or using global definition)
962+
if (produces != null && produces.size() > 0) {
927963
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
928964
int count = 0;
929-
for (String key : operation.getProduces()) {
965+
for (String key : produces) {
930966
Map<String, String> mediaType = new HashMap<String, String>();
931967
mediaType.put("mediaType", key);
932968
count += 1;
933-
if (count < operation.getProduces().size()) {
969+
if (count < produces.size()) {
934970
mediaType.put("hasMore", "true");
935971
} else {
936972
mediaType.put("hasMore", null);

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
471471
for (String tag : tags) {
472472
CodegenOperation co = null;
473473
try {
474-
co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions());
474+
co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions(), swagger);
475475
co.tags = new ArrayList<String>();
476476
co.tags.add(sanitizeTag(tag));
477477
config.addOperationToGroup(sanitizeTag(tag), resourcePath, operation, co, operations);

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SwiftCodegen.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package io.swagger.codegen.languages;
22

33
import com.google.common.base.Predicate;
4+
45
import com.google.common.collect.Iterators;
56
import com.google.common.collect.Lists;
67
import io.swagger.codegen.*;
8+
import io.swagger.models.Swagger;
79
import io.swagger.models.Model;
810
import io.swagger.models.Operation;
911
import io.swagger.models.parameters.HeaderParameter;
@@ -256,7 +258,7 @@ public String toApiName(String name) {
256258
}
257259

258260
@Override
259-
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
261+
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
260262
path = normalizePath(path);
261263
List<Parameter> parameters = operation.getParameters();
262264
parameters = Lists.newArrayList(Iterators.filter(parameters.iterator(), new Predicate<Parameter>() {
@@ -266,7 +268,7 @@ public boolean apply(@Nullable Parameter parameter) {
266268
}
267269
}));
268270
operation.setParameters(parameters);
269-
return super.fromOperation(path, httpMethod, operation, definitions);
271+
return super.fromOperation(path, httpMethod, operation, definitions, swagger);
270272
}
271273

272274
private static String normalizePath(String path) {

modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,52 @@ public void binaryDataTest() {
139139
Assert.assertTrue(op.bodyParam.isBinary);
140140
Assert.assertTrue(op.responses.get(0).isBinary);
141141
}
142+
143+
@Test(description = "use operation consumes and produces")
144+
public void localConsumesAndProducesTest() {
145+
final Swagger model = new SwaggerParser().read("src/test/resources/2_0/globalConsumesAndProduces.json");
146+
final DefaultCodegen codegen = new DefaultCodegen();
147+
final String path = "/tests/localConsumesAndProduces";
148+
final Operation p = model.getPaths().get(path).getGet();
149+
CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions(), model);
150+
151+
Assert.assertTrue(op.hasConsumes);
152+
Assert.assertEquals(op.consumes.size(), 1);
153+
Assert.assertEquals(op.consumes.get(0).get("mediaType"), "application/json");
154+
Assert.assertTrue(op.hasProduces);
155+
Assert.assertEquals(op.produces.size(), 1);
156+
Assert.assertEquals(op.produces.get(0).get("mediaType"), "application/json");
157+
}
158+
159+
@Test(description = "use spec consumes and produces")
160+
public void globalConsumesAndProducesTest() {
161+
final Swagger model = new SwaggerParser().read("src/test/resources/2_0/globalConsumesAndProduces.json");
162+
final DefaultCodegen codegen = new DefaultCodegen();
163+
final String path = "/tests/globalConsumesAndProduces";
164+
final Operation p = model.getPaths().get(path).getGet();
165+
CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions(), model);
166+
167+
Assert.assertTrue(op.hasConsumes);
168+
Assert.assertEquals(op.consumes.size(), 1);
169+
Assert.assertEquals(op.consumes.get(0).get("mediaType"), "application/global_consumes");
170+
Assert.assertTrue(op.hasProduces);
171+
Assert.assertEquals(op.produces.size(), 1);
172+
Assert.assertEquals(op.produces.get(0).get("mediaType"), "application/global_produces");
173+
}
174+
175+
@Test(description = "use operation consumes and produces (reset in operation with empty array)")
176+
public void localResetConsumesAndProducesTest() {
177+
final Swagger model = new SwaggerParser().read("src/test/resources/2_0/globalConsumesAndProduces.json");
178+
final DefaultCodegen codegen = new DefaultCodegen();
179+
final String path = "/tests/localResetConsumesAndProduces";
180+
final Operation p = model.getPaths().get(path).getGet();
181+
CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions(), model);
182+
183+
Assert.assertNotNull(op);
184+
Assert.assertFalse(op.hasConsumes);
185+
Assert.assertNull(op.consumes);
186+
Assert.assertFalse(op.hasProduces);
187+
Assert.assertNull(op.produces);
188+
189+
}
142190
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
{
2+
"swagger": "2.0",
3+
"info": {
4+
"description": "Spec for testing global consumes and produces",
5+
"version": "1.0.0",
6+
"title": "Swagger Petstore",
7+
"termsOfService": "http://swagger.io/terms/",
8+
"contact": {
9+
"email": "[email protected]"
10+
},
11+
"license": {
12+
"name": "Apache 2.0",
13+
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
14+
}
15+
},
16+
"host": "petstore.swagger.io",
17+
"basePath": "/v2",
18+
"consumes": ["application/global_consumes"],
19+
"produces": ["application/global_produces"],
20+
"schemes": [
21+
"http"
22+
],
23+
"paths": {
24+
"/tests/localConsumesAndProduces": {
25+
"get": {
26+
"tags": [
27+
"tests"
28+
],
29+
"summary": "Operation with local consumes and produces",
30+
"description": "",
31+
"operationId": "localConsumesAndProduces",
32+
"produces": [
33+
"application/json"
34+
],
35+
"consumes": [
36+
"application/json"
37+
],
38+
"parameters": [
39+
],
40+
"responses": {
41+
"200": {
42+
"description": "successful operation. Returning a simple int.",
43+
"schema": {
44+
"type": "integer",
45+
"format": "int64"
46+
}
47+
}
48+
}
49+
}
50+
},
51+
"/tests/globalConsumesAndProduces": {
52+
"get": {
53+
"tags": [
54+
"tests"
55+
],
56+
"summary": "Operation with global consumes and produces",
57+
"description": "",
58+
"operationId": "globalConsumesAndProduces",
59+
"parameters": [
60+
],
61+
"responses": {
62+
"200": {
63+
"description": "successful operation. Returning a simple int.",
64+
"schema": {
65+
"type": "integer",
66+
"format": "int64"
67+
}
68+
}
69+
}
70+
}
71+
},
72+
"/tests/localResetConsumesAndProduces": {
73+
"get": {
74+
"tags": [
75+
"tests"
76+
],
77+
"summary": "Operation with local consumes and produces set to empty (reset)",
78+
"description": "",
79+
"operationId": "localResetConsumesAndProduces",
80+
"parameters": [
81+
],
82+
"consumes": [],
83+
"produces": [],
84+
"responses": {
85+
"200": {
86+
"description": "successful operation. Returning a simple int.",
87+
"schema": {
88+
"type": "integer",
89+
"format": "int64"
90+
}
91+
}
92+
}
93+
}
94+
}
95+
96+
},
97+
"securityDefinitions": {
98+
"api_key": {
99+
"type": "apiKey",
100+
"name": "api_key",
101+
"in": "header"
102+
},
103+
"petstore_auth": {
104+
"type": "oauth2",
105+
"authorizationUrl": "http://petstore.swagger.io/api/oauth/dialog",
106+
"flow": "implicit",
107+
"scopes": {
108+
"write:pets": "modify pets in your account",
109+
"read:pets": "read your pets"
110+
}
111+
}
112+
},
113+
"definitions": {
114+
"CustomModel": {
115+
"required": [
116+
"id"
117+
],
118+
"properties": {
119+
"id": {
120+
"type": "integer",
121+
"format": "int64"
122+
},
123+
"name": {
124+
"type": "string",
125+
"example": "doggie"
126+
}
127+
}
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)