コード例 #1
0
        public void Apply(Schema model, SchemaFilterContext context)
        {
            if (!IsBaseOrSubtype(context.SystemType))
            {
                return;
            }
            if (context.SystemType == typeof(TBaseType))
            {
                model.Discriminator = "$type";
                model.Properties.Add(
                    "$type",
                    new Schema {
                    Enum = TypeNames.Cast <object>().ToArray(), Type = "string"
                });
                if (model.Required == null)
                {
                    model.Required = new List <string>();
                }
                model.Required.Add("$type");

                foreach (var identifierType in SubTypes)
                {
                    try
                    {
                        context.SchemaRegistry.GetOrRegister(identifierType);
                    }
                    catch (ArgumentException e)
                    {
                        throw new InvalidOperationException($"Cannot included {identifierType} in schema - see inner exception for details", e);
                    }
                }
            }
            else
            {
                var schema = new Schema {
                    Properties = model.Properties, Type = model.Type, Required = model.Required
                };
                model.AllOf = new[] { context.SchemaRegistry.GetOrRegister(typeof(TBaseType)), schema };
                model.Extensions["x-ms-client-name"]         = context.SystemType.FriendlyId();
                model.Extensions["x-ms-discriminator-value"] = context.SystemType.FriendlyId();
                model.Properties = null;
                model.Required   = null;
                model.Type       = null;
            }
        }