Skip to content

Commit 5144c54

Browse files
committed
add test case for global consumes and produces
1 parent 9311dca commit 5144c54

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -902,15 +902,21 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
902902
op.summary = escapeText(operation.getSummary());
903903
op.notes = escapeText(operation.getDescription());
904904
op.tags = operation.getTags();
905+
op.hasConsumes = false;
906+
op.hasProduces = false;
905907

906908
List<String> consumes = new ArrayList<String>();
907-
if (operation.getConsumes() != null && operation.getConsumes().size() > 0) {
908-
// use consumes defined in the operation
909-
consumes = operation.getConsumes();
909+
if (operation.getConsumes() != null) {
910+
if (operation.getConsumes().size() > 0) {
911+
// use consumes defined in the operation
912+
consumes = operation.getConsumes();
913+
} else {
914+
// empty list, do nothing to override global setting
915+
}
910916
} else if (swagger != null && swagger.getConsumes() != null && swagger.getConsumes().size() > 0) {
911917
// use consumes defined globally
912918
consumes = swagger.getConsumes();
913-
LOGGER.debug("Using global consumes (" + swagger.getConsumes() + ") for " + op.operationId);
919+
LOGGER.debug("No consumes defined in operation. Using global consumes (" + swagger.getConsumes() + ") for " + op.operationId);
914920
}
915921

916922
// if "consumes" is defined (per operation or using global definition)
@@ -933,13 +939,17 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
933939
}
934940

935941
List<String> produces = new ArrayList<String>();
936-
if (operation.getProduces() != null && operation.getProduces().size() > 0) {
937-
// use produces defined in the operation
938-
produces = operation.getProduces();
942+
if (operation.getProduces() != null) {
943+
if (operation.getProduces().size() > 0) {
944+
// use produces defined in the operation
945+
produces = operation.getProduces();
946+
} else {
947+
// empty list, do nothing to override global setting
948+
}
939949
} else if (swagger != null && swagger.getProduces() != null && swagger.getProduces().size() > 0) {
940950
// use produces defined globally
941951
produces = swagger.getProduces();
942-
LOGGER.debug("Using global produces (" + swagger.getProduces() + ") for " + op.operationId);
952+
LOGGER.debug("No produces defined in operation. Using global produces (" + swagger.getProduces() + ") for " + op.operationId);
943953
}
944954

945955
// if "produces" is defined (per operation or using global definition)

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 producus")
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 producus")
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 spec consumes and producus (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
}

0 commit comments

Comments
 (0)