private static ParameterType parseParameterType( com.palantir.conjure.parser.services.ArgumentDefinition argumentDef, ArgumentName argName, HttpPath httpPath) { Set<ArgumentName> args = HttpPathValidator.pathArgs(httpPath.get()); switch (argumentDef.paramType()) { case AUTO: // AUTO type if (args.contains(argName)) { // argument exists in request line -- it is a path arg return ParameterType.path(PathParameterType.of()); } else { // argument does not exist in request line -- it is a body arg return ParameterType.body(BodyParameterType.of()); } case HEADER: String headerParamId = argumentDef.paramId().map(id -> id.name()).orElse(argName.get()); return ParameterType.header(HeaderParameterType.of(ParameterId.of(headerParamId))); case PATH: return ParameterType.path(PathParameterType.of()); case BODY: return ParameterType.body(BodyParameterType.of()); case QUERY: String queryParamId = argumentDef.paramId().map(id -> id.name()).orElse(argName.get()); return ParameterType.query(QueryParameterType.of(ParameterId.of(queryParamId))); default: throw new IllegalArgumentException("Unknown parameter type: " + argumentDef.paramType()); } }
private EndpointDefinition.Builder createEndpoint(String paramName) { ArgumentDefinition arg = ArgumentDefinition.builder() .paramType(ParameterType.body(BodyParameterType.of())) .type(Type.primitive(PrimitiveType.STRING)) .argName(ArgumentName.of(paramName)) .build(); return EndpointDefinition.builder() .httpMethod(HttpMethod.POST) .httpPath(HttpPath.of("/a/path")) .args(ImmutableList.of(arg)) .endpointName(EndpointName.of("test")); } }
@Test @SuppressWarnings("CheckReturnValue") public void testArgumentBodyTypeValidator() { EndpointDefinition.Builder definition = EndpointDefinition.builder() .args(ImmutableList.of(ArgumentDefinition.builder() .argName(ArgumentName.of("testArg")) .type(Type.primitive(PrimitiveType.BINARY)) .paramType(ParameterType.body(BodyParameterType.of())) .build())) .endpointName(ENDPOINT_NAME) .httpMethod(HttpMethod.POST) .httpPath(HttpPath.of("/a/path")); // Should not throw exception EndpointDefinitionValidator.validateAll(definition.build(), emptyDealiasingVisitor); }