Skip to content

Commit

Permalink
GERONIMO-6698 read defaultMediaType from the extension instead of pus…
Browse files Browse the repository at this point in the history
…hing it to the filter to workaround a jersey bug
  • Loading branch information
rmannibucau committed Feb 22, 2019
1 parent ee3fc82 commit 396da85
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
Expand Up @@ -33,7 +33,6 @@

import javax.enterprise.event.Observes;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.Bean;
Expand All @@ -57,7 +56,6 @@
import org.apache.geronimo.microprofile.openapi.impl.processor.AnnotationProcessor;
import org.apache.geronimo.microprofile.openapi.impl.processor.spi.NamingStrategy;
import org.apache.geronimo.microprofile.openapi.jaxrs.JacksonOpenAPIYamlBodyWriter;
import org.apache.geronimo.microprofile.openapi.jaxrs.OpenAPIFilter;
import org.eclipse.microprofile.openapi.OASConfig;
import org.eclipse.microprofile.openapi.OASFilter;
import org.eclipse.microprofile.openapi.OASModelReader;
Expand Down Expand Up @@ -94,6 +92,10 @@ void init(@Observes final BeforeBeanDiscovery beforeBeanDiscovery) {
}
}

public MediaType getDefaultMediaType() {
return jacksonIsPresent ? new MediaType("text", "vnd.yaml") : APPLICATION_JSON_TYPE;
}

private NamingStrategy loadNamingStrategy(final GeronimoOpenAPIConfig config) {
return ofNullable(config.read("model.operation.naming.strategy", null))
.map(String::trim)
Expand Down Expand Up @@ -126,14 +128,6 @@ <T> void findEndpointsAndApplication(@Observes final ProcessBean<T> event) {
}
}

void afterValidation(@Observes final AfterDeploymentValidation validation,
final BeanManager beanManager) {
final OpenAPIFilter filter = OpenAPIFilter.class.cast(
beanManager.getReference(beanManager.resolve(beanManager.getBeans(OpenAPIFilter.class)),
OpenAPIFilter.class, beanManager.createCreationalContext(null)));
filter.setDefaultMediaType(jacksonIsPresent ? new MediaType("text", "vnd.yaml") : APPLICATION_JSON_TYPE);
}

public OpenAPI getOrCreateOpenAPI(final Application application) {
if (classes != null) {
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
Expand Down
Expand Up @@ -16,11 +16,17 @@
*/
package org.apache.geronimo.microprofile.openapi.cxf;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;

import java.util.logging.Logger;

import javax.enterprise.inject.Vetoed;
import javax.enterprise.inject.spi.CDI;

import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.ext.JAXRSServerFactoryCustomizationExtension;
import org.apache.geronimo.microprofile.openapi.cdi.GeronimoOpenAPIExtension;
import org.apache.geronimo.microprofile.openapi.jaxrs.JacksonOpenAPIYamlBodyWriter;
import org.apache.geronimo.microprofile.openapi.jaxrs.OpenAPIFilter;

@Vetoed
Expand All @@ -30,6 +36,15 @@ public void customize(final JAXRSServerFactoryBean bean) {
if (bean.getProviders().stream().anyMatch(OpenAPIFilter.class::isInstance)) { // default app, nothing to do
return;
}
bean.setProvider(CDI.current().select(OpenAPIFilter.class).get());
final CDI<Object> current = CDI.current();
bean.setProvider(current.select(OpenAPIFilter.class).get());
try {
if (current.select(GeronimoOpenAPIExtension.class).get().getDefaultMediaType().equals(APPLICATION_JSON_TYPE)) {
return;
}
bean.setProvider(current.select(JacksonOpenAPIYamlBodyWriter.class).get());
} catch (final NoClassDefFoundError | RuntimeException cne) {
Logger.getLogger(CxfForceSetup.class.getName()).warning(cne.getMessage());
}
}
}
Expand Up @@ -23,8 +23,8 @@
import java.lang.annotation.Annotation;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Priority;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.ws.rs.HttpMethod;
Expand Down Expand Up @@ -60,6 +60,11 @@ public class OpenAPIFilter implements ContainerRequestFilter {
private OpenAPI openApi;
private MediaType defaultMediaType;

@PostConstruct
private void init() {
defaultMediaType = defaultMediaType == null ? extension.getDefaultMediaType() : defaultMediaType;
}

@Override
public void filter(final ContainerRequestContext rc) {
if (!HttpMethod.GET.equals(rc.getRequest().getMethod())) {
Expand Down

0 comments on commit 396da85

Please sign in to comment.