Search in sources :

Example 1 with ArgumentDefinition

use of com.palantir.conjure.spec.ArgumentDefinition in project conjure-java by palantir.

the class JerseyServiceGenerator method generateServiceMethod.

private MethodSpec generateServiceMethod(EndpointDefinition endpointDef, TypeMapper returnTypeMapper, TypeMapper argumentTypeMapper) {
    TypeName returnType = endpointDef.getReturns().map(returnTypeMapper::getClassName).orElse(ClassName.VOID);
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(endpointDef.getEndpointName().get()).addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).addAnnotation(httpMethodToClassName(endpointDef.getHttpMethod().get().name())).addParameters(createServiceMethodParameters(endpointDef, argumentTypeMapper, true)).returns(returnType);
    // @Path("") is invalid in Feign JaxRs and equivalent to absent on an endpoint method
    String rawHttpPath = endpointDef.getHttpPath().get();
    String httpPath = rawHttpPath.startsWith("/") ? rawHttpPath.substring(1) : rawHttpPath;
    if (!httpPath.isEmpty()) {
        methodBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "Path")).addMember("value", "$S", httpPath).build());
    }
    if (returnType.equals(BINARY_RETURN_TYPE_OUTPUT) || returnType.equals(BINARY_RETURN_TYPE_RESPONSE) || returnType.equals(OPTIONAL_BINARY_RETURN_TYPE)) {
        methodBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "Produces")).addMember("value", "$T.APPLICATION_OCTET_STREAM", ClassName.get("javax.ws.rs.core", "MediaType")).build());
    }
    boolean consumesTypeIsBinary = endpointDef.getArgs().stream().map(ArgumentDefinition::getType).map(argumentTypeMapper::getClassName).anyMatch(BINARY_ARGUMENT_TYPE::equals);
    if (consumesTypeIsBinary) {
        methodBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "Consumes")).addMember("value", "$T.APPLICATION_OCTET_STREAM", ClassName.get("javax.ws.rs.core", "MediaType")).build());
    }
    endpointDef.getDeprecated().ifPresent(deprecatedDocsValue -> methodBuilder.addAnnotation(ClassName.get("java.lang", "Deprecated")));
    methodBuilder.addAnnotations(ConjureAnnotations.getClientEndpointAnnotations(endpointDef));
    ServiceGenerators.getJavaDoc(endpointDef).ifPresent(content -> methodBuilder.addJavadoc("$L", content));
    return methodBuilder.build();
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition)

Example 2 with ArgumentDefinition

use of com.palantir.conjure.spec.ArgumentDefinition in project conjure-java by palantir.

the class Retrofit2ServiceGenerator method generateCompatibilityBackfillServiceMethods.

/**
 * Provides a linear expansion of optional query arguments to improve Java back-compat.
 */
private List<MethodSpec> generateCompatibilityBackfillServiceMethods(EndpointDefinition endpointDef, TypeMapper returnTypeMapper, TypeMapper argumentTypeMapper) {
    Set<ArgumentName> encodedPathArgs = extractEncodedPathArgs(endpointDef.getHttpPath());
    List<ArgumentDefinition> queryArgs = new ArrayList<>();
    for (ArgumentDefinition arg : endpointDef.getArgs()) {
        if (arg.getParamType().accept(ParameterTypeVisitor.IS_QUERY) && arg.getType().accept(DefaultableTypeVisitor.INSTANCE)) {
            queryArgs.add(arg);
        }
    }
    List<MethodSpec> alternateMethods = new ArrayList<>();
    for (int i = 0; i < queryArgs.size(); i++) {
        alternateMethods.add(createCompatibilityBackfillMethod(endpointDef, returnTypeMapper, argumentTypeMapper, encodedPathArgs, queryArgs.subList(i, queryArgs.size())));
    }
    return alternateMethods;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition) ArrayList(java.util.ArrayList) ArgumentName(com.palantir.conjure.spec.ArgumentName)

Example 3 with ArgumentDefinition

use of com.palantir.conjure.spec.ArgumentDefinition in project conjure by palantir.

the class ConjureParserUtils method parseArgs.

