Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -941,13 +942,16 @@ public void preprocessSwagger(Swagger swagger) {
}
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
})
Expand Down Expand Up @@ -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}}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import io.swagger.codegen.CodegenType;
import io.swagger.codegen.languages.AbstractJavaCodegen;
import io.swagger.models.*;
import io.swagger.models.parameters.*;

public class AbstractJavaCodegenTest {

Expand Down Expand Up @@ -49,4 +51,50 @@ public void toModelNameShouldUseProvidedMapping() throws Exception {
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"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
})
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -97,7 +94,6 @@ public FindPetsByStatusQueryParams status(final List<String> value) {
*/
@RequestLine("GET /pet/findByTags?tags={tags}")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
List<Pet> findPetsByTags(@Param("tags") List<String> tags);
Expand All @@ -119,7 +115,6 @@ public FindPetsByStatusQueryParams status(final List<String> value) {
*/
@RequestLine("GET /pet/findByTags?tags={tags}")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
List<Pet> findPetsByTags(@QueryMap(encoded=true) Map<String, Object> queryParams);
Expand All @@ -143,7 +138,6 @@ public FindPetsByTagsQueryParams tags(final List<String> value) {
*/
@RequestLine("GET /pet/{petId}")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
Pet getPetById(@Param("petId") Long petId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -134,7 +130,6 @@ public LoginUserQueryParams password(final String value) {
*/
@RequestLine("GET /user/logout")
@Headers({
"Content-Type: application/json",
"Accept: application/json",
})
void logoutUser();
Expand Down
1 change: 0 additions & 1 deletion samples/client/petstore/java/retrofit2rx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@
</dependency>



<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
Expand Down
1 change: 0 additions & 1 deletion samples/client/petstore/java/retrofit2rx2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@
</dependency>



<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
Expand Down