question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model schema for multipart header

See original GitHub issue

Hi, is there a way to specify a model schema when using a multipart POST/PUT request? We’re sending both a file and a JSON string mapped to one of our classes and currently no model schema shows up in the generated documentation. What we’d like to add is minimally a model schema for the custom class object (in the example below a Document). I don’t see a way to do this currently, at least through the documentation and what I could find on the web.

Our methods are pretty straight forward and not sure if it’s helpful but here’s what we have:

[ValidateModel]
[HttpPost]
[ValidateMimeMultipartContent]
[Route(Name = "CreateNewDocument")]
[SwaggerResponse(HttpStatusCode.Created, null, typeof(Document))]
[SwaggerResponse(HttpStatusCode.InternalServerError)]
[SwaggerResponse(HttpStatusCode.BadRequest)]
[SwaggerResponse(HttpStatusCode.Unauthorized)]
public async Task<HttpResponseMessage> Post()
{
        ...
        Document value = null;
        ...
        var provider = new MultipartFormDataStreamProvider(tempPath);

        // Read the form data.
        var allData = await Request.Content.ReadAsMultipartAsync(provider);

        string documentJson = allData.FormData["DocumentModel"].NormalizeJson();

        // get document data
        value = JsonConvert.DeserializeObject<Document>(documentJson);
        ...
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
HarelMcommented, Jul 30, 2016

I had the exact same scenario. Here is the backwards engineering I made to make it work: controller action (which receives multi-part data):

[SwaggerOperationFilter(typeof(ReportProblemOperationFilter))]
public async Task<IHttpActionResult> PostProblem()
{
    ...

The object to specify the schema for is ReportProblem: The code for the filter referenced above:

    public class ReportProblemOperationFilter : IOperationFilter
    {
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            operation.consumes.Add("application/form-data");
            var reportProblemSchema = schemaRegistry.GetOrRegister(typeof (ReportProblem));
            operation.parameters = new[]
            {
                new Parameter
                {
                    name = "file",
                    @in = "formData",
                    required = true,
                    type = "file",
                    description = "The screenshot to upload"
                },
                new Parameter
                {
                    name = "info",
                    @in = "body",
                    required = true,
                    type = "object",
                    schema = reportProblemSchema,
                    description = "Other related data"
                }
            };
        }
    }

Hope this will help you guys, I’ve search this issue through about 5 different swashbuckle issue to get it going in the right direction.

Final screenshot: swagger

Here are some related issues: #283, #638.

0reactions
rvhuangcommented, Sep 13, 2017

HI all. Is there any way to specify acceptable content type of each part in multi-part request? For example, an API that allows user to upload one or more image files. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Model schema for multipart header · Issue #796
Hi, is there a way to specify a model schema when using a multipart POST/PUT request? We're sending both a file and a...
Read more >
Multipart Requests
You describe individual parts of the request as properties of the schema object. As you can see, a multipart request can include various...
Read more >
OpenAPI Example multipart form data - schema
I have a multipart/form-data POST in an API endpoint which takes some key/value strings, and a file upload via the key files ....
Read more >
Define schema for multipart/form-data
Hi folks,. In my raml i need to define the schema for the body of a POST method. Content type of the body...
Read more >
Media type object with 'multipart/form-data' has no schema ...
One or more media type objects in your API where Content-Type is set to multipart/form-data do not have schemas defined. Schema is required...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found