private Schema CreateResourceChildSchema(ResourceChildItem resourceChildItem, SwaggerResource swaggerResource) { var properties = resourceChildItem.Properties .Select( p => new { IsRequired = p.IsIdentifying || !p.PropertyType.IsNullable, Key = UniqueIdSpecification.GetUniqueIdPropertyName(p.JsonPropertyName).ToCamelCase(), Schema = SwaggerDocumentHelper.CreatePropertySchema(p) }).Concat( resourceChildItem.References.Select( r => new { r.IsRequired, Key = r.PropertyName.ToCamelCase(), Schema = new Schema { @ref = SwaggerDocumentHelper.GetDefinitionReference( _swaggerDefinitionsFactoryNamingStrategy.GetReferenceName(swaggerResource.Resource, r)) } })).Concat( resourceChildItem.Collections.Select( c => new { IsRequired = c.Association.IsRequiredCollection, Key = c.JsonPropertyName, Schema = CreateCollectionSchema(c, swaggerResource) })).Concat( resourceChildItem.EmbeddedObjects.Select( e => new { IsRequired = false, Key = e.JsonPropertyName, Schema = CreateEmbeddedObjectSchema(e, swaggerResource) })).ToList(); var bridgeSchema = GetEdFiExtensionBridgeReferenceSchema(resourceChildItem, swaggerResource); if (bridgeSchema != null) { properties.Add( new { IsRequired = false, Key = ExtensionCollectionKey, Schema = bridgeSchema }); } var requiredProperties = properties.Where(x => x.IsRequired).Select(x => x.Key).ToList(); return(new Schema { type = "object", required = requiredProperties.Any() ? requiredProperties : null, properties = properties.ToDictionary(k => k.Key, v => v.Schema) }); }
private bool ShouldIncludeInQueryCriteria(PropertyDescriptor property, object value, TEntity entity) { // Null values and underscore-prefixed properties are ignored for specification purposes if (value == null || property.Name.StartsWith("_") || "|Url|".Contains(property.Name)) { // TODO: Come up with better way to exclude non-data properties return(false); } Type valueType = value.GetType(); // Only use value types (or strings), and non-default values (i.e. ignore 0's) var result = (valueType.IsValueType || valueType == typeof(string)) && (!value.Equals(valueType.GetDefaultValue()) || UniqueIdSpecification.IsUSI(property.Name) && GetPropertyValue(entity, UniqueIdSpecification.GetUniqueIdPropertyName(property.Name)) != null); // Don't include properties that are explicitly to be ignored result = result && !_propertiesToIgnore.Contains(property.Name); // Don't include UniqueId properties when they appear on a Person entity result = result && (!_uniqueIdProperties.Contains(property.Name) || PersonEntitySpecification.IsPersonEntity(entity.GetType())); return(result); }
/// <summary> /// Will convert a USI property to a UniqueID format /// If the property is not a USI type property, then an exception is thrown. /// </summary> /// <param name="usiPropertyName"></param> /// <returns></returns> public static string ConvertToUniqueId(this string usiPropertyName) { if (!UniqueIdSpecification.IsUSI(usiPropertyName)) { throw new ArgumentException( string.Format( "Supplied property '{0}' is not an USI property.", usiPropertyName)); } return(UniqueIdSpecification.GetUniqueIdPropertyName(usiPropertyName)); }
/// <summary> /// Converts surrogate id property names to their publicly visible counterparts. /// </summary> /// <param name="entityProperty">The property to be evaluated.</param> /// <returns>The property name appropriate for use on the model abstraction.</returns> public static string GetModelsInterfacePropertyName(this EntityProperty entityProperty) { if (entityProperty.IsLookup) { return(entityProperty.PropertyName.TrimSuffix("Id")); } if (UniqueIdSpecification.IsCoreUSI(entityProperty.PropertyName)) { return(UniqueIdSpecification.GetUniqueIdPropertyName(entityProperty.PropertyName)); } return(entityProperty.PropertyName); }
protected ResourceMemberBase(ResourceClassBase resourceClass, string propertyName) { ResourceClass = resourceClass; PropertyName = propertyName; _jsonPropertyName = new Lazy <string>( () => { var jsonPropertyName = ResourceClass.AllProperties.Any(x => x.PropertyName == PropertyName) || ResourceClass.MemberNamesInvolvedInJsonCollisions.Contains(PropertyName) ? PropertyName.ToCamelCase() : JsonNamingConvention.ProposeJsonPropertyName(ParentFullName.Name, PropertyName); return(UniqueIdSpecification.IsUSI(jsonPropertyName) ? UniqueIdSpecification.GetUniqueIdPropertyName(jsonPropertyName) : jsonPropertyName); }); }
private static IDictionary <string, List <string> > ExpectedPropertyNamesByDefinitionName(IEnumerable <Resource> resources) { var namingStrategy = new SwaggerDefinitionsFactoryDefaultNamingStrategy(); var definitions = resources.Select( d => new { DefinitionName = namingStrategy.GetResourceName(d, new SwaggerResource(d)), Properties = d.UnifiedKeyAllProperties().Select(p => p.JsonPropertyName).Concat( d.Collections.Select( c => c.IsDerivedEntityATypeEntity() && c.IsInherited ? c.Association.OtherEntity.PluralName.ToCamelCase() : c.JsonPropertyName)).Concat(d.EmbeddedObjects.Select(e => e.JsonPropertyName)) .Concat(d.References.Select(r => r.PropertyName.ToCamelCase())) }).Concat( resources.SelectMany(r => r.AllContainedItemTypes).Select( i => new { DefinitionName = namingStrategy.GetContainedItemTypeName( new SwaggerResource(new Resource("TestResource")), i).ToCamelCase(), Properties = i.Properties .Select(p => UniqueIdSpecification.GetUniqueIdPropertyName(p.JsonPropertyName)) .Concat(i.Collections.Select(c => c.JsonPropertyName)) .Concat(i.EmbeddedObjects.Select(e => e.JsonPropertyName)) .Concat(i.References.Select(r => r.PropertyName.ToCamelCase())) })).Concat( resources.SelectMany(x => x.AllContainedReferences).Distinct(ModelComparers.ReferenceTypeNameOnly).Select( reference => new { DefinitionName = namingStrategy.GetReferenceName(new Resource("TestResource"), reference), Properties = reference.ReferenceTypeProperties.Where(p => p.IsIdentifying).Select( p => UniqueIdSpecification.GetUniqueIdPropertyName(p.JsonPropertyName)) })).ToList(); return(definitions.GroupBy(x => x.DefinitionName).Select(x => x.First()).ToDictionary( k => k.DefinitionName.ToCamelCase(), v => v.Properties.ToList())); }
public static PropertyData CreateReferencedProperty( ResourceProperty property, string desc = null, string className = null, ResourceClassBase resource = null) { var propertyData = new PropertyData(property); var associations = resource == null || !resource.References.Any() ? property.EntityProperty.IncomingAssociations : resource.References.Where( r => r.Properties.Contains( property, ModelComparers.ResourcePropertyNameOnly)) .Select(r => r.Association) .ToList(); // we want to prioritize identifiers that are not optional. var association = associations .OrderBy(x => x.ThisProperties.Any(y => y.PropertyType.IsNullable)) .ThenBy(x => x.Name) .FirstOrDefault( x => x.PropertyMappingByThisName.ContainsKey(property.PropertyName) || x.PropertyMappingByThisName.ContainsKey(property.EntityProperty.PropertyName)); var parent = association != null ? association.PropertyMappingByThisName.ContainsKey(property.PropertyName) ? association.PropertyMappingByThisName[property.PropertyName] .OtherProperty : association.PropertyMappingByThisName.ContainsKey(property.EntityProperty.PropertyName) ? association.PropertyMappingByThisName[property.EntityProperty.PropertyName] .OtherProperty : null : null; string parentPropertyName = parent != null ? property.IsLookup ? parent.PropertyName.TrimSuffix("Id") : UniqueIdSpecification.GetUniqueIdPropertyName(parent.PropertyName) : property.ParentFullName.Name; propertyData[ResourceRenderer.ParentPropertyName] = UniqueIdSpecification.GetUniqueIdPropertyName(parentPropertyName); if (className != null) { propertyData[ResourceRenderer.ClassName] = className; } if (desc != null) { propertyData[ResourceRenderer.DescriptionOverride] = desc; } if (associations.Any()) { propertyData.Associations.AddRange(associations); } if (property.IsLookup) { propertyData[ResourceRenderer.RenderType] = ResourceRenderer.RenderUnified; propertyData[ResourceRenderer.MiscellaneousComment] = string.Format( "// IS in a reference ({0}.{1}Id), IS a lookup column ", className ?? property.EntityProperty.Entity.ResolvedEdFiEntityName(), property.PropertyName); } else { propertyData[ResourceRenderer.MiscellaneousComment] = "// IS in a reference, NOT a lookup column "; propertyData[ResourceRenderer.RenderType] = ResourceRenderer.RenderReferenced; } return(propertyData); }