@Override public Type visitMap(MapType type) { return Type.map(com.palantir.conjure.spec.MapType.of( type.keyType().visit(this), type.valueType().visit(this))); }
private static void checkForComplexType(FieldDefinition typeDef) { if (typeDef.getType().accept(TypeVisitor.IS_MAP)) { MapType mapType = typeDef.getType().accept(TypeVisitor.MAP); if (!mapType.getKeyType().accept(TypeVisitor.IS_PRIMITIVE_OR_REFERENCE)) { throw new IllegalStateException( String.format("Complex type '%s' not allowed in map key: %s.", mapType.getKeyType(), typeDef)); } } } }
@Test public void testNoComplexKeysInMaps() { String illegalFieldName = "asdf"; Type complexKeyType = Type.list(ListType.of(Type.primitive(PrimitiveType.STRING))); FieldDefinition fieldDefinition = FieldDefinition.of( FieldName.of(illegalFieldName), Type.map(MapType.of(complexKeyType, Type.primitive(PrimitiveType.STRING))), Documentation.of("docs")); assertThatThrownBy(() -> FieldDefinitionValidator.validate(fieldDefinition)) .isInstanceOf(IllegalStateException.class) .hasMessageContaining(illegalFieldName) .hasMessageContaining(complexKeyType.toString()); } }
@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); }