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}");
}
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}");
}
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}}");
}
});
}
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();
}
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;
}
Aggregations