@Override public Boolean visitPrimitive(PrimitiveType value) { return value.get() == PrimitiveType.Value.BINARY; } }
/** * Inlines outer-level aliases and references, but not within objects or container types. * <p> * For example, a reference to an alias A which wraps another alias B which wraps a {@code list<integer>}, we'll * return {@code list<integer>}. Note that these are outer-level references being resolved. * However, if the aforementioned list's inner type was also a reference e.g. {@code list<C>}, we * wouldn't unwrap that, so we'd just return the same {@code list<C>}. */ public Either<TypeDefinition, Type> dealias(Type type) { return type.accept(this); }
@Override public TypeName visitExternal(ExternalReference value) { return value.getExternalReference(); } }
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")); } }
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 public void testUnionMemberKeyMustNotBeEmpty() { FieldDefinition fieldDefinition = FieldDefinition.builder() .fieldName(FieldName.of("")) .type(Type.primitive(PrimitiveType.STRING)) .build(); assertThatThrownBy(() -> UnionDefinitionValidator.validateAll(UnionDefinition.builder() .union(fieldDefinition) .typeName(TypeName.of("string", "")) .build())) .isInstanceOf(IllegalArgumentException.class) .hasMessageStartingWith("Union member key must not be empty"); }
@Test public void testSingleBodyParamValidator() { EndpointDefinition.Builder definition = EndpointDefinition.builder() .args(BODY_ARG_BUILDER.argName(ArgumentName.of("bodyArg1")).build()) .args(BODY_ARG_BUILDER.argName(ArgumentName.of("bodyArg2")).build()) .endpointName(ENDPOINT_NAME) .httpMethod(HttpMethod.GET) .httpPath(HttpPath.of("/a/path")); assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition.build(), emptyDealiasingVisitor)) .isInstanceOf(IllegalStateException.class) .hasMessage("Endpoint cannot have multiple body parameters: [bodyArg1, bodyArg2]"); }
private static Optional<TypeName> resolveReferenceType(Type type) { if (type.accept(TypeVisitor.IS_REFERENCE)) { return Optional.of(type.accept(TypeVisitor.REFERENCE)); } else if (type.accept(TypeVisitor.IS_PRIMITIVE)) { return Optional.of( TypeName.of(type.accept(TypeVisitor.PRIMITIVE).get().name(), "")); } return Optional.empty(); }
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)); } } } }
@Override public Type visitAny(AnyType type) { return Type.primitive(com.palantir.conjure.spec.PrimitiveType.ANY); }
@Override public TypeName visitUnion(UnionDefinition value) { return value.getTypeName(); }
@Override public TypeName visitEnum(EnumDefinition value) { return value.getTypeName(); }
@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); }
@Override public Boolean visitPrimitive(PrimitiveType value) { return value.get() == PrimitiveType.Value.BINARY; } }
@Override public Type visitDateTime(DateTimeType type) { return Type.primitive(com.palantir.conjure.spec.PrimitiveType.DATETIME); } }
/** * Inlines outer-level aliases and references, but not within objects or container types. * <p> * For example, a reference to an alias A which wraps another alias B which wraps a {@code list<integer>}, we'll * return {@code list<integer>}. Note that these are outer-level references being resolved. * However, if the aforementioned list's inner type was also a reference e.g. {@code list<C>}, we * wouldn't unwrap that, so we'd just return the same {@code list<C>}. */ public Either<TypeDefinition, Type> dealias(Type type) { return type.accept(this); }
@Override public TypeName visitExternal(ExternalReference value) { return value.getExternalReference(); } }
@Override public Boolean visitPrimitive(PrimitiveType value) { return value.get() == PrimitiveType.Value.ANY; } }
@Override public Boolean visitPrimitive(PrimitiveType value) { return value.get() == PrimitiveType.Value.ANY; } }