Skip to content

Instantly share code, notes, and snippets.

@eladchen
Created April 19, 2020 07:57
Show Gist options
  • Save eladchen/c084fef2c9a980e0b20a85f5a7b1dfb9 to your computer and use it in GitHub Desktop.
Save eladchen/c084fef2c9a980e0b20a85f5a7b1dfb9 to your computer and use it in GitHub Desktop.
An example of OpenAPISpecFilter using CDI
package com.example;
import io.swagger.v3.core.filter.OpenAPISpecFilter;
import io.swagger.v3.core.model.ApiDescription;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.CDI;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class CdiDelegatedOpenAPISpecFilter implements OpenAPISpecFilter {
private OpenAPISpecFilter openAPISpecFilter;
@SuppressWarnings("unchecked")
private OpenAPISpecFilter openAPISpecFilter() {
if (openAPISpecFilter == null) {
BeanManager beanManager = CDI.current().getBeanManager();
Bean<OpenAPISpecFilter> bean = (Bean<OpenAPISpecFilter>)beanManager.resolve(beanManager.getBeans(OpenAPISpecFilter.class));
openAPISpecFilter = (OpenAPISpecFilter)beanManager.getReference(bean, bean.getBeanClass(), beanManager.createCreationalContext(bean));
}
return openAPISpecFilter;
}
@Override
public Optional<OpenAPI> filterOpenAPI(OpenAPI openAPI, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
return openAPISpecFilter().filterOpenAPI(openAPI, params, cookies, headers);
}
@Override
public Optional<PathItem> filterPathItem(PathItem pathItem, ApiDescription api, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
return openAPISpecFilter().filterPathItem(pathItem, api, params, cookies, headers);
}
@Override
public Optional<Operation> filterOperation(Operation operation, ApiDescription api, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
return openAPISpecFilter().filterOperation(operation, api, params, cookies, headers);
}
@Override
public Optional<Parameter> filterParameter(Parameter parameter, Operation operation, ApiDescription api, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
return openAPISpecFilter().filterParameter(parameter, operation, api, params, cookies, headers);
}
@Override
public Optional<RequestBody> filterRequestBody(RequestBody requestBody, Operation operation, ApiDescription api, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
return openAPISpecFilter().filterRequestBody(requestBody, operation, api, params, cookies, headers);
}
@Override
public Optional<ApiResponse> filterResponse(ApiResponse response, Operation operation, ApiDescription api, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
return openAPISpecFilter().filterResponse(response, operation, api, params, cookies, headers);
}
@Override
public Optional<Schema> filterSchema(Schema schema, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
return openAPISpecFilter().filterSchema(schema, params, cookies, headers);
}
@Override
public Optional<Schema> filterSchemaProperty(Schema property, Schema schema, String propName, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
return openAPISpecFilter().filterSchemaProperty(property, schema, propName, params, cookies, headers);
}
@Override
public boolean isRemovingUnreferencedDefinitions() {
return openAPISpecFilter().isRemovingUnreferencedDefinitions();
}
}
@eladchen
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment