Skip to content

Commit

Permalink
Allow multiple mime types in OpenApiBodyParameterAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Sep 15, 2020
1 parent 0e580f6 commit 612181f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/NSwag.Annotations/OpenApiBodyParameterAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ public class OpenApiBodyParameterAttribute : Attribute
/// <summary>Initializes a new instance of the <see cref="OpenApiBodyParameterAttribute"/> class.</summary>
public OpenApiBodyParameterAttribute()
{
MimeType = "application/json";
MimeTypes = new[] { "application/json" };
}

/// <summary>Initializes a new instance of the <see cref="OpenApiBodyParameterAttribute"/> class.</summary>
/// <param name="mimeType">The expected mime type.</param>
public OpenApiBodyParameterAttribute(string mimeType)
/// <param name="mimeTypes">The expected mime types.</param>
public OpenApiBodyParameterAttribute(params string[] mimeTypes)
{
MimeType = mimeType;
MimeTypes = mimeTypes;
}

/// <summary>
/// Gets the expected body mime type.
/// </summary>
public string MimeType { get; }
public string[] MimeTypes { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,20 @@ private void ApplyOpenApiBodyParameterAttribute(OpenApiOperationDescription oper
operationDescription.Operation.RequestBody = new OpenApiRequestBody();
}

operationDescription.Operation.RequestBody.Content[bodyParameterAttribute.MimeType] = new OpenApiMediaType
var mimeTypes = ObjectExtensions.HasProperty(bodyParameterAttribute, "MimeType") ?
new string[] { bodyParameterAttribute.MimeType } : bodyParameterAttribute.MimeTypes;

foreach (var mimeType in mimeTypes)
{
Schema = bodyParameterAttribute.MimeType == "application/json" ? JsonSchema.CreateAnySchema() : new JsonSchema
operationDescription.Operation.RequestBody.Content[mimeType] = new OpenApiMediaType
{
Type = _settings.SchemaType == SchemaType.Swagger2 ? JsonObjectType.File : JsonObjectType.String,
Format = _settings.SchemaType == SchemaType.Swagger2 ? null : JsonFormatStrings.Binary,
}
};
Schema = mimeType == "application/json" ? JsonSchema.CreateAnySchema() : new JsonSchema
{
Type = _settings.SchemaType == SchemaType.Swagger2 ? JsonObjectType.File : JsonObjectType.String,
Format = _settings.SchemaType == SchemaType.Swagger2 ? null : JsonFormatStrings.Binary,
}
};
}
}
}

Expand Down

0 comments on commit 612181f

Please sign in to comment.