private async Task <SigSpecOperation> GenerateOperationAsync(Type type, MethodInfo method, JsonSchemaGenerator generator, SigSpecSchemaResolver resolver, SigSpecOperationType operationType) { var operation = new SigSpecOperation { Description = await type.GetXmlSummaryAsync(), Type = operationType }; foreach (var arg in method.GetParameters()) { var parameter = await generator.GenerateWithReferenceAndNullabilityAsync <SigSpecParameter>( arg.ParameterType, arg.GetCustomAttributes(), resolver, async (p, s) => { p.Description = await arg.GetXmlDocumentationAsync(); }); operation.Parameters[arg.Name] = parameter; } var returnType = operationType == SigSpecOperationType.Observable ? method.ReturnType.GetGenericTypeArguments().First() : method.ReturnType == typeof(Task) ? null : method.ReturnType.IsGenericType && method.ReturnType.BaseType == typeof(Task) ? method.ReturnType.GetGenericTypeArguments().First() : method.ReturnType; operation.ReturnType = returnType == null ? null : await generator.GenerateWithReferenceAndNullabilityAsync <JsonSchema4>( returnType, null, resolver, async (p, s) => { p.Description = await method.ReturnType.GetXmlSummaryAsync(); }); return(operation); }
private async Task <SigSpecOperation> GenerateOperationAsync(Type type, MethodInfo method, JsonSchemaGenerator generator, SigSpecSchemaResolver resolver) { var operation = new SigSpecOperation(); operation.Description = await type.GetXmlSummaryAsync(); foreach (var arg in method.GetParameters()) { var parameter = await generator.GenerateWithReferenceAndNullabilityAsync <SigSpecParameter>( arg.ParameterType, arg.GetCustomAttributes(), resolver, async (p, s) => { p.Description = await arg.GetXmlDocumentationAsync(); }); operation.Parameters[arg.Name] = parameter; } return(operation); }
/// <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); }