forked from loafle/openapi-generator-original
add test case for global consumes and produces
This commit is contained in:
parent
9311dcaccb
commit
5144c54895
@ -902,15 +902,21 @@ public class DefaultCodegen {
|
||||
op.summary = escapeText(operation.getSummary());
|
||||
op.notes = escapeText(operation.getDescription());
|
||||
op.tags = operation.getTags();
|
||||
op.hasConsumes = false;
|
||||
op.hasProduces = false;
|
||||
|
||||
List<String> consumes = new ArrayList<String>();
|
||||
if (operation.getConsumes() != null && operation.getConsumes().size() > 0) {
|
||||
if (operation.getConsumes() != null) {
|
||||
if (operation.getConsumes().size() > 0) {
|
||||
// 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) {
|
||||
// use consumes defined globally
|
||||
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)
|
||||
@ -933,13 +939,17 @@ public class DefaultCodegen {
|
||||
}
|
||||
|
||||
List<String> produces = new ArrayList<String>();
|
||||
if (operation.getProduces() != null && operation.getProduces().size() > 0) {
|
||||
if (operation.getProduces() != null) {
|
||||
if (operation.getProduces().size() > 0) {
|
||||
// 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) {
|
||||
// use produces defined globally
|
||||
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)
|
||||
|
@ -139,4 +139,52 @@ public class CodegenTest {
|
||||
Assert.assertTrue(op.bodyParam.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);
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user