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());
}
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();
}
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();
}
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());
}
}
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);
}
}
Aggregations