Search in sources :

Example 1 with ParameterType

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

the class JerseyServiceGenerator method getParamTypeAnnotation.

private Optional<AnnotationSpec> getParamTypeAnnotation(ArgumentDefinition def) {
    AnnotationSpec.Builder annotationSpecBuilder;
    ParameterType paramType = def.getParamType();
    if (paramType.accept(ParameterTypeVisitor.IS_PATH)) {
        annotationSpecBuilder = AnnotationSpec.builder(ClassName.get("javax.ws.rs", "PathParam")).addMember("value", "$S", def.getArgName().get());
    } else if (paramType.accept(ParameterTypeVisitor.IS_QUERY)) {
        ParameterId paramId = paramType.accept(ParameterTypeVisitor.QUERY).getParamId();
        annotationSpecBuilder = AnnotationSpec.builder(ClassName.get("javax.ws.rs", "QueryParam")).addMember("value", "$S", paramId.get());
    } else if (paramType.accept(ParameterTypeVisitor.IS_HEADER)) {
        ParameterId paramId = paramType.accept(ParameterTypeVisitor.HEADER).getParamId();
        annotationSpecBuilder = AnnotationSpec.builder(ClassName.get("javax.ws.rs", "HeaderParam")).addMember("value", "$S", paramId.get());
    } else if (paramType.accept(ParameterTypeVisitor.IS_BODY)) {
        if (def.getType().accept(TypeVisitor.IS_OPTIONAL) || !options.requireNotNullAuthAndBodyParams()) {
            return Optional.empty();
        }
        annotationSpecBuilder = AnnotationSpec.builder(NOT_NULL);
    } else {
        throw new IllegalStateException("Unrecognized argument type: " + def.getParamType());
    }
    return Optional.of(annotationSpecBuilder.build());
}
Also used : ParameterType(com.palantir.conjure.spec.ParameterType) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ParameterId(com.palantir.conjure.spec.ParameterId)

Example 2 with ParameterType

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

the class Retrofit2ServiceGenerator method createEndpointParameter.

private ParameterSpec createEndpointParameter(TypeMapper argumentTypeMapper, Set<ArgumentName> encodedPathArgs, ArgumentDefinition def) {
    ParameterSpec.Builder param = ParameterSpec.builder(argumentTypeMapper.getClassName(def.getType()), def.getArgName().get());
    ParameterType paramType = def.getParamType();
    if (paramType.accept(ParameterTypeVisitor.IS_PATH)) {
        AnnotationSpec.Builder builder = AnnotationSpec.builder(ClassName.get("retrofit2.http", "Path")).addMember("value", "$S", def.getArgName().get());
        if (encodedPathArgs.contains(def.getArgName())) {
            builder.addMember("encoded", "$L", true);
        }
        param.addAnnotation(builder.build());
    } else if (paramType.accept(ParameterTypeVisitor.IS_QUERY)) {
        ParameterId paramId = paramType.accept(ParameterTypeVisitor.QUERY).getParamId();
        param.addAnnotation(AnnotationSpec.builder(ClassName.get("retrofit2.http", "Query")).addMember("value", "$S", paramId.get()).build());
    } else if (paramType.accept(ParameterTypeVisitor.IS_HEADER)) {
        ParameterId paramId = paramType.accept(ParameterTypeVisitor.HEADER).getParamId();
        param.addAnnotation(AnnotationSpec.builder(ClassName.get("retrofit2.http", "Header")).addMember("value", "$S", paramId.get()).build());
    } else if (paramType.accept(ParameterTypeVisitor.IS_BODY)) {
        param.addAnnotation(ClassName.get("retrofit2.http", "Body"));
    } else {
        throw new IllegalStateException("Unrecognized argument type: " + def.getParamType());
    }
    return param.build();
}
Also used : ParameterType(com.palantir.conjure.spec.ParameterType) ParameterSpec(com.squareup.javapoet.ParameterSpec) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ParameterId(com.palantir.conjure.spec.ParameterId)

Example 3 with ParameterType

use of com.palantir.conjure.spec.ParameterType 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 ParameterType

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

the class ParamIdValidatorTest method testInvalidNonHeader.

@Test
public void testInvalidNonHeader() {
    for (String paramId : ImmutableList.of("AB", "123", "foo.bar")) {
        ParameterType parameterType = ParameterType.query(QueryParameterType.of(ParameterId.of(paramId)));
        assertThatThrownByEndpointValidation(parameterType).isInstanceOf(IllegalStateException.class).hasMessage("Query param id %s on endpoint test{http: POST /a/path} must match one of the " + "following patterns: [LOWER_CAMEL_CASE[%s], KEBAB_CASE[%s], SNAKE_CASE[%s]]", paramId, CaseConverter.CAMEL_CASE_PATTERN.pattern(), CaseConverter.KEBAB_CASE_PATTERN.pattern(), CaseConverter.SNAKE_CASE_PATTERN.pattern());
    }
}
Also used : ParameterType(com.palantir.conjure.spec.ParameterType) QueryParameterType(com.palantir.conjure.spec.QueryParameterType) HeaderParameterType(com.palantir.conjure.spec.HeaderParameterType) Test(org.junit.jupiter.api.Test)

Example 5 with ParameterType

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

the class ParamIdValidatorTest method testProtocolHeaders.

@Test
public void testProtocolHeaders() {
    for (String protocolHeader : EndpointDefinitionValidator.PROTOCOL_HEADERS) {
        ParameterType parameterType = ParameterType.header(HeaderParameterType.of(ParameterId.of(protocolHeader)));
        assertThatThrownByEndpointValidation(parameterType).isInstanceOf(IllegalStateException.class).hasMessage("Header parameter id %s on endpoint test{http: POST /a/path} should not be one of the " + "protocol headers %s", protocolHeader, EndpointDefinitionValidator.PROTOCOL_HEADERS);
    }
}
Also used : ParameterType(com.palantir.conjure.spec.ParameterType) QueryParameterType(com.palantir.conjure.spec.QueryParameterType) HeaderParameterType(com.palantir.conjure.spec.HeaderParameterType) Test(org.junit.jupiter.api.Test)

Aggregations

ParameterType (com.palantir.conjure.spec.ParameterType)6 HeaderParameterType (com.palantir.conjure.spec.HeaderParameterType)4 QueryParameterType (com.palantir.conjure.spec.QueryParameterType)4 Test (org.junit.jupiter.api.Test)3 ParameterId (com.palantir.conjure.spec.ParameterId)2 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)2 ImmutableList (com.google.common.collect.ImmutableList)1 ParameterName (com.palantir.conjure.parser.services.ParameterName)1 ArgumentDefinition (com.palantir.conjure.spec.ArgumentDefinition)1 ArgumentName (com.palantir.conjure.spec.ArgumentName)1 BodyParameterType (com.palantir.conjure.spec.BodyParameterType)1 Documentation (com.palantir.conjure.spec.Documentation)1 PathParameterType (com.palantir.conjure.spec.PathParameterType)1 ParameterSpec (com.squareup.javapoet.ParameterSpec)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1