/// <summary>Creates a <see cref="JsonSchema4" /> from a given type.</summary>
        /// <typeparam name="TType">The type to create the schema for.</typeparam>
        /// <param name="settings">The settings.</param>
        /// <returns>The <see cref="JsonSchema4" />.</returns>
        public static async Task <JsonSchema4> FromTypeAsync <TType>(JsonSchemaGeneratorSettings settings)
        {
            var generator = new JsonSchemaGenerator(settings);

            return(await generator.GenerateAsync(typeof(TType)).ConfigureAwait(false));
        }
Beispiel #2
0
        /// <summary>Creates a primitive parameter for the given parameter information reflection object.</summary>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <param name="parameterType">Type of the parameter.</param>
        /// <param name="parentAttributes">The parent attributes.</param>
        /// <returns>The parameter.</returns>
        public async Task <SwaggerParameter> CreatePrimitiveParameterAsync(
            string name, string description, Type parameterType, IEnumerable <Attribute> parentAttributes)
        {
            SwaggerParameter operationParameter;

            var typeDescription = _settings.ReflectionService.GetDescription(parameterType, parentAttributes, _settings);

            if (typeDescription.RequiresSchemaReference(_settings.TypeMappers))
            {
                var schema = await _schemaGenerator
                             .GenerateAsync(parameterType, parentAttributes, _schemaResolver)
                             .ConfigureAwait(false);

                operationParameter      = new SwaggerParameter();
                operationParameter.Type = typeDescription.Type;

                if (_settings.SchemaType == SchemaType.Swagger2)
                {
                    operationParameter.CustomSchema = new JsonSchema4 {
                        SchemaReference = schema.ActualSchema
                    };

                    // Copy enumeration for compatibility with other tools which do not understand x-schema.
                    // The enumeration will be ignored by NSwag and only the x-schema is processed
                    if (schema.ActualSchema.IsEnumeration)
                    {
                        operationParameter.Enumeration.Clear();
                        foreach (var item in schema.ActualSchema.Enumeration)
                        {
                            operationParameter.Enumeration.Add(item);
                        }
                    }
                }
                else
                {
                    // TODO(OpenApi3): How to handle this in OpenApi3?
                    operationParameter.Schema = new JsonSchema4 {
                        SchemaReference = schema.ActualSchema
                    };
                }
            }
            else
            {
                operationParameter = await _schemaGenerator
                                     .GenerateAsync <SwaggerParameter>(parameterType, parentAttributes, _schemaResolver)
                                     .ConfigureAwait(false);

                if (typeDescription.Type.HasFlag(JsonObjectType.Array))
                {
                    operationParameter.CollectionFormat = SwaggerParameterCollectionFormat.Multi;
                }
            }

            operationParameter.Name       = name;
            operationParameter.IsRequired = parentAttributes.TryGetIfAssignableTo("RequiredAttribute", TypeNameStyle.Name) != null;

            if (_settings.SchemaType == SchemaType.Swagger2)
            {
                operationParameter.IsNullableRaw = typeDescription.IsNullable;
            }
            else if (typeDescription.IsNullable)
            {
                operationParameter.Type = typeDescription.Type | JsonObjectType.Null;
            }

            _schemaGenerator.ApplyDataAnnotations(operationParameter, typeDescription, parentAttributes);

            if (description != string.Empty)
            {
                operationParameter.Description = description;
            }

            return(operationParameter);
        }
Beispiel #3
0
        /// <summary>Creates a primitive parameter for the given parameter information reflection object.</summary>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <param name="parameterType">Type of the parameter.</param>
        /// <param name="parentAttributes">The parent attributes.</param>
        /// <returns>The parameter.</returns>
        public async Task <SwaggerParameter> CreatePrimitiveParameterAsync(
            string name, string description, Type parameterType, IEnumerable <Attribute> parentAttributes)
        {
            SwaggerParameter operationParameter;

            var typeDescription = _settings.ReflectionService.GetDescription(parameterType, parentAttributes, _settings);

            if (typeDescription.RequiresSchemaReference(_settings.TypeMappers))
            {
                var schema = await _schemaGenerator
                             .GenerateAsync(parameterType, parentAttributes, _schemaResolver)
                             .ConfigureAwait(false);

                operationParameter      = new SwaggerParameter();
                operationParameter.Type = typeDescription.Type;

                if (_settings.SchemaType == SchemaType.Swagger2)
                {
                    operationParameter.CustomSchema = new JsonSchema4 {
                        SchemaReference = schema.ActualSchema
                    }
                }
                ;
                else
                {
                    operationParameter.Schema = new JsonSchema4 {
                        SchemaReference = schema.ActualSchema
                    }
                };
            }
            else
            {
                operationParameter = await _schemaGenerator
                                     .GenerateAsync <SwaggerParameter>(parameterType, parentAttributes, _schemaResolver)
                                     .ConfigureAwait(false);

                if (typeDescription.Type.HasFlag(JsonObjectType.Array))
                {
                    operationParameter.CollectionFormat = SwaggerParameterCollectionFormat.Multi;
                }
            }

            operationParameter.Name       = name;
            operationParameter.IsRequired = parentAttributes.TryGetIfAssignableTo("RequiredAttribute", TypeNameStyle.Name) != null;

            if (_settings.SchemaType == SchemaType.Swagger2)
            {
                operationParameter.IsNullableRaw = typeDescription.IsNullable;
            }
            else if (typeDescription.IsNullable)
            {
                operationParameter.Type = typeDescription.Type | JsonObjectType.Null;
            }

            _schemaGenerator.ApplyDataAnnotations(operationParameter, typeDescription, parentAttributes);

            if (description != string.Empty)
            {
                operationParameter.Description = description;
            }

            return(operationParameter);
        }