private static JToken ToJson(this IOperationParameter operationParameter) { if (operationParameter == null) { return(JValue.CreateNull()); } var reference = operationParameter as Reference; if (reference != null) { return(reference.ToJson()); } var parameter = operationParameter as Parameter; if (parameter == null) { throw new ArgumentException($"Invalid operation parameter type {operationParameter.GetType()}", nameof(operationParameter)); } var jParameter = new JObject { { "name", parameter.Name.ToJson() }, { "in", parameter.In.ToJson() } }; jParameter.AddOptional("description", parameter.Description.ToJson()); jParameter.AddOptional("required", parameter.Required.ToJson()); var body = parameter as BodyParameter; if (body != null) { jParameter.Add("schema", body.Schema.ToJson()); return(jParameter); } var notBody = parameter as INotBodyParameter; if (notBody == null) { throw new ArgumentException($"Invalid operation parameter type {operationParameter.GetType()}", nameof(operationParameter)); } jParameter.Add("type", notBody.Type.ToJson()); jParameter.AddOptional("format", notBody.Format.ToJson()); jParameter.AddOptional("allowEmptyValue", notBody.AllowEmptyValue.ToJson()); if (notBody.Type == ParameterType.Array) { if (notBody.Items == null) { throw new ArgumentException("Parameter type is array but items not specified", nameof(operationParameter)); } jParameter.Add("items", notBody.Items.ToJson()); } else { jParameter.AddOptional("items", notBody.Items.ToJson()); } jParameter.AddOptional("collectionFormat", notBody.CollectionFormat.ToJson()); jParameter.AddOptional("default", notBody.Default); jParameter.AddOptional("maximum", notBody.Maximum.ToJson()); jParameter.AddOptional("exclusiveMaximum", notBody.ExclusiveMaximum.ToJson()); jParameter.AddOptional("minimum", notBody.Minimum.ToJson()); jParameter.AddOptional("exclusiveMinimum", notBody.ExclusiveMinimum.ToJson()); jParameter.AddOptional("maxLength", notBody.MaxLength.ToJson()); jParameter.AddOptional("minLength", notBody.MinLength.ToJson()); jParameter.AddOptional("pattern", notBody.Pattern.ToJson()); jParameter.AddOptional("maxItems", notBody.MaxItems.ToJson()); jParameter.AddOptional("minItems", notBody.MinItems.ToJson()); jParameter.AddOptional("uniqueItems", notBody.UniqueItems.ToJson()); jParameter.AddOptional("enum", notBody.Enum); jParameter.AddOptional("multipleOf", notBody.MultipleOf.ToJson()); return(jParameter); }
private IOperationParameter ResolveParameter(IOperationParameter parameter) { if (!typesMap.ContainsKey(parameter.Type)) { return parameter; } if (parameter is BodyOperationParameter) { return new BodyOperationParameter(typesMap[parameter.Type], parameter.IsRequired); } throw new InvalidOperationException("Type reference in non-body operation parameter, possible invalid swagger"); }