Search in sources :

Example 6 with TypeName

use of com.palantir.conjure.spec.TypeName in project conjure by palantir.

the class EndpointDefinitionTest method testNoOptionalBinaryBodyParamValidator_throughAlias.

@Test
public void testNoOptionalBinaryBodyParamValidator_throughAlias() {
    TypeName typeName = TypeName.of("OptionalBinary", "foo");
    EndpointDefinition definition = EndpointDefinition.builder().args(bodyArgBuilder().argName(ArgumentName.of("someName")).type(Type.reference(typeName)).build()).endpointName(ENDPOINT_NAME).httpMethod(HttpMethod.POST).httpPath(HttpPath.of("/a/path")).build();
    DealiasingTypeVisitor dealiasingVisitor = new DealiasingTypeVisitor(ImmutableMap.of(typeName, TypeDefinition.alias(AliasDefinition.builder().typeName(typeName).alias(Type.optional(OptionalType.of(Type.primitive(PrimitiveType.BINARY)))).docs(Documentation.of("")).build())));
    assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition, dealiasingVisitor)).isInstanceOf(IllegalStateException.class).hasMessage("Endpoint BODY argument must not be optional<binary> or alias thereof: " + "test{http: POST /a/path}");
}
Also used : TypeName(com.palantir.conjure.spec.TypeName) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) DealiasingTypeVisitor(com.palantir.conjure.visitor.DealiasingTypeVisitor) Test(org.junit.jupiter.api.Test)

Example 7 with TypeName

use of com.palantir.conjure.spec.TypeName in project conjure by palantir.

the class EndpointDefinitionTest method testComplexHeaderObject.

@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 on endpoint test{http: GET /a/path}");
}
Also used : TypeName(com.palantir.conjure.spec.TypeName) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) DealiasingTypeVisitor(com.palantir.conjure.visitor.DealiasingTypeVisitor) Test(org.junit.jupiter.api.Test)

Example 8 with TypeName

use of com.palantir.conjure.spec.TypeName 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}}");
        }
    });
}
Also used : ListType(com.palantir.conjure.spec.ListType) ExternalReference(com.palantir.conjure.spec.ExternalReference) Type(com.palantir.conjure.spec.Type) TypeDefinitionVisitor(com.palantir.conjure.visitor.TypeDefinitionVisitor) Function(java.util.function.Function) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) UnionDefinition(com.palantir.conjure.spec.UnionDefinition) MapType(com.palantir.conjure.spec.MapType) Map(java.util.Map) OptionalType(com.palantir.conjure.spec.OptionalType) JsonNode(com.fasterxml.jackson.databind.JsonNode) SetType(com.palantir.conjure.spec.SetType) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) ObjectDefinition(com.palantir.conjure.spec.ObjectDefinition) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) EnumDefinition(com.palantir.conjure.spec.EnumDefinition) EnumValueDefinition(com.palantir.conjure.spec.EnumValueDefinition) FieldDefinition(com.palantir.conjure.spec.FieldDefinition) Collectors(java.util.stream.Collectors) TextNode(com.fasterxml.jackson.databind.node.TextNode) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) List(java.util.List) AliasDefinition(com.palantir.conjure.spec.AliasDefinition) TypeName(com.palantir.conjure.spec.TypeName) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) Preconditions(com.google.common.base.Preconditions) FieldName(com.palantir.conjure.spec.FieldName) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) ArrayDeque(java.util.ArrayDeque) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AliasDefinition(com.palantir.conjure.spec.AliasDefinition) UnionDefinition(com.palantir.conjure.spec.UnionDefinition) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) ObjectDefinition(com.palantir.conjure.spec.ObjectDefinition) EnumDefinition(com.palantir.conjure.spec.EnumDefinition) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) FieldName(com.palantir.conjure.spec.FieldName)

Example 9 with TypeName

use of com.palantir.conjure.spec.TypeName in project conjure by palantir.

the class ConjureParserUtils method parseErrors.

static List<ErrorDefinition> parseErrors(NamedTypesDefinition defs, ConjureTypeParserVisitor.ReferenceTypeResolver typeResolver) {
    Optional<String> defaultPackage = defs.defaultConjurePackage().map(ConjurePackage::name);
    ImmutableList.Builder<ErrorDefinition> errorsBuidler = ImmutableList.builder();
    errorsBuidler.addAll(defs.errors().entrySet().stream().map(entry -> {
        TypeName typeName = TypeName.of(entry.getKey().name(), parsePackageOrElseThrow(entry.getValue().conjurePackage(), defaultPackage));
        return parseErrorType(typeName, entry.getValue(), typeResolver);
    }).collect(Collectors.toList()));
    return errorsBuidler.build();
}
Also used : ErrorDefinition(com.palantir.conjure.spec.ErrorDefinition) TypeName(com.palantir.conjure.spec.TypeName) ImmutableList(com.google.common.collect.ImmutableList) ConjurePackage(com.palantir.conjure.parser.types.names.ConjurePackage) PathString(com.palantir.conjure.parser.services.PathString)

Example 10 with TypeName

