private FieldDefinition field(FieldName name, String type) { return FieldDefinition.of(name, Type.reference(TypeName.of(type, PACKAGE)), DOCS); } }
private static Type resolveFromTypeName( com.palantir.conjure.parser.types.names.TypeName name, TypesDefinition types) { Optional<String> defaultPackage = types.definitions().defaultConjurePackage().map(ConjureParserUtils::parseConjurePackage); BaseObjectTypeDefinition maybeDirectDef = types.definitions().objects().get(name); String conjurePackage; String typeName; if (maybeDirectDef == null) { ExternalTypeDefinition maybeExternalDef = types.imports().get(name); if (maybeExternalDef == null) { throw new IllegalStateException("Unknown LocalReferenceType: " + name); } String externalPath = maybeExternalDef.external().java(); int lastIndex = externalPath.lastIndexOf("."); conjurePackage = externalPath.substring(0, lastIndex); typeName = externalPath.substring(lastIndex + 1); return Type.external(ExternalReference.builder() .externalReference(TypeName.of(typeName, conjurePackage)) .fallback(ConjureParserUtils.parsePrimitiveType(maybeExternalDef.baseType())) .build()); } else { // Conjure-defined object conjurePackage = ConjureParserUtils.parsePackageOrElseThrow( maybeDirectDef.conjurePackage(), defaultPackage); return Type.reference(TypeName.of(name.name(), conjurePackage)); } } }
@Test public void testNoSelfRecursiveType() { ConjureDefinition conjureDef = ConjureDefinition.builder() .version(1) .types(ImmutableList.of(TypeDefinition.object( ObjectDefinition.builder() .typeName(FOO) .fields(FieldDefinition.of(FieldName.of("self"), Type.reference(FOO), DOCS)) .build()))) .build(); assertThatThrownBy(() -> ConjureDefinitionValidator.NO_RECURSIVE_TYPES.validate(conjureDef)) .isInstanceOf(IllegalStateException.class) .hasMessage("Illegal recursive data type: Foo -> Foo"); }
@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 testRecursiveTypeOkInReference() { Type referenceType = Type.reference(FOO); TypeDefinition objectDefinition = TypeDefinition.object( ObjectDefinition.builder() .typeName(TypeName.of("Foo", "bar")) .addAllFields(ImmutableList.of( FieldDefinition.of(FieldName.of("selfOptional"), Type.optional(OptionalType.of(Type.reference(FOO))), DOCS), FieldDefinition.of(FieldName.of("selfMap"), Type.map(MapType.of(referenceType, referenceType)), DOCS), FieldDefinition.of(FieldName.of("selfSet"), Type.set(SetType.of(referenceType)), DOCS), FieldDefinition.of(FieldName.of("selfList"), Type.list(ListType.of(referenceType)), DOCS))) .build()); ConjureDefinition conjureDef = ConjureDefinition.builder() .version(1) .types(ImmutableList.of(objectDefinition)) .build(); ConjureDefinitionValidator.NO_RECURSIVE_TYPES.validate(conjureDef); }