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<com.palantir.conjure.parser.services.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)) .markers(parseMarkers(original.markers(), typeResolver)); resultBuilder.add(builder.build()); } return resultBuilder.build(); }
@Test public void testArgumentTypeValidator() { EndpointDefinition.Builder definition = EndpointDefinition.builder() .args(ImmutableList.of(ArgumentDefinition.builder() .argName(ArgumentName.of("testArg")) .type(Type.primitive(PrimitiveType.BINARY)) .paramType(ParameterType.header(HeaderParameterType.of(ParameterId.of("testArg")))) .build())) .endpointName(ENDPOINT_NAME) .httpMethod(HttpMethod.GET) .httpPath(HttpPath.of("/a/path")); assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition.build(), emptyDealiasingVisitor)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Non body parameters cannot be of the 'binary' type: 'testArg' is not allowed"); }
@Test public void testComplexHeaderObject() { TypeName typeName = TypeName.of("SomeObject", "com.palantir.foo"); EndpointDefinition.Builder definition = EndpointDefinition.builder() .args(ArgumentDefinition.builder() .argName(ArgumentName.of("someName")) .type(Type.reference(typeName)) .paramType(ParameterType.header(HeaderParameterType.of(ParameterId.of("SomeId")))) .build()) .endpointName(ENDPOINT_NAME) .httpMethod(HttpMethod.GET) .httpPath(HttpPath.of("/a/path")); DealiasingTypeVisitor dealiasingVisitor = new DealiasingTypeVisitor(ImmutableMap.of( typeName, TypeDefinition.object(ObjectDefinition.of(typeName, ImmutableList.of(), Documentation.of(""))) )); assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition.build(), dealiasingVisitor)) .isInstanceOf(IllegalStateException.class) .hasMessage("Header parameters must be enums, primitives, aliases or optional primitive:" + " \"someName\" is not allowed"); } }
@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 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]"); }
@Test public void testComplexHeader() { EndpointDefinition.Builder definition = EndpointDefinition.builder() .args(ArgumentDefinition.builder() .argName(ArgumentName.of("someName")) .type(Type.list(ListType.builder().itemType(Type.primitive(PrimitiveType.STRING)).build())) .paramType(ParameterType.header(HeaderParameterType.of(ParameterId.of("someId")))) .build()) .endpointName(ENDPOINT_NAME) .httpMethod(HttpMethod.GET) .httpPath(HttpPath.of("/a/path")); assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition.build(), emptyDealiasingVisitor)) .isInstanceOf(IllegalStateException.class) .hasMessage("Header parameters must be enums, primitives, aliases or optional primitive:" + " \"someName\" is not allowed"); }
private EndpointDefinition createEndpoint(ParameterType paramType) { ArgumentDefinition arg = ArgumentDefinition.builder() .argName(PARAMETER_NAME) .paramType(paramType) .type(Type.primitive(PrimitiveType.INTEGER)) .build(); EndpointDefinition definition = EndpointDefinition.builder() .httpMethod(HttpMethod.POST) .httpPath(HttpPath.of("/a/path")) .args(arg) .endpointName(EndpointName.of("test")) .build(); EndpointDefinitionValidator.validateAll(definition, dealiasingVisitor); return definition; } }
@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); }
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")); } }