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