Do not apply content-type header if no request body (#6648)

* Do not apply content-type header if no request body

* Set content-type header if there are form params

* Add tests

* Run update script

* Regenerated sample clients
This commit is contained in:
Ben Mordue 2017-11-27 10:33:28 +00:00 committed by William Cheng
parent a8642dbdfc
commit 02bcc55e23
8 changed files with 58 additions and 22 deletions

View File

@ -30,6 +30,7 @@ import io.swagger.models.Model;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.BodyParameter;
import io.swagger.models.parameters.FormParameter;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.properties.ArrayProperty;
@ -941,13 +942,16 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
}
for (Operation operation : path.getOperations()) {
boolean hasFormParameters = false;
boolean hasBodyParameters = false;
for (Parameter parameter : operation.getParameters()) {
if (parameter instanceof FormParameter) {
hasFormParameters = true;
}
if (parameter instanceof BodyParameter) {
hasBodyParameters = true;
}
}
//only add content-Type if its no a GET-Method
if(path.getGet() != null || ! operation.equals(path.getGet())){
if (hasBodyParameters || hasFormParameters){
String defaultContentType = hasFormParameters ? "application/x-www-form-urlencoded" : "application/json";
String contentType = operation.getConsumes() == null || operation.getConsumes().isEmpty() ? defaultContentType : operation.getConsumes().get(0);
operation.setVendorExtension("x-contentType", contentType);

View File

@ -37,8 +37,8 @@ public interface {{classname}} extends ApiClient.Api {
*/
@RequestLine("{{httpMethod}} {{{path}}}{{#hasQueryParams}}?{{/hasQueryParams}}{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{#hasMore}}&{{/hasMore}}{{/queryParams}}")
@Headers({
"Content-Type: {{vendorExtensions.x-contentType}}",
"Accept: {{vendorExtensions.x-accepts}}",{{#headerParams}}
{{#vendorExtensions.x-contentType}} "Content-Type: {{vendorExtensions.x-contentType}}",
{{/vendorExtensions.x-contentType}} "Accept: {{vendorExtensions.x-accepts}}",{{#headerParams}}
"{{baseName}}: {{=<% %>=}}{<%paramName%>}<%={{ }}=%>"{{#hasMore}},
{{/hasMore}}{{/headerParams}}
})
@ -75,8 +75,8 @@ public interface {{classname}} extends ApiClient.Api {
*/
@RequestLine("{{httpMethod}} {{{path}}}?{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{#hasMore}}&{{/hasMore}}{{/queryParams}}")
@Headers({
"Content-Type: {{vendorExtensions.x-contentType}}",
"Accept: {{vendorExtensions.x-accepts}}",{{#headerParams}}
{{#vendorExtensions.x-contentType}} "Content-Type: {{vendorExtensions.x-contentType}}",
{{/vendorExtensions.x-contentType}} "Accept: {{vendorExtensions.x-accepts}}",{{#headerParams}}
"{{baseName}}: {{=<% %>=}}{<%paramName%>}<%={{ }}=%>"{{#hasMore}},
{{/hasMore}}{{/headerParams}}
})

View File

@ -5,6 +5,8 @@ import org.testng.annotations.Test;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.languages.AbstractJavaCodegen;
import io.swagger.models.*;
import io.swagger.models.parameters.*;
public class AbstractJavaCodegenTest {
@ -49,4 +51,50 @@ public class AbstractJavaCodegenTest {
public void toModelNameUsesPascalCase() throws Exception {
Assert.assertEquals("JsonAnotherclass", fakeJavaCodegen.toModelName("json_anotherclass"));
}
@Test
public void preprocessSwaggerWithFormParamsSetsContentType() {
Path dummyPath = new Path()
.post(new Operation().parameter(new FormParameter()))
.get(new Operation());
Swagger swagger = new Swagger()
.path("dummy", dummyPath);
fakeJavaCodegen.preprocessSwagger(swagger);
Assert.assertNull(swagger.getPath("dummy").getGet().getVendorExtensions().get("x-contentType"));
Assert.assertEquals(swagger.getPath("dummy").getPost().getVendorExtensions().get("x-contentType"), "application/x-www-form-urlencoded");
}
@Test
public void preprocessSwaggerWithBodyParamsSetsContentType() {
Path dummyPath = new Path()
.post(new Operation().parameter(new BodyParameter()))
.get(new Operation());
Swagger swagger = new Swagger()
.path("dummy", dummyPath);
fakeJavaCodegen.preprocessSwagger(swagger);
Assert.assertNull(swagger.getPath("dummy").getGet().getVendorExtensions().get("x-contentType"));
Assert.assertEquals(swagger.getPath("dummy").getPost().getVendorExtensions().get("x-contentType"), "application/json");
}
@Test
public void preprocessSwaggerWithNoFormOrBodyParamsDoesNotSetContentType() {
Path dummyPath = new Path()
.post(new Operation())
.get(new Operation());
Swagger swagger = new Swagger()
.path("dummy", dummyPath);
fakeJavaCodegen.preprocessSwagger(swagger);
Assert.assertNull(swagger.getPath("dummy").getGet().getVendorExtensions().get("x-contentType"));
Assert.assertNull(swagger.getPath("dummy").getPost().getVendorExtensions().get("x-contentType"));
}
}

View File

@ -37,7 +37,6 @@ public interface PetApi extends ApiClient.Api {
*/
@RequestLine("DELETE /pet/{petId}")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
"api_key: {apiKey}"
})
@ -51,7 +50,6 @@ public interface PetApi extends ApiClient.Api {
*/
@RequestLine("GET /pet/findByStatus?status={status}")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
List<Pet> findPetsByStatus(@Param("status") List<String> status);
@ -73,7 +71,6 @@ public interface PetApi extends ApiClient.Api {
*/
@RequestLine("GET /pet/findByStatus?status={status}")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
List<Pet> findPetsByStatus(@QueryMap(encoded=true) Map<String, Object> queryParams);
@ -97,7 +94,6 @@ public interface PetApi extends ApiClient.Api {
*/
@RequestLine("GET /pet/findByTags?tags={tags}")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
List<Pet> findPetsByTags(@Param("tags") List<String> tags);
@ -119,7 +115,6 @@ public interface PetApi extends ApiClient.Api {
*/
@RequestLine("GET /pet/findByTags?tags={tags}")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
List<Pet> findPetsByTags(@QueryMap(encoded=true) Map<String, Object> queryParams);
@ -143,7 +138,6 @@ public interface PetApi extends ApiClient.Api {
*/
@RequestLine("GET /pet/{petId}")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
Pet getPetById(@Param("petId") Long petId);

View File

@ -22,7 +22,6 @@ public interface StoreApi extends ApiClient.Api {
*/
@RequestLine("DELETE /store/order/{orderId}")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
void deleteOrder(@Param("orderId") String orderId);
@ -34,7 +33,6 @@ public interface StoreApi extends ApiClient.Api {
*/
@RequestLine("GET /store/inventory")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
Map<String, Integer> getInventory();
@ -47,7 +45,6 @@ public interface StoreApi extends ApiClient.Api {
*/
@RequestLine("GET /store/order/{orderId}")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
Order getOrderById(@Param("orderId") Long orderId);

View File

@ -58,7 +58,6 @@ public interface UserApi extends ApiClient.Api {
*/
@RequestLine("DELETE /user/{username}")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
void deleteUser(@Param("username") String username);
@ -71,7 +70,6 @@ public interface UserApi extends ApiClient.Api {
*/
@RequestLine("GET /user/{username}")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
User getUserByName(@Param("username") String username);
@ -85,7 +83,6 @@ public interface UserApi extends ApiClient.Api {
*/
@RequestLine("GET /user/login?username={username}&password={password}")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
String loginUser(@Param("username") String username, @Param("password") String password);
@ -108,7 +105,6 @@ public interface UserApi extends ApiClient.Api {
*/
@RequestLine("GET /user/login?username={username}&password={password}")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
String loginUser(@QueryMap(encoded=true) Map<String, Object> queryParams);
@ -134,7 +130,6 @@ public interface UserApi extends ApiClient.Api {
*/
@RequestLine("GET /user/logout")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
void logoutUser();

View File

@ -235,7 +235,6 @@
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>

View File

@ -235,7 +235,6 @@
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>