OpenApi customization

151 views
Skip to first unread message

Pablo

unread,
Jul 18, 2023, 8:06:24 AM7/18/23
to HAPI FHIR
Hi All

We have a spring boot (2.7.13) project with hapi fhir version 6.6.2. where we have configured OpenApi 

    OpenApiInterceptor openApiInterceptor = new OpenApiInterceptor();
    registerInterceptor(openApiInterceptor);

We need to add a header field as mandatory in all endpoints (called trackId). Then I would like to customize OpenApi to say that this header is mandatory.

Do you know how could be customize the hapi open api ?

Thanks in advance.

Our dependencies are:
    <dependency>
      <groupId>ca.uhn.hapi.fhir</groupId>
      <artifactId>hapi-fhir-base</artifactId>
      <version>${hapi-fhir.version}</version>
    </dependency>
    <dependency>
      <groupId>ca.uhn.hapi.fhir</groupId>
      <artifactId>hapi-fhir-client</artifactId>
      <version>${hapi-fhir.version}</version>
    </dependency>
    <dependency>
      <groupId>ca.uhn.hapi.fhir</groupId>
      <artifactId>hapi-fhir-structures-r4b</artifactId>
      <version>${hapi-fhir.version}</version>
    </dependency>
    <dependency>
      <groupId>ca.uhn.hapi.fhir</groupId>
      <artifactId>hapi-fhir-server</artifactId>
      <version>${hapi-fhir.version}</version>
    </dependency>
    <dependency>
      <groupId>ca.uhn.hapi.fhir</groupId>
      <artifactId>hapi-fhir-server-openapi</artifactId>
      <version>${hapi-fhir.version}</version>
    </dependency>


James Agnew

unread,
Jul 18, 2023, 8:16:29 AM7/18/23
to Pablo, HAPI FHIR
Probably the easiest thing would be to extend OpenApiInterceptor and override the "generateOpenApi" method. You can customize the internally generated schema before returning it.

Cheers,
James

--
You received this message because you are subscribed to the Google Groups "HAPI FHIR" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hapi-fhir+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hapi-fhir/3bf83a3b-e625-41f1-84f7-a29ab52a12d2n%40googlegroups.com.

Pablo

unread,
Jul 18, 2023, 10:17:34 AM7/18/23
to James Agnew, HAPI FHIR
Hi James, thanks for your quick response. Finally I have got it!! let me share with all.

@WebServlet(urlPatterns = { "/fhir/*" }, displayName = "FHIR Server")
@Configuration
@Log4j2
@RequiredArgsConstructor
public class RestfulServerConfig extends RestfulServer
{
private final ApplicationContext applicationContext;
private final DomainStorage domainStorage;

@Generated
@Override
protected void initialize() throws ServletException
{
super.initialize();
//create a context for the appropriate version
setFhirContext(FhirContext.forR4B());
setDefaultPrettyPrint(true);
//Register Resource Providers
setResourceProviders(Arrays.asList(
applicationContext.getBean(CommunicationProvider.class),
applicationContext.getBean(DeviceProvider.class),
applicationContext.getBean(DeviceMetricProvider.class),
applicationContext.getBean(SpecimenProvider.class)));

registerInterceptor(new CustomizeOpenApiInterceptor());

}
}
CustomizeOpenApiInterceptor


public class CustomizeOpenApiInterceptor extends OpenApiInterceptor
{

private static final List<Function<PathItem, Operation>> OPERATION_GETTERS = Arrays.asList(
PathItem::getGet, PathItem::getPost, PathItem::getDelete, PathItem::getHead,
PathItem::getOptions, PathItem::getPatch, PathItem::getPut);

private Stream<Operation> getOperations(PathItem pathItem)
{
return OPERATION_GETTERS.stream()
.map(getter -> getter.apply(pathItem))
.filter(Objects::nonNull);
}

public void customise(OpenAPI openApi)
{
openApi.getPaths().values().stream()
.flatMap(this::getOperations)
.forEach(this::customize);
}

@Override
protected OpenAPI generateOpenApi(ServletRequestDetails theRequestDetails)
{
OpenAPI openAPI = super.generateOpenApi(theRequestDetails);

openAPI.getPaths().values().stream()
.flatMap(this::getOperations)
.forEach(this::customize);

return openAPI;
}

private void customize(Operation operation)
{
operation.addParametersItem(
new Parameter()
.in("header")
.required(true)
.description("trackId is mandatory header")
.name("TRACKID"));
}
}

Thanks

Richard Braman

unread,
Jul 19, 2023, 1:15:28 AM7/19/23
to Pablo, James Agnew, HAPI FHIR
Hey Pablo, Will OpenAI API be responding to the FHIR requests?  We have been doing some research in this space too :)





--

Pablo

unread,
Jul 19, 2023, 3:17:01 AM7/19/23
to Richard Braman, James Agnew, HAPI FHIR
Yes, with my code swagger is adding the header in the UI. This header is mandatory
image.png
Reply all
Reply to author
Forward
0 new messages