add test case for global consumes and produces

This commit is contained in:
wing328 2015-10-10 22:47:24 +08:00
parent 9311dcaccb
commit 5144c54895
2 changed files with 66 additions and 8 deletions

View File

@ -902,15 +902,21 @@ public class DefaultCodegen {
op.summary = escapeText(operation.getSummary()); op.summary = escapeText(operation.getSummary());
op.notes = escapeText(operation.getDescription()); op.notes = escapeText(operation.getDescription());
op.tags = operation.getTags(); op.tags = operation.getTags();
op.hasConsumes = false;
op.hasProduces = false;
List<String> consumes = new ArrayList<String>(); List<String> consumes = new ArrayList<String>();
if (operation.getConsumes() != null && operation.getConsumes().size() > 0) { if (operation.getConsumes() != null) {
// use consumes defined in the operation if (operation.getConsumes().size() > 0) {
consumes = operation.getConsumes(); // use consumes defined in the operation
consumes = operation.getConsumes();
} else {
// empty list, do nothing to override global setting
}
} else if (swagger != null && swagger.getConsumes() != null && swagger.getConsumes().size() > 0) { } else if (swagger != null && swagger.getConsumes() != null && swagger.getConsumes().size() > 0) {
// use consumes defined globally // use consumes defined globally
consumes = swagger.getConsumes(); consumes = swagger.getConsumes();
LOGGER.debug("Using global consumes (" + swagger.getConsumes() + ") for " + op.operationId); LOGGER.debug("No consumes defined in operation. Using global consumes (" + swagger.getConsumes() + ") for " + op.operationId);
} }
// if "consumes" is defined (per operation or using global definition) // if "consumes" is defined (per operation or using global definition)
@ -933,13 +939,17 @@ public class DefaultCodegen {
} }
List<String> produces = new ArrayList<String>(); List<String> produces = new ArrayList<String>();
if (operation.getProduces() != null && operation.getProduces().size() > 0) { if (operation.getProduces() != null) {
// use produces defined in the operation if (operation.getProduces().size() > 0) {
produces = operation.getProduces(); // use produces defined in the operation
produces = operation.getProduces();
} else {
// empty list, do nothing to override global setting
}
} else if (swagger != null && swagger.getProduces() != null && swagger.getProduces().size() > 0) { } else if (swagger != null && swagger.getProduces() != null && swagger.getProduces().size() > 0) {
// use produces defined globally // use produces defined globally
produces = swagger.getProduces(); produces = swagger.getProduces();
LOGGER.debug("Using global produces (" + swagger.getProduces() + ") for " + op.operationId); LOGGER.debug("No produces defined in operation. Using global produces (" + swagger.getProduces() + ") for " + op.operationId);
} }
// if "produces" is defined (per operation or using global definition) // if "produces" is defined (per operation or using global definition)

View File

@ -139,4 +139,52 @@ public class CodegenTest {
Assert.assertTrue(op.bodyParam.isBinary); Assert.assertTrue(op.bodyParam.isBinary);
Assert.assertTrue(op.responses.get(0).isBinary); Assert.assertTrue(op.responses.get(0).isBinary);
} }
@Test(description = "use operation consumes and producus")
public void localConsumesAndProducesTest() {
final Swagger model = new SwaggerParser().read("src/test/resources/2_0/globalConsumesAndProduces.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/tests/localConsumesAndProduces";
final Operation p = model.getPaths().get(path).getGet();
CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions(), model);
Assert.assertTrue(op.hasConsumes);
Assert.assertEquals(op.consumes.size(), 1);
Assert.assertEquals(op.consumes.get(0).get("mediaType"), "application/json");
Assert.assertTrue(op.hasProduces);
Assert.assertEquals(op.produces.size(), 1);
Assert.assertEquals(op.produces.get(0).get("mediaType"), "application/json");
}
@Test(description = "use spec consumes and producus")
public void globalConsumesAndProducesTest() {
final Swagger model = new SwaggerParser().read("src/test/resources/2_0/globalConsumesAndProduces.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/tests/globalConsumesAndProduces";
final Operation p = model.getPaths().get(path).getGet();
CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions(), model);
Assert.assertTrue(op.hasConsumes);
Assert.assertEquals(op.consumes.size(), 1);
Assert.assertEquals(op.consumes.get(0).get("mediaType"), "application/global_consumes");
Assert.assertTrue(op.hasProduces);
Assert.assertEquals(op.produces.size(), 1);
Assert.assertEquals(op.produces.get(0).get("mediaType"), "application/global_produces");
}
@Test(description = "use spec consumes and producus (reset in operation with empty array)")
public void localResetConsumesAndProducesTest() {
final Swagger model = new SwaggerParser().read("src/test/resources/2_0/globalConsumesAndProduces.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/tests/localResetConsumesAndProduces";
final Operation p = model.getPaths().get(path).getGet();
CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions(), model);
Assert.assertNotNull(op);
Assert.assertFalse(op.hasConsumes);
Assert.assertNull(op.consumes);
Assert.assertFalse(op.hasProduces);
Assert.assertNull(op.produces);
}
} }