/// <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(); if (_settings.SchemaType == SchemaType.Swagger2) { operationParameter.Type = typeDescription.Type; operationParameter.CustomSchema = new JsonSchema4 { Reference = 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 { if (typeDescription.IsNullable) { operationParameter.Schema = new JsonSchema4 { IsNullableRaw = true }; operationParameter.Schema.OneOf.Add(new JsonSchema4 { Reference = schema.ActualSchema }); } else { operationParameter.Schema = new JsonSchema4 { Reference = schema.ActualSchema }; } } } else { if (_settings.SchemaType == SchemaType.Swagger2) { operationParameter = await _schemaGenerator .GenerateAsync <SwaggerParameter>(parameterType, parentAttributes, _schemaResolver) .ConfigureAwait(false); } else { operationParameter = new SwaggerParameter { Schema = await _schemaGenerator .GenerateWithReferenceAndNullabilityAsync <JsonSchema4>( parameterType, parentAttributes, typeDescription.IsNullable, _schemaResolver) .ConfigureAwait(false) }; } } operationParameter.Name = name; operationParameter.IsRequired = parentAttributes.TryGetIfAssignableTo("RequiredAttribute", TypeNameStyle.Name) != null; if (typeDescription.Type.HasFlag(JsonObjectType.Array)) { operationParameter.CollectionFormat = SwaggerParameterCollectionFormat.Multi; } operationParameter.IsNullableRaw = typeDescription.IsNullable; _schemaGenerator.ApplyDataAnnotations(operationParameter, typeDescription, parentAttributes); if (description != string.Empty) { operationParameter.Description = description; } return(operationParameter); }
/// <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); }