use of com.palantir.conjure.spec.FieldDefinition in project conjure-java by palantir.
the class UnionGenerator method generateBase.
private static TypeSpec generateBase(ClassName baseClass, ClassName visitorClass, Map<FieldDefinition, TypeName> memberTypes) {
ClassName unknownWrapperClass = baseClass.peerClass(UNKNOWN_WRAPPER_CLASS_NAME);
TypeSpec.Builder baseBuilder = TypeSpec.interfaceBuilder(baseClass).addModifiers(Modifier.PRIVATE).addAnnotation(AnnotationSpec.builder(JsonTypeInfo.class).addMember("use", "JsonTypeInfo.Id.NAME").addMember("include", "JsonTypeInfo.As.EXISTING_PROPERTY").addMember("property", "\"type\"").addMember("visible", "$L", true).addMember("defaultImpl", "$T.class", unknownWrapperClass).build());
if (!memberTypes.isEmpty()) {
List<AnnotationSpec> subAnnotations = memberTypes.entrySet().stream().map(entry -> AnnotationSpec.builder(JsonSubTypes.Type.class).addMember("value", "$T.class", peerWrapperClass(baseClass, sanitizeUnknown(entry.getKey().getFieldName()))).build()).collect(Collectors.toList());
AnnotationSpec.Builder annotationBuilder = AnnotationSpec.builder(JsonSubTypes.class);
subAnnotations.forEach(subAnnotation -> annotationBuilder.addMember("value", "$L", subAnnotation));
baseBuilder.addAnnotation(annotationBuilder.build());
}
baseBuilder.addAnnotation(AnnotationSpec.builder(JsonIgnoreProperties.class).addMember("ignoreUnknown", "$L", true).build());
ParameterizedTypeName parameterizedVisitorClass = ParameterizedTypeName.get(visitorClass, TYPE_VARIABLE);
ParameterSpec visitor = ParameterSpec.builder(parameterizedVisitorClass, "visitor").build();
baseBuilder.addMethod(MethodSpec.methodBuilder("accept").addTypeVariable(TYPE_VARIABLE).addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).addParameter(visitor).returns(TYPE_VARIABLE).build());
return baseBuilder.build();
}
use of com.palantir.conjure.spec.FieldDefinition in project conjure-java by palantir.
the class UnionGenerator method generateUnionType.
public static JavaFile generateUnionType(TypeMapper typeMapper, Map<com.palantir.conjure.spec.TypeName, TypeDefinition> typesMap, UnionDefinition typeDef, Options options) {
com.palantir.conjure.spec.TypeName prefixedTypeName = Packages.getPrefixedName(typeDef.getTypeName(), options.packagePrefix());
ClassName unionClass = ClassName.get(prefixedTypeName.getPackage(), prefixedTypeName.getName());
ClassName baseClass = unionClass.nestedClass("Base");
ClassName visitorClass = unionClass.nestedClass("Visitor");
ClassName visitorBuilderClass = unionClass.nestedClass("VisitorBuilder");
Map<FieldDefinition, TypeName> memberTypes = typeDef.getUnion().stream().collect(StableCollectors.toLinkedMap(Function.identity(), entry -> typeMapper.getClassName(entry.getType())));
List<FieldSpec> fields = ImmutableList.of(FieldSpec.builder(baseClass, VALUE_FIELD_NAME, Modifier.PRIVATE, Modifier.FINAL).build());
TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(typeDef.getTypeName().getName()).addAnnotation(ConjureAnnotations.getConjureGeneratedAnnotation(UnionGenerator.class)).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addFields(fields).addMethod(generateConstructor(baseClass)).addMethod(generateGetValue(baseClass)).addMethods(generateStaticFactories(typeMapper, unionClass, typeDef.getUnion())).addMethod(generateAcceptVisitMethod(visitorClass)).addType(generateVisitor(unionClass, visitorClass, memberTypes, visitorBuilderClass, options)).addType(generateVisitorBuilder(unionClass, visitorClass, visitorBuilderClass, memberTypes, options)).addTypes(generateVisitorBuilderStageInterfaces(unionClass, visitorClass, memberTypes, options)).addType(generateBase(baseClass, visitorClass, memberTypes)).addTypes(generateWrapperClasses(typeMapper, typesMap, baseClass, visitorClass, typeDef.getUnion(), options)).addType(generateUnknownWrapper(baseClass, visitorClass, options)).addMethod(generateEquals(unionClass)).addMethod(MethodSpecs.createEqualTo(unionClass, fields)).addMethod(MethodSpecs.createHashCode(fields)).addMethod(MethodSpecs.createToString(unionClass.simpleName(), fields.stream().map(fieldSpec -> FieldName.of(fieldSpec.name)).collect(Collectors.toList())));
typeDef.getDocs().ifPresent(docs -> typeBuilder.addJavadoc("$L", Javadoc.render(docs)));
return JavaFile.builder(prefixedTypeName.getPackage(), typeBuilder.build()).skipJavaLangImports(true).indent(" ").build();
}
use of com.palantir.conjure.spec.FieldDefinition in project conjure by palantir.
the class ObjectDefinitionValidatorTest method testUniqueFieldNameValidator.
private void testUniqueFieldNameValidator(String fieldName1, String fieldName2) {
FieldDefinition field1 = FieldDefinition.builder().fieldName(FieldName.of(fieldName1)).type(Type.primitive(PrimitiveType.STRING)).build();
FieldDefinition field2 = FieldDefinition.builder().fieldName(FieldName.of(fieldName2)).type(Type.primitive(PrimitiveType.STRING)).build();
TypeName name = TypeName.of("Foo", "package");
ObjectDefinition definition = ObjectDefinition.builder().typeName(name).fields(field1).fields(field2).build();
assertThatThrownBy(() -> ObjectDefinitionValidator.validate(definition)).isInstanceOf(IllegalArgumentException.class).hasMessage(String.format("ObjectDefinition must not contain duplicate field names " + "(modulo case normalization): %s vs %s", fieldName2, fieldName1));
}
use of com.palantir.conjure.spec.FieldDefinition in project conjure by palantir.
the class FieldDefinitionValidatorTest method assertInvalidType.
private void assertInvalidType(Type type) {
String illegalFieldName = "asdf";
FieldDefinition fieldDefinition = FieldDefinition.builder().fieldName(FieldName.of(illegalFieldName)).type(type).docs(Documentation.of("docs")).build();
assertThatThrownBy(() -> FieldDefinitionValidator.validate(fieldDefinition)).isInstanceOf(IllegalStateException.class).hasMessageContaining(illegalFieldName).hasMessageContaining(COMPLEX_KEY_TYPE.toString());
}
use of com.palantir.conjure.spec.FieldDefinition in project conjure-postman by palantir.
the class TemplateTypeVisitor method visitReference.
@SuppressWarnings("PreferSafeLoggingPreconditions")
@Override
public JsonNode visitReference(TypeName value) {
TypeDefinition definition = types.get(value);
TemplateTypeVisitor visitor = this;
return definition.accept(new TypeDefinition.Visitor<JsonNode>() {
@Override
public JsonNode visitAlias(AliasDefinition value) {
JsonNode wrapped = value.getAlias().accept(visitor);
if (wrapped instanceof TextNode) {
return new TextNode(String.format("{{ %s(%s) }}", value.getTypeName().getName(), wrapped.toString().replaceAll("[\"{}]", "")));
}
return wrapped;
}
@Override
public JsonNode visitEnum(EnumDefinition value) {
return new TextNode(value.getValues().stream().map(EnumValueDefinition::getValue).collect(Collectors.joining("|")));
}
@Override
public JsonNode visitObject(ObjectDefinition value) {
if (seenTypeStack.contains(value.getTypeName())) {
return new TextNode(String.format("{{%s}}", value.getTypeName().getName()));
}
seenTypeStack.push(value.getTypeName());
ObjectNode node = objectMapper.createObjectNode();
value.getFields().forEach(fieldDefinition -> node.set(fieldDefinition.getFieldName().get(), fieldDefinition.getType().accept(visitor)));
Preconditions.checkState(seenTypeStack.pop().equals(value.getTypeName()));
return node;
}
@Override
public JsonNode visitUnion(UnionDefinition value) {
if (value.getUnion().isEmpty()) {
return null;
} else {
if (seenTypeStack.contains(value.getTypeName())) {
return new TextNode(String.format("{{%s}}", value.getTypeName().getName()));
}
seenTypeStack.push(value.getTypeName());
String unionTypes = value.getUnion().stream().map(FieldDefinition::getFieldName).map(FieldName::get).collect(Collectors.joining("|"));
ObjectNode templates = objectMapper.createObjectNode();
value.getUnion().forEach(field -> templates.set(field.getFieldName().get(), field.getType().accept(visitor)));
JsonNode union = objectMapper.createObjectNode().put("type", unionTypes).set("oneOf", templates);
Preconditions.checkState(seenTypeStack.pop().equals(value.getTypeName()));
return union;
}
}
@Override
public JsonNode visitUnknown(String _unknownType) {
return new TextNode("{{UNKNOWN}}");
}
});
}
Aggregations