Skip to content

Commit

Permalink
[fix] Alias 'valueOf' generation validates nested aliases recursively (
Browse files Browse the repository at this point in the history
…#352)

Certain nested aliases no longer result in Java which cannot compile.
  • Loading branch information
carterkozak authored and bulldozer-bot[bot] committed Apr 19, 2019
1 parent ecdf838 commit 671bb6e
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.palantir.product;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.palantir.conjure.java.lib.Bytes;
import java.util.Objects;
import javax.annotation.Generated;

@Generated("com.palantir.conjure.java.types.AliasGenerator")
public final class BinaryAliasOne {
private final Bytes value;

private BinaryAliasOne(Bytes value) {
Objects.requireNonNull(value, "value cannot be null");
this.value = value;
}

@JsonValue
public Bytes get() {
return value;
}

@Override
public String toString() {
return value.toString();
}

@Override
public boolean equals(Object other) {
return this == other
|| (other instanceof BinaryAliasOne
&& this.value.equals(((BinaryAliasOne) other).value));
}

@Override
public int hashCode() {
return value.hashCode();
}

@JsonCreator
public static BinaryAliasOne of(Bytes value) {
return new BinaryAliasOne(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.palantir.product;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.Objects;
import javax.annotation.Generated;

@Generated("com.palantir.conjure.java.types.AliasGenerator")
public final class BinaryAliasTwo {
private final BinaryAliasOne value;

private BinaryAliasTwo(BinaryAliasOne value) {
Objects.requireNonNull(value, "value cannot be null");
this.value = value;
}

@JsonValue
public BinaryAliasOne get() {
return value;
}

@Override
public String toString() {
return value.toString();
}

@Override
public boolean equals(Object other) {
return this == other
|| (other instanceof BinaryAliasTwo
&& this.value.equals(((BinaryAliasTwo) other).value));
}

@Override
public int hashCode() {
return value.hashCode();
}

@JsonCreator
public static BinaryAliasTwo of(BinaryAliasOne value) {
return new BinaryAliasTwo(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.palantir.product;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.Objects;
import javax.annotation.Generated;

@Generated("com.palantir.conjure.java.types.AliasGenerator")
public final class StringAliasOne {
private final String value;

private StringAliasOne(String value) {
Objects.requireNonNull(value, "value cannot be null");
this.value = value;
}

@JsonValue
public String get() {
return value;
}

@Override
public String toString() {
return value.toString();
}

@Override
public boolean equals(Object other) {
return this == other
|| (other instanceof StringAliasOne
&& this.value.equals(((StringAliasOne) other).value));
}

@Override
public int hashCode() {
return value.hashCode();
}

public static StringAliasOne valueOf(String value) {
return new StringAliasOne(value);
}

@JsonCreator
public static StringAliasOne of(String value) {
return new StringAliasOne(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.palantir.product;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.Objects;
import javax.annotation.Generated;

@Generated("com.palantir.conjure.java.types.AliasGenerator")
public final class StringAliasThree {
private final StringAliasTwo value;

private StringAliasThree(StringAliasTwo value) {
Objects.requireNonNull(value, "value cannot be null");
this.value = value;
}

@JsonValue
public StringAliasTwo get() {
return value;
}

@Override
public String toString() {
return value.toString();
}

@Override
public boolean equals(Object other) {
return this == other
|| (other instanceof StringAliasThree
&& this.value.equals(((StringAliasThree) other).value));
}

@Override
public int hashCode() {
return value.hashCode();
}

@JsonCreator
public static StringAliasThree of(StringAliasTwo value) {
return new StringAliasThree(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.palantir.product;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Generated;

@Generated("com.palantir.conjure.java.types.AliasGenerator")
public final class StringAliasTwo {
private final Optional<StringAliasOne> value;

private StringAliasTwo(Optional<StringAliasOne> value) {
Objects.requireNonNull(value, "value cannot be null");
this.value = value;
}

@JsonValue
public Optional<StringAliasOne> get() {
return value;
}

@Override
public String toString() {
return value.toString();
}

@Override
public boolean equals(Object other) {
return this == other
|| (other instanceof StringAliasTwo
&& this.value.equals(((StringAliasTwo) other).value));
}

@Override
public int hashCode() {
return value.hashCode();
}

@JsonCreator
public static StringAliasTwo of(Optional<StringAliasOne> value) {
return new StringAliasTwo(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,26 +158,24 @@ private static Optional<CodeBlock> valueOfFactoryMethod(
ClassName thisClass,
TypeName aliasTypeName,
TypeMapper typeMapper) {

// doesn't support valueOf factories for ANY and BINARY types
// doesn't support valueOf factories for ANY or BINARY types
if (conjureType.accept(TypeVisitor.IS_PRIMITIVE)
&& !conjureType.accept(TypeVisitor.IS_ANY)
&& !conjureType.accept(TypeVisitor.IS_BINARY)) {
return Optional.of(valueOfFactoryMethodForPrimitive(
conjureType.accept(TypeVisitor.PRIMITIVE), thisClass, aliasTypeName));
} else if (conjureType.accept(MoreVisitors.IS_INTERNAL_REFERENCE)) {
// delegate to aliased type's valueOf factory method
Optional<AliasDefinition> aliasTypeDef = typeMapper.getType(conjureType.accept(TypeVisitor.REFERENCE))
return typeMapper.getType(conjureType.accept(TypeVisitor.REFERENCE))
.filter(type -> type.accept(TypeDefinitionVisitor.IS_ALIAS))
.map(type -> type.accept(TypeDefinitionVisitor.ALIAS));

return aliasTypeDef.map(type -> {
ClassName className = ClassName.get(type.getTypeName().getPackage(), type.getTypeName().getName());
return CodeBlock.builder()
.addStatement("return new $T($T.valueOf(value))", thisClass, className).build();
});
.map(type -> type.accept(TypeDefinitionVisitor.ALIAS))
.flatMap(type -> valueOfFactoryMethod(type.getAlias(), thisClass, aliasTypeName, typeMapper)
.map(ignored -> {
ClassName className = ClassName.get(
type.getTypeName().getPackage(), type.getTypeName().getName());
return CodeBlock.builder()
.addStatement("return new $T($T.valueOf(value))", thisClass, className).build();
}));
}

return Optional.empty();
}

Expand Down
10 changes: 10 additions & 0 deletions conjure-java-core/src/test/resources/example-types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,13 @@ types:
SimpleEnum:
values:
- VALUE
StringAliasOne:
alias: string
StringAliasTwo:
alias: optional<StringAliasOne>
StringAliasThree:
alias: StringAliasTwo
BinaryAliasOne:
alias: binary
BinaryAliasTwo:
alias: BinaryAliasOne

0 comments on commit 671bb6e

Please sign in to comment.