hapifhir / hapi-fhir

🔥 HAPI FHIR - Java API for HL7 FHIR Clients and Servers
http://hapifhir.io
Apache License 2.0
2k stars 1.31k forks source link

HAPI FHIR JAX-RS server endpoints always returning prettified JSON #5568

Open AndersMySugr opened 9 months ago

AndersMySugr commented 9 months ago

Description The endpoints in our server annotated with the HAPI FHIR JAX-RS annotations documented here always return prettified JSON (added whitespace + newlines) despite the HAPI FHIR documentation stating that pretty print of JSON should be disabled by default.

We are using DSTU3.

Package: package ca.uhn.fhir.rest.annotation;

To Reproduce Steps to reproduce the behavior:

  1. Implement a REST endpoint and annotate it with @READ from this library (it also occurs for @UPDATE and @CREATE): Package: package ca.uhn.fhir.rest.annotation; 2) Call the endpoint and return a FHIR Domain resource. I used Observation DSTU3.

Expected behavior A response with a body in JSON format in one long line non-prettified, optimized for machine-to-machine communication.

Observed behavior Response body is prettified containing newlines and whitespace.

Screenshots Below screenshot shows an example of the issue:

image

The same endpoint implemented without HAPI FHIR like below screenshot, correctly produces a non-prettified response:

image

Environment (please complete the following information):

Additional context The spring.jackson.serialization.indent_output is not defined in our project, nor are any other jackson objectmapper properties. As far as i can tell, then the default HAPI FHIR parser, prettifies the output despite documentation stating otherwise. And i cannot figure out how to disable it.

AndersMySugr commented 8 months ago

So for us the solution was to override the isDefaultPrettyPrint method in our class that extends the AbstractJaxRsResourceProvider, making it return false instead of true.

The thing is that AbstractJaxRsResourceProvider extends AbstractJaxRsProvider which by default returns true when isDefaultPrettyPrint is called.

Given that the HAPI FHIR documentation here states that it is false by default and Spring also has this disabled by default, then this should probably be changed to return false instead of true.