Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property of type IFormFile is nullable even with NotEmpty rule #131

Open
icnocop opened this issue Apr 25, 2023 · 3 comments
Open

Property of type IFormFile is nullable even with NotEmpty rule #131

icnocop opened this issue Apr 25, 2023 · 3 comments

Comments

@icnocop
Copy link
Contributor

icnocop commented Apr 25, 2023

Hi.

If I have an action which accepts a model decorated with FromForm which contains a property of type IFormFile which has a NotEmpty rule, the OpenAPI schema is generated and unexpectedly has nullable: true on that property.

I expected the property to not be nullable because of the NotEmpty rule.

It may be related to #24

See #130

The unit test in IFormFileTests passes successfully, but when I view the swagger UI or generated swagger.json of the UploadFile action in SampleNSwagWebApi, nullable is set to true.

image

image

Thank you.

@petriashev
Copy link
Member

Hi Rami
I have looked at this case.
Swachbuckle and NSwag do form-data processing with other filter type.
I've created a prototype for Swashbuckle and will do the same for NSwag

@icnocop
Copy link
Contributor Author

icnocop commented May 1, 2023

Thank you.

As a work-around, I created a document processor for NSwag:

    using System.Linq;
    using NJsonSchema;
    using NSwag.Generation.Processors;
    using NSwag.Generation.Processors.Contexts;

    public class FormFileSchemaProcessor : IDocumentProcessor
    {
        private const string MultipartFormData = "multipart/form-data";

        public void Process(DocumentProcessorContext context)
        {
            foreach (var (_, operation) in context.Document.Paths.SelectMany(x => x.Value))
            {
                var requestBody = operation.RequestBody;
                if (requestBody == null)
                {
                    continue;
                }

                if (!requestBody.Content.ContainsKey(MultipartFormData))
                {
                    continue;
                }

                var multipartFormData = requestBody.Content[MultipartFormData];
                foreach (var property in multipartFormData.Schema.Properties)
                {
                    var propertyValue = property.Value;
                    if ((propertyValue.Type == JsonObjectType.String)
                        && (propertyValue.Format == JsonFormatStrings.Binary))
                    {
                        propertyValue.IsRequired = true;
                    }
                }
            }
        }
    }

In Startup.cs:

services.AddOpenApiDocument((settings, serviceProvider) =>
{
    ...
    settings.DocumentProcessors.Add(new FormFileSchemaProcessor());
});

@molekp
Copy link

molekp commented May 5, 2023

Hi,

I also have same issue, but I'm using Swacsbuckle.

To reproduce it you can simply copy FilesController from SampleNSwagWebApi to SampleWebApi and start SampleWebApi. You will get similar results:
image

    "/api/Files/UploadFile": {
      "post": {
        "tags": [
          "Files"
        ],
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "File": {
                    "type": "string",
                    "format": "binary"
                  }
                }
              },
              "encoding": {
                "File": {
                  "style": "form"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success"
          }
        }
      }
    }

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants