public override string ToString() { return($"{nameof(TargetType)}: {TargetType.BeautifulName()}, {nameof(Property)}: {Property.Name}, {nameof(RouteTemplateParameterName)}: {RouteTemplateParameterName}"); }
public KeyPropertiesOfSchema(string schemaPropertyName, IEnumerable <KeyFromUriProperty> properties, Func <Type, ImmutableArray <string> > getTemplates) { SchemaPropertyName = schemaPropertyName; Properties = properties.ToImmutableArray(); TargetType = Properties.Select(p => p.TargetType).Distinct().Single(); IsRequired = Properties.Any(p => p.Property.GetCustomAttribute <RequiredAttribute>() != null); var templates = getTemplates(TargetType); if (templates == null || templates.Length == 0) { throw new HypermediaException($"No Get route found for hypermedia type '{TargetType.BeautifulName()}'"); } TemplateMatchers = templates.Select(RouteMatcher.GetTemplateMatcher).ToImmutableArray(); if (Properties.Length == 1) { var property = Properties[0]; if (property.RouteTemplateParameterName == null && TemplateMatchers.All(t => t.Template.Parameters.Count == 1)) { var templateParameterNames = TemplateMatchers.Select(t => t.Template.Parameters.Select(p => p.Name).First()).Distinct().ToImmutableArray(); if (templateParameterNames.Length > 1) { throw new HypermediaException($"Different template parameter names used in routes to objects with same base type '{TargetType.BeautifulName()}': {string.Join(",", templateParameterNames)}"); } Properties = ImmutableArray.Create(new KeyFromUriProperty(property.TargetType, property.Property, property.SchemaPropertyName, templateParameterNames[0])); } } var keyPropertiesMissingInTemplate = Properties .Where(k => TemplateMatchers.Any(t => t.Template.Parameters.All(p => p.Name != k.ResolvedRouteTemplateParameterName))) .ToImmutableArray(); if (keyPropertiesMissingInTemplate.Any()) { throw new ArgumentException($"Type {Properties.First().Property.DeclaringType.BeautifulName()} contains KeyFromUri properties that are not represented as route template parameters: {string.Join(",", keyPropertiesMissingInTemplate.Select(k => $"Property {k.Property.Name}"))}. Route templates: {string.Join(",", TemplateMatchers.Select(r => r.Template.TemplateText))}"); } }