示例#1
0
        private void ProcessProperties(IOpenApiSchemaAcceptor instance, string schemaName, Dictionary <string, PropertyInfo> properties, NamingStrategy namingStrategy)
        {
            var schemas = new Dictionary <string, OpenApiSchema>();

            var subAcceptor = new OpenApiSchemaAcceptor()
            {
                Properties  = properties,
                RootSchemas = instance.RootSchemas,
                Schemas     = schemas,
            };

            subAcceptor.Accept(this.VisitorCollection, namingStrategy);

            // Add required properties to schema.
            var jsonPropertyAttributes = properties.Where(p => !p.Value.GetCustomAttribute <JsonPropertyAttribute>(inherit: false).IsNullOrDefault())
                                         .Select(p => new KeyValuePair <string, JsonPropertyAttribute>(p.Key, p.Value.GetCustomAttribute <JsonPropertyAttribute>(inherit: false)))
                                         .Where(p => p.Value.Required == Required.Always || p.Value.Required == Required.DisallowNull);

            foreach (var attribute in jsonPropertyAttributes)
            {
                instance.Schemas[schemaName].Required.Add(attribute.Key);
            }

            var jsonRequiredAttributes = properties.Where(p => !p.Value.GetCustomAttribute <JsonRequiredAttribute>(inherit: false).IsNullOrDefault())
                                         .Select(p => new KeyValuePair <string, JsonRequiredAttribute>(p.Key, p.Value.GetCustomAttribute <JsonRequiredAttribute>(inherit: false)));

            foreach (var attribute in jsonRequiredAttributes)
            {
                var attributeName = namingStrategy.GetPropertyName(attribute.Key, hasSpecifiedName: false);
                if (instance.Schemas[schemaName].Required.Contains(attributeName))
                {
                    continue;
                }

                instance.Schemas[schemaName].Required.Add(attributeName);
            }

            instance.Schemas[schemaName].Properties = subAcceptor.Schemas;

            // Adds schemas to the root.
            var schemasToBeAdded = subAcceptor.Schemas
                                   .Where(p => !instance.Schemas.Keys.Contains(p.Key))
                                   .Where(p => p.Value.IsOpenApiSchemaObject())
                                   .GroupBy(p => p.Value.Title)
                                   .Select(p => p.First())
                                   .ToDictionary(p => p.Value.Title, p => p.Value);

            foreach (var schema in schemasToBeAdded.Where(p => p.Key != "jObject" && p.Key != "jToken"))
            {
                if (instance.RootSchemas.ContainsKey(schema.Key))
                {
                    continue;
                }

                instance.RootSchemas.Add(schema.Key, schema.Value);
            }

            // Removes title of each property.
            var subSchemas = instance.Schemas[schemaName].Properties;

            subSchemas = subSchemas.Select(p =>
            {
                p.Value.Title = null;
                return(new KeyValuePair <string, OpenApiSchema>(p.Key, p.Value));
            })
                         .ToDictionary(p => p.Key, p => p.Value);

            instance.Schemas[schemaName].Properties = subSchemas;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DocumentHelper"/> class.
 /// </summary>
 /// <param name="filter"><see cref="RouteConstraintFilter"/> instance.</param>
 /// <param name="acceptor"><see cref="IAcceptor"/> instance.</param>
 public DocumentHelper(RouteConstraintFilter filter, IOpenApiSchemaAcceptor acceptor)
 {
     this._filter   = filter.ThrowIfNullOrDefault();
     this._acceptor = acceptor.ThrowIfNullOrDefault();
 }