/// <summary>Creates an API scalar type for the API schema builder if needed.</summary> /// <param name="clrScalarType">CLR scalar type</param> /// <returns>A fluent-style API schema builder for the API schema.</returns> public static IApiSchemaBuilder ApiScalarType(this IApiSchemaBuilder apiSchemaBuilder, Type clrScalarType) { Contract.Requires(apiSchemaBuilder != null); Contract.Requires(clrScalarType != null); return(apiSchemaBuilder.ApiScalarType(clrScalarType, null)); }
/// <summary>Creates an API enumeration type for the API schema builder if needed.</summary> /// <typeparam name="TEnum">CLR enumeration type</typeparam> /// <returns>A fluent-style API schema builder for the API schema.</returns> public static IApiSchemaBuilder ApiEnumerationType <TEnum>(this IApiSchemaBuilder apiSchemaBuilder) where TEnum : Enum { Contract.Requires(apiSchemaBuilder != null); return(apiSchemaBuilder.ApiEnumerationType <TEnum>(null)); }
// PUBLIC METHODS /////////////////////////////////////////////////// #region IApiSchemaConvention Implementation public void Apply(IApiSchemaBuilder apiSchemaBuilder, ApiConventionSettings apiConventionSettings) { Contract.Requires(apiSchemaBuilder != null); apiConventionSettings = apiConventionSettings ?? ApiConventionSettings.Empty; var apiDiscoverySettings = apiConventionSettings.ApiDiscoverySettings; var assemblies = apiDiscoverySettings.Assemblies; var clrTypes = assemblies.SelectMany(x => x.GetExportedTypes()) .ToList(); foreach (var clrType in clrTypes) { var apiTypeKind = clrType.GetApiTypeKind(); switch (apiTypeKind) { case ApiTypeKind.Object: { if (ClrTypeDiscoveryRules.CanAddApiObjectType(clrType) == false) { continue; } var apiTypeDiscoveryPredicateResult = apiDiscoverySettings.ApiTypeDiscoveryPredicate?.Invoke(clrType) ?? true; if (apiTypeDiscoveryPredicateResult == false) { continue; } apiSchemaBuilder.ApiObjectType(clrType); break; } } } }
// PUBLIC METHODS /////////////////////////////////////////////////// #region IApiSchemaConvention Implementation public void Apply(IApiSchemaBuilder apiSchemaBuilder, ApiConventionSettings apiConventionSettings) { Contract.Requires(apiSchemaBuilder != null); apiConventionSettings = apiConventionSettings ?? ApiConventionSettings.Empty; var apiDiscoverySettings = apiConventionSettings.ApiDiscoverySettings; var assemblies = apiDiscoverySettings.Assemblies; var clrTypes = assemblies.SelectMany(x => x.GetExportedTypes()) .ToList(); foreach (var clrType in clrTypes) { if (ClrTypeDiscoveryRules.CanAddApiTypeConfiguration(clrType) == false) { continue; } var apiConfigurationDiscoveryPredicateResult = apiDiscoverySettings.ApiConfigurationDiscoveryPredicate?.Invoke(clrType) ?? true; if (apiConfigurationDiscoveryPredicateResult == false) { continue; } var apiTypeConfigurationNewExpression = ExpressionBuilder.New <ApiTypeConfiguration>(clrType); var apiTypeConfigurationNewLambda = apiTypeConfigurationNewExpression.Compile(); var apiTypeConfiguration = apiTypeConfigurationNewLambda(); apiSchemaBuilder.HasConfiguration(apiTypeConfiguration); } }
// PUBLIC METHODS /////////////////////////////////////////////////// #region IApiSchemaConvention Implementation public void Apply(IApiSchemaBuilder apiSchemaBuilder, ApiConventionSettings apiConventionSettings) { Contract.Requires(apiSchemaBuilder != null); apiConventionSettings = apiConventionSettings ?? ApiConventionSettings.Empty; var apiDiscoverySettings = apiConventionSettings.ApiDiscoverySettings; var assemblies = apiDiscoverySettings.Assemblies; var clrTypes = assemblies.SelectMany(x => x.GetExportedTypes()) .ToList(); var apiSchemaConfiguration = (ApiSchemaConfiguration)apiSchemaBuilder; var apiPrecedenceStack = apiSchemaConfiguration.ApiPrecedenceStack; foreach (var clrType in clrTypes) { var apiTypeKind = clrType.GetApiTypeKind(); switch (apiTypeKind) { case ApiTypeKind.Enumeration: { this.HandleApiEnumerationTypeAttribute(apiSchemaConfiguration, apiConventionSettings, apiPrecedenceStack, clrType); break; } case ApiTypeKind.Object: { this.HandleApiObjectTypeAttribute(apiSchemaConfiguration, apiConventionSettings, apiPrecedenceStack, clrType); break; } } } }
private void HandleApiObjectTypeAttribute(IApiSchemaBuilder apiSchemaBuilder, ApiConventionSettings apiConventionSettings, ApiPrecedenceStack apiPrecedenceStack, Type clrType) { Contract.Requires(apiSchemaBuilder != null); Contract.Requires(apiConventionSettings != null); Contract.Requires(clrType != null); var apiObjectTypeAttribute = (ApiObjectTypeAttribute)Attribute.GetCustomAttribute(clrType, typeof(ApiObjectTypeAttribute)); if (apiObjectTypeAttribute == null) { return; } if (ClrTypeDiscoveryRules.CanAddApiObjectType(clrType) == false) { return; } var apiDiscoverySettings = apiConventionSettings.ApiDiscoverySettings; var apiAnnotationDiscoveryPredicateResult = apiDiscoverySettings.ApiAnnotationDiscoveryPredicate?.Invoke(clrType) ?? true; if (apiAnnotationDiscoveryPredicateResult == false) { return; } if (apiObjectTypeAttribute.Excluded) { apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); apiSchemaBuilder.Exclude(clrType); apiPrecedenceStack.Pop(); return; } IApiObjectTypeBuilder ApiObjectTypeConfiguration(IApiObjectTypeBuilder apiObjectTypeBuilder) { apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); if (String.IsNullOrWhiteSpace(apiObjectTypeAttribute.Name) == false) { apiObjectTypeBuilder.HasName(apiObjectTypeAttribute.Name); } if (String.IsNullOrWhiteSpace(apiObjectTypeAttribute.Description) == false) { apiObjectTypeBuilder.HasDescription(apiObjectTypeAttribute.Description); } apiPrecedenceStack.Pop(); return(apiObjectTypeBuilder); } apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); apiSchemaBuilder.ApiObjectType(clrType, ApiObjectTypeConfiguration); apiPrecedenceStack.Pop(); }
// PUBLIC METHODS /////////////////////////////////////////////////// #region Extension Methods /// <summary>Excludes the API type from the API schema.</summary> /// <typeparam name="T">CLR type that represents the API type to exclude.</typeparam> /// <returns>A fluent-style API schema builder for the API schema.</returns> public static IApiSchemaBuilder Exclude <T>(this IApiSchemaBuilder apiSchemaBuilder) { Contract.Requires(apiSchemaBuilder != null); var clrType = typeof(T); return(apiSchemaBuilder.Exclude(clrType)); }
public void Apply(IApiSchemaBuilder apiSchemaBuilder, ApiConventionSettings apiConventionSettings) { apiSchemaBuilder.HasName(null); }
/// <summary>Creates an API scalar type for the API schema builder if needed.</summary> /// <typeparam name="TScalar">CLR scalar type</typeparam> /// <returns>A fluent-style API schema builder for the API schema.</returns> public static IApiSchemaBuilder ApiScalarType <TScalar>(this IApiSchemaBuilder apiSchemaBuilder) { Contract.Requires(apiSchemaBuilder != null); return(apiSchemaBuilder.ApiScalarType <TScalar>(null)); }