private static List<ArgumentDefinition> parseArgs(Map<ParameterName, com.palantir.conjure.parser.services.ArgumentDefinition> args, HttpPath httpPath, ReferenceTypeResolver typeResolver) {
    ImmutableList.Builder<ArgumentDefinition> resultBuilder = ImmutableList.builder();
    for (Map.Entry<ParameterName, com.palantir.conjure.parser.services.ArgumentDefinition> entry : args.entrySet()) {
        com.palantir.conjure.parser.services.ArgumentDefinition original = entry.getValue();
        ArgumentName argName = ArgumentName.of(entry.getKey().name());
        ParameterType paramType = parseParameterType(original, argName, httpPath);
        ArgumentDefinition.Builder builder = ArgumentDefinition.builder().argName(argName).type(original.type().visit(new ConjureTypeParserVisitor(typeResolver))).paramType(paramType).docs(original.docs().map(Documentation::of)).safety(original.safety().map(ConjureParserUtils::parseLogSafety)).markers(parseMarkers(original.markers(), typeResolver)).tags(original.tags().stream().peek(tag -> Preconditions.checkArgument(!tag.isEmpty(), "tag must not be empty")).collect(Collectors.toSet()));
        resultBuilder.add(builder.build());
    }
    return resultBuilder.build();
}
Also used : BodyParameterType(com.palantir.conjure.spec.BodyParameterType) ParameterType(com.palantir.conjure.spec.ParameterType) PathParameterType(com.palantir.conjure.spec.PathParameterType) QueryParameterType(com.palantir.conjure.spec.QueryParameterType) HeaderParameterType(com.palantir.conjure.spec.HeaderParameterType) ImmutableList(com.google.common.collect.ImmutableList) Documentation(com.palantir.conjure.spec.Documentation) ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition) ParameterName(com.palantir.conjure.parser.services.ParameterName) Map(java.util.Map) HashMap(java.util.HashMap) ArgumentName(com.palantir.conjure.spec.ArgumentName)

Example 4 with ArgumentDefinition

use of com.palantir.conjure.spec.ArgumentDefinition in project conjure by palantir.

the class EndpointDefinitionTest method testPathParamValidatorUniquePathParams.

@Test
public void testPathParamValidatorUniquePathParams() {
    ArgumentDefinition paramDefinition1 = ArgumentDefinition.builder().argName(ArgumentName.of("paramName")).type(Type.primitive(PrimitiveType.STRING)).paramType(ParameterType.path(PathParameterType.of())).build();
    ArgumentDefinition paramDefinition2 = ArgumentDefinition.builder().argName(ArgumentName.of("paramName")).type(Type.primitive(PrimitiveType.STRING)).paramType(ParameterType.path(PathParameterType.of())).build();
    EndpointDefinition.Builder definition = EndpointDefinition.builder().args(ImmutableList.of(paramDefinition1, paramDefinition2)).endpointName(ENDPOINT_NAME).httpMethod(HttpMethod.GET).httpPath(HttpPath.of("/a/path"));
    assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition.build(), emptyDealiasingVisitor)).isInstanceOf(IllegalStateException.class).hasMessage("Path parameter with identifier \"paramName\" is " + "defined multiple times for endpoint test{http: GET /a/path}");
}
Also used : ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) Test(org.junit.jupiter.api.Test)

Example 5 with ArgumentDefinition

use of com.palantir.conjure.spec.ArgumentDefinition in project conjure by palantir.

the class EndpointDefinitionTest method testPathParamValidatorExtraParams.

@Test
public void testPathParamValidatorExtraParams() {
    ArgumentDefinition paramDefinition = ArgumentDefinition.builder().type(Type.primitive(PrimitiveType.STRING)).argName(ArgumentName.of("paramName")).paramType(ParameterType.path(PathParameterType.of())).build();
    EndpointDefinition.Builder definition = EndpointDefinition.builder().args(paramDefinition).endpointName(ENDPOINT_NAME).httpMethod(HttpMethod.GET).httpPath(HttpPath.of("/a/path"));
    assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition.build(), emptyDealiasingVisitor)).isInstanceOf(IllegalStateException.class).hasMessageContaining("Path parameters defined in endpoint but not present in path template: [paramName]");
}
Also used : ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

ArgumentDefinition (com.palantir.conjure.spec.ArgumentDefinition)8 MethodSpec (com.squareup.javapoet.MethodSpec)5 EndpointDefinition (com.palantir.conjure.spec.EndpointDefinition)4 ArrayList (java.util.ArrayList)4 ImmutableList (com.google.common.collect.ImmutableList)3 ParameterType (com.palantir.conjure.spec.ParameterType)3 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)3 TypeName (com.squareup.javapoet.TypeName)3 Map (java.util.Map)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 ConjureAnnotations (com.palantir.conjure.java.ConjureAnnotations)2 Options (com.palantir.conjure.java.Options)2 TypeMapper (com.palantir.conjure.java.types.TypeMapper)2 Packages (com.palantir.conjure.java.util.Packages)2 ParameterOrder (com.palantir.conjure.java.util.ParameterOrder)2 TypeFunctions (com.palantir.conjure.java.util.TypeFunctions)2 ArgumentName (com.palantir.conjure.spec.ArgumentName)2 AuthType (com.palantir.conjure.spec.AuthType)2 ServiceDefinition (com.palantir.conjure.spec.ServiceDefinition)2 TypeDefinition (com.palantir.conjure.spec.TypeDefinition)2