I have a Swagger interface with multiple URLs. I have split this into three API groups.
Endpoints starting with: /employee, /customer and all the rest (including this /employee and /customer).
It looks like this:
class OpenApiSwaggerConfiguration {
@Bean
fun ClientApi(globalHeaderConfiguration: GlobalHeaderConfiguration?): GroupedOpenApi {
return GroupedOpenApi.builder()
.group(CLIENT_NAME)
.pathsToExclude("/error/**")
.addOperationCustomizer(globalHeaderConfiguration)
.build()
}
@Bean
fun EmployeeApi(): GroupedOpenApi {
return GroupedOpenApi.builder()
.group(EMPLOYEE_NAME)
.pathsToMatch("/employee/**")
.pathsToExclude("/error/**")
.build()
}
@Bean
fun CustomerApi(customerHeaderConfiguration: CustomerHeaderConfiguration): GroupedOpenApi {
return GroupedOpenApi.builder()
.group(CUSTOMER_NAME)
.pathsToMatch("/customer/**")
.pathsToExclude("/error/**")
.addOperationCustomizer(customerHeaderConfiguration)
.build()
}
}
For endpint /customer, I want to always add the Caller field as a parameter, which is why I added the coffiguration .addOperationCustomizer(customerHeaderConfiguration).
For /employee, on the other hand, I don’t want to add the Caller field at all, hence there is no coffiguration defined there.
The problem arises for the ClientApi, as it displays all endpoints so far, and contains the .addOperationCustomizer(globalHeaderConfiguration) configuration, which adds a caller wherever another parameter is added, then appends a new caller field to this. However, along the lines of what I’ve previously stated, I’d like to improve this somehow so that it takes into account those two previous conditions, i.e. always add a Caller field to /customer regardless of whether there is already a parameter, but don’t add any Caller to /employee. That is, display all endpoints but take into account the conditions from EmployeeApi and CustomerApi. I tried to do this with some exclude but don’t know how to do this.
Is there any way to do this?
@Component
public class GlobalHeaderConfiguration implements OperationCustomizer {
@Override
public Operation customize(Operation operation, HandlerMethod handlerMethod) {
Parameter customHeaderVersion = new Parameter().in(ParameterIn.HEADER.toString())
.name("Caller")
.description("JWT")
.schema(new StringSchema())
.example(JWT)
.required(false);
List<Parameter> parameterList = operation.getParameters();
if (parameterList!=null && !parameterList.contains(customHeaderVersion)) {
operation.addParametersItem(customHeaderVersion);
Collections.rotate(parameterList, 1);
}
return operation;
}
}
@Component
public class CustomerHeaderConfiguration implements OperationCustomizer {
@Override
public Operation customize(Operation operation, HandlerMethod handlerMethod) {
Parameter customHeaderVersion = new Parameter().in(ParameterIn.HEADER.toString())
.name("Caller")
.description("JWT")
.schema(new StringSchema())
.example(JWT)
.required(false);
List<Parameter> parameterList = operation.getParameters();
if (parameterList == null) {
parameterList = new ArrayList<>();
operation.setParameters(parameterList);
}
if (!parameterList.contains(customHeaderVersion)) {
operation.addParametersItem(customHeaderVersion);
Collections.rotate(parameterList, 1);
}
return operation;
}
}
user25675382 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.