diff --git a/src/main/java/io/swagger/inflector/controllers/SwaggerOperationController.java b/src/main/java/io/swagger/inflector/controllers/SwaggerOperationController.java index 22ed5093..b045c51c 100644 --- a/src/main/java/io/swagger/inflector/controllers/SwaggerOperationController.java +++ b/src/main/java/io/swagger/inflector/controllers/SwaggerOperationController.java @@ -29,6 +29,7 @@ import io.swagger.inflector.models.ApiError; import io.swagger.inflector.models.RequestContext; import io.swagger.inflector.models.ResponseContext; +import io.swagger.inflector.processors.EntityProcessor; import io.swagger.inflector.processors.EntityProcessorFactory; import io.swagger.inflector.schema.SchemaValidator; import io.swagger.inflector.utils.ApiErrorUtils; @@ -561,8 +562,6 @@ public Response apply(ContainerRequestContext ctx) { ResponseBuilder builder = Response.status(code); io.swagger.models.Response response = responses.get(defaultKey); - Map responseHeaders = null; - if(response.getHeaders() != null && response.getHeaders().size() > 0) { for(String key: response.getHeaders().keySet()) { Property headerProperty = response.getHeaders().get(key); @@ -601,8 +600,25 @@ else if(output instanceof ObjectExample) { setContentType(requestContext, resp, operation); builder.entity(output); if (resp.getContentType() != null) { + // this comes from the operation itself builder.type(resp.getContentType()); } + else { + // get acceptable content types + List processors = EntityProcessorFactory.getProcessors(); + + // take first compatible one + for (EntityProcessor processor : processors) { + for (MediaType mt : requestContext.getAcceptableMediaTypes()) { + LOGGER.debug("checking type " + mt.toString() + " against " + processor.getClass().getName()); + if (processor.supports(mt)) { + builder.type(mt); + break; + } + } + } + } + builder.entity(output); } diff --git a/src/test/java/io/swagger/test/examples/ResponseExamplesTest.java b/src/test/java/io/swagger/test/examples/ResponseExamplesTest.java index 1422bffb..cb9323da 100644 --- a/src/test/java/io/swagger/test/examples/ResponseExamplesTest.java +++ b/src/test/java/io/swagger/test/examples/ResponseExamplesTest.java @@ -51,7 +51,6 @@ public class ResponseExamplesTest { @Test public void testResponseJsonExample() throws Exception { - Configuration config = new Configuration(); Swagger swagger = new SwaggerParser().read( "src/test/swagger/sample1.yaml"); Operation operation = swagger.getPath( "/mockResponses/responseWithExamples").getGet(); diff --git a/src/test/java/io/swagger/test/integration/responses/SchemaResponseIT.java b/src/test/java/io/swagger/test/integration/responses/SchemaResponseIT.java index 8bbfe009..de72a628 100644 --- a/src/test/java/io/swagger/test/integration/responses/SchemaResponseIT.java +++ b/src/test/java/io/swagger/test/integration/responses/SchemaResponseIT.java @@ -94,4 +94,28 @@ public void testResponseHeaders() throws Exception { assertEquals(value.get(0).toString(), "[http://foo.bar/other]"); } } + + /** + * ensure the response honors the produces type with no explicit Accept header + * @throws Exception + */ + @Test + public void testProducesType() throws Exception { + String response = client.invokeAPI("/producesTest", "GET", new HashMap(), new HashMap(), + new HashMap(), null, null, null, new String[0]); + + assertEquals("{\"name\":\"string\"}", response); + } + + /** + * ensure the response honors the produces type with incompatible Accept header + * @throws Exception + */ + @Test + public void testProducesTypeWithConflict() throws Exception { + String response = client.invokeAPI("/producesTest", "GET", new HashMap(), new HashMap(), + new HashMap(), null, null, "application/xml", new String[0]); + + assertEquals("{\"name\":\"string\"}", response); + } } diff --git a/src/test/swagger/sample1.yaml b/src/test/swagger/sample1.yaml index be595e0a..cd588d8d 100644 --- a/src/test/swagger/sample1.yaml +++ b/src/test/swagger/sample1.yaml @@ -16,6 +16,19 @@ info: basePath: /api paths: + /producesTest: + get: + produces: + - application/json + parameters: [] + responses: + 200: + description: it works + schema: + type: object + properties: + name: + type: string /fileUpload: post: x-swagger-router-controller: TestController