Revert "Add support for top-level consumes and produces"

This commit is contained in:
wing328 2015-10-13 23:47:42 +08:00
parent c7b08bc112
commit 0b4b5e8839
6 changed files with 10 additions and 228 deletions

View File

@ -67,8 +67,6 @@ public interface CodegenConfig {
CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions); CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions);
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger);
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions); CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions);
List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes); List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes);

View File

@ -869,10 +869,6 @@ public class DefaultCodegen {
} }
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) { public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
return fromOperation(path, httpMethod, operation, definitions, null);
}
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
CodegenOperation op = CodegenModelFactory.newInstance(CodegenModelType.OPERATION); CodegenOperation op = CodegenModelFactory.newInstance(CodegenModelType.OPERATION);
Set<String> imports = new HashSet<String>(); Set<String> imports = new HashSet<String>();
op.vendorExtensions = operation.getVendorExtensions(); op.vendorExtensions = operation.getVendorExtensions();
@ -908,32 +904,15 @@ 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>(); 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("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 != null && consumes.size() > 0) {
List<Map<String, String>> c = new ArrayList<Map<String, String>>(); List<Map<String, String>> c = new ArrayList<Map<String, String>>();
int count = 0; int count = 0;
for (String key : consumes) { for (String key : operation.getConsumes()) {
Map<String, String> mediaType = new HashMap<String, String>(); Map<String, String> mediaType = new HashMap<String, String>();
mediaType.put("mediaType", key); mediaType.put("mediaType", key);
count += 1; count += 1;
if (count < consumes.size()) { if (count < operation.getConsumes().size()) {
mediaType.put("hasMore", "true"); mediaType.put("hasMore", "true");
} else { } else {
mediaType.put("hasMore", null); mediaType.put("hasMore", null);
@ -944,29 +923,14 @@ public class DefaultCodegen {
op.hasConsumes = true; op.hasConsumes = true;
} }
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("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 != null && produces.size() > 0) {
List<Map<String, String>> c = new ArrayList<Map<String, String>>(); List<Map<String, String>> c = new ArrayList<Map<String, String>>();
int count = 0; int count = 0;
for (String key : produces) { for (String key : operation.getProduces()) {
Map<String, String> mediaType = new HashMap<String, String>(); Map<String, String> mediaType = new HashMap<String, String>();
mediaType.put("mediaType", key); mediaType.put("mediaType", key);
count += 1; count += 1;
if (count < produces.size()) { if (count < operation.getProduces().size()) {
mediaType.put("hasMore", "true"); mediaType.put("hasMore", "true");
} else { } else {
mediaType.put("hasMore", null); mediaType.put("hasMore", null);

View File

@ -470,7 +470,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
for (String tag : tags) { for (String tag : tags) {
CodegenOperation co = null; CodegenOperation co = null;
try { try {
co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions(), swagger); co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions());
co.tags = new ArrayList<String>(); co.tags = new ArrayList<String>();
co.tags.add(sanitizeTag(tag)); co.tags.add(sanitizeTag(tag));
config.addOperationToGroup(sanitizeTag(tag), resourcePath, operation, co, operations); config.addOperationToGroup(sanitizeTag(tag), resourcePath, operation, co, operations);

View File

@ -1,11 +1,9 @@
package io.swagger.codegen.languages; package io.swagger.codegen.languages;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.Iterators; import com.google.common.collect.Iterators;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import io.swagger.codegen.*; import io.swagger.codegen.*;
import io.swagger.models.Swagger;
import io.swagger.models.Model; import io.swagger.models.Model;
import io.swagger.models.Operation; import io.swagger.models.Operation;
import io.swagger.models.parameters.HeaderParameter; import io.swagger.models.parameters.HeaderParameter;
@ -258,7 +256,7 @@ public class SwiftCodegen extends DefaultCodegen implements CodegenConfig {
} }
@Override @Override
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) { public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
path = normalizePath(path); path = normalizePath(path);
List<Parameter> parameters = operation.getParameters(); List<Parameter> parameters = operation.getParameters();
parameters = Lists.newArrayList(Iterators.filter(parameters.iterator(), new Predicate<Parameter>() { parameters = Lists.newArrayList(Iterators.filter(parameters.iterator(), new Predicate<Parameter>() {
@ -268,7 +266,7 @@ public class SwiftCodegen extends DefaultCodegen implements CodegenConfig {
} }
})); }));
operation.setParameters(parameters); operation.setParameters(parameters);
return super.fromOperation(path, httpMethod, operation, definitions, swagger); return super.fromOperation(path, httpMethod, operation, definitions);
} }
private static String normalizePath(String path) { private static String normalizePath(String path) {

View File

@ -139,52 +139,4 @@ 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 produces")
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 produces")
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 operation consumes and produces (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);
}
} }

View File

@ -1,130 +0,0 @@
{
"swagger": "2.0",
"info": {
"description": "Spec for testing global consumes and produces",
"version": "1.0.0",
"title": "Swagger Petstore",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"email": "apiteam@swagger.io"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"host": "petstore.swagger.io",
"basePath": "/v2",
"consumes": ["application/global_consumes"],
"produces": ["application/global_produces"],
"schemes": [
"http"
],
"paths": {
"/tests/localConsumesAndProduces": {
"get": {
"tags": [
"tests"
],
"summary": "Operation with local consumes and produces",
"description": "",
"operationId": "localConsumesAndProduces",
"produces": [
"application/json"
],
"consumes": [
"application/json"
],
"parameters": [
],
"responses": {
"200": {
"description": "successful operation. Returning a simple int.",
"schema": {
"type": "integer",
"format": "int64"
}
}
}
}
},
"/tests/globalConsumesAndProduces": {
"get": {
"tags": [
"tests"
],
"summary": "Operation with global consumes and produces",
"description": "",
"operationId": "globalConsumesAndProduces",
"parameters": [
],
"responses": {
"200": {
"description": "successful operation. Returning a simple int.",
"schema": {
"type": "integer",
"format": "int64"
}
}
}
}
},
"/tests/localResetConsumesAndProduces": {
"get": {
"tags": [
"tests"
],
"summary": "Operation with local consumes and produces set to empty (reset)",
"description": "",
"operationId": "localResetConsumesAndProduces",
"parameters": [
],
"consumes": [],
"produces": [],
"responses": {
"200": {
"description": "successful operation. Returning a simple int.",
"schema": {
"type": "integer",
"format": "int64"
}
}
}
}
}
},
"securityDefinitions": {
"api_key": {
"type": "apiKey",
"name": "api_key",
"in": "header"
},
"petstore_auth": {
"type": "oauth2",
"authorizationUrl": "http://petstore.swagger.io/api/oauth/dialog",
"flow": "implicit",
"scopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
}
},
"definitions": {
"CustomModel": {
"required": [
"id"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string",
"example": "doggie"
}
}
}
}
}