use of com.palantir.conjure.spec.TypeName in project conjure by palantir.

the class ConjureParserUtils method parseConjureDef.

static ConjureDefinition parseConjureDef(Map<String, AnnotatedConjureSourceFile> annotatedParsedDefs, SafetyDeclarationRequirements safetyDeclarations) {
    ImmutableList.Builder<ServiceDefinition> servicesBuilder = ImmutableList.builder();
    ImmutableList.Builder<ErrorDefinition> errorsBuilder = ImmutableList.builder();
    ImmutableList.Builder<TypeDefinition> typesBuilder = ImmutableList.builder();
    annotatedParsedDefs.values().forEach(annotatedParsed -> {
        ConjureSourceFile parsed = annotatedParsed.conjureSourceFile();
        try {
            ConjureTypeParserVisitor.ReferenceTypeResolver typeResolver = new ConjureTypeParserVisitor.ByParsedRepresentationTypeNameResolver(parsed.types(), annotatedParsed.importProviders(), annotatedParsedDefs);
            // Resolve objects first, so we can use them in service validations
            Map<TypeName, TypeDefinition> objects = parseObjects(parsed.types(), typeResolver);
            Map<TypeName, TypeDefinition> importedObjects = parseImportObjects(parsed.types().conjureImports(), annotatedParsedDefs);
            Map<TypeName, TypeDefinition> allObjects = new HashMap<>();
            allObjects.putAll(objects);
            allObjects.putAll(importedObjects);
            DealiasingTypeVisitor dealiasingVisitor = new DealiasingTypeVisitor(allObjects);
            parsed.services().forEach((serviceName, service) -> {
                servicesBuilder.add(parseService(service, TypeName.of(serviceName.name(), parseConjurePackage(service.conjurePackage())), typeResolver, dealiasingVisitor));
            });
            typesBuilder.addAll(objects.values());
            errorsBuilder.addAll(parseErrors(parsed.types().definitions(), typeResolver));
        } catch (RuntimeException e) {
            throw new ConjureRuntimeException(String.format("Encountered error trying to parse file '%s'", annotatedParsed.sourceFile()), e);
        }
    });
    ConjureDefinition definition = ConjureDefinition.builder().version(Conjure.SUPPORTED_IR_VERSION).types(typesBuilder.build()).errors(errorsBuilder.build()).services(servicesBuilder.build()).build();
    ConjureDefinitionValidator.validateAll(definition, safetyDeclarations);
    return definition;
}
Also used : ErrorDefinition(com.palantir.conjure.spec.ErrorDefinition) AnnotatedConjureSourceFile(com.palantir.conjure.parser.AnnotatedConjureSourceFile) ConjureSourceFile(com.palantir.conjure.parser.ConjureSourceFile) TypeName(com.palantir.conjure.spec.TypeName) ReferenceTypeResolver(com.palantir.conjure.defs.ConjureTypeParserVisitor.ReferenceTypeResolver) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) ConjureRuntimeException(com.palantir.conjure.exceptions.ConjureRuntimeException) DealiasingTypeVisitor(com.palantir.conjure.visitor.DealiasingTypeVisitor) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) ConjureRuntimeException(com.palantir.conjure.exceptions.ConjureRuntimeException) ConjureDefinition(com.palantir.conjure.spec.ConjureDefinition) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition)

Aggregations

TypeName (com.palantir.conjure.spec.TypeName)13 TypeDefinition (com.palantir.conjure.spec.TypeDefinition)6 ImmutableList (com.google.common.collect.ImmutableList)4 DealiasingTypeVisitor (com.palantir.conjure.visitor.DealiasingTypeVisitor)4 ReferenceTypeResolver (com.palantir.conjure.defs.ConjureTypeParserVisitor.ReferenceTypeResolver)3 AnnotatedConjureSourceFile (com.palantir.conjure.parser.AnnotatedConjureSourceFile)3 ConjureSourceFile (com.palantir.conjure.parser.ConjureSourceFile)3 ConjureDefinition (com.palantir.conjure.spec.ConjureDefinition)3 ErrorDefinition (com.palantir.conjure.spec.ErrorDefinition)3 HashMap (java.util.HashMap)3 ConjureRuntimeException (com.palantir.conjure.exceptions.ConjureRuntimeException)2 DefaultClassNameVisitor (com.palantir.conjure.java.types.DefaultClassNameVisitor)2 SpecializeBinaryClassNameVisitor (com.palantir.conjure.java.types.SpecializeBinaryClassNameVisitor)2 TypeMapper (com.palantir.conjure.java.types.TypeMapper)2 PathString (com.palantir.conjure.parser.services.PathString)2 EndpointDefinition (com.palantir.conjure.spec.EndpointDefinition)2 ExternalReference (com.palantir.conjure.spec.ExternalReference)2 FieldDefinition (com.palantir.conjure.spec.FieldDefinition)2 ObjectDefinition (com.palantir.conjure.spec.ObjectDefinition)2 TypeDefinitionVisitor (com.palantir.conjure.visitor.TypeDefinitionVisitor)2