All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.resteasy.reactive.common.deployment.JsonDefaultProducersHandler Maven / Gradle / Ivy

There is a newer version: 3.15.2
Show newest version
package io.quarkus.resteasy.reactive.common.deployment;

import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.COLLECTION;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.LIST;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.MAP;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.SET;

import java.util.Collections;
import java.util.List;
import java.util.Set;

import jakarta.ws.rs.core.MediaType;

import org.jboss.jandex.DotName;
import org.jboss.jandex.Type;
import org.jboss.resteasy.reactive.common.processor.DefaultProducesHandler;

public class JsonDefaultProducersHandler implements DefaultProducesHandler {

    private static final List PRODUCES_APPLICATION_JSON = Collections.singletonList(MediaType.APPLICATION_JSON_TYPE);

    private static final Set SUPPORTED_JAVA_TYPES = Set.of(COLLECTION, LIST, SET, MAP);

    @Override
    public List handle(Context context) {
        if (context.config().isDefaultProduces() && isJsonCompatibleType(context)) {
            return PRODUCES_APPLICATION_JSON;
        }
        return Collections.emptyList();
    }

    private boolean isJsonCompatibleType(Context context) {
        if (context.httpMethod() == null) {
            // don't warn for subresources
            return false;
        }

        Type type = context.nonAsyncReturnType();
        // this doesn't catch every single case, but it should be good enough to cover most common cases
        if ((type.kind() != Type.Kind.CLASS) && (type.kind() != Type.Kind.PARAMETERIZED_TYPE)) {
            return false;
        }
        DotName dotName = type.name();
        if (dotName.toString().startsWith("java")) {
            return SUPPORTED_JAVA_TYPES.contains(dotName);
        }
        if (dotName.toString().startsWith("jakarta")) { // let's be forward compatible
            return false;
        }
        // check if the class is an application class
        return context.index().getClassByName(dotName) != null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy