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

Fix required handling for formdata properties #4994

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,21 @@ public class CaseAttachmentModel

public IFormFile Contents { get; set; }
}

[HttpPost("UploadAttachment2")]
public Task<IActionResult> UploadAttachment2(
[FromForm][Required] CaseAttachmentModel2 model,
[Required] IFormFile contents)
{
return Task.FromResult<IActionResult>(Ok());
}

public class CaseAttachmentModel2
{
[Required]
public string Title { get; init; }

public int? MessageId { get; set; }
}
}
}
51 changes: 51 additions & 0 deletions src/NSwag.Generation.AspNetCore.Tests/Parameters/FormDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,56 @@ public async Task WhenOperationHasFormDataComplex_ThenItIsInRequestBody()
}
},".Replace("\r", ""), json.Replace("\r", ""));
}

[Fact]
public async Task WhenOperationHasFormDataComplexWithRequiredProperties_ThenItIsInRequestBody()
{
// Arrange
var settings = new AspNetCoreOpenApiDocumentGeneratorSettings
{
SchemaSettings = new NewtonsoftJsonSchemaGeneratorSettings
{
SchemaType = SchemaType.OpenApi3
}
};

// Act
var document = await GenerateDocumentAsync(settings, typeof(FileUploadController));
var json = document.ToJson();

// Assert
var operation = document.Operations.First(o => o.Operation.OperationId == "FileUpload_UploadAttachment2").Operation;

Assert.NotNull(operation);
Assert.Contains(@"""requestBody"": {
""content"": {
""multipart/form-data"": {
""schema"": {
""type"": ""object"",
""required"": [
""Title"",
""contents""
],
""properties"": {
""Title"": {
""type"": ""string"",
""nullable"": false
},
""MessageId"": {
""type"": ""integer"",
""format"": ""int32"",
""nullable"": true
},
""contents"": {
""type"": ""string"",
""format"": ""binary"",
""nullable"": false
}
}
}
}
}
},".Replace("\r", ""), json.Replace("\r", ""));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,18 @@ private JsonSchema CreateOrGetFormDataSchema(OperationProcessorContext context)
return requestBody.Content[MultipartFormData].Schema;
}

private static JsonSchemaProperty CreateFormDataProperty(OperationProcessorContext context, ExtendedApiParameterDescription extendedApiParameter, JsonSchema schema)
private JsonSchemaProperty CreateFormDataProperty(OperationProcessorContext context, ExtendedApiParameterDescription extendedApiParameter, JsonSchema schema)
{
return context.SchemaGenerator.GenerateWithReferenceAndNullability<JsonSchemaProperty>(
var formDataProperty = context.SchemaGenerator.GenerateWithReferenceAndNullability<JsonSchemaProperty>(
extendedApiParameter.ApiParameter.Type.ToContextualType(extendedApiParameter.Attributes), context.SchemaResolver);

var contextualPropertyType = extendedApiParameter.ParameterType.ToContextualType();
var typeDescription = _settings.SchemaSettings.ReflectionService.GetDescription(contextualPropertyType, _settings.SchemaSettings);
var isRequired = extendedApiParameter.IsRequired(_settings.RequireParametersWithoutDefault);
formDataProperty.IsRequired = isRequired;
formDataProperty.IsNullableRaw = _settings.AllowNullableBodyParameters && !isRequired && typeDescription.IsNullable;

return formDataProperty;
}

private bool IsFileArray(Type type, JsonTypeDescription typeInfo)
Expand Down Expand Up @@ -527,7 +535,7 @@ public bool IsRequired(bool requireParametersWithoutDefault)
// available in asp.net core >= 2.2
if (ApiParameter.HasProperty("IsRequired"))
{
isRequired = ApiParameter.TryGetPropertyValue("IsRequired", false);
isRequired = ApiParameter.TryGetPropertyValue("IsRequired", false) || ApiParameter.ModelMetadata?.IsRequired == true;
}
else
{
Expand All @@ -538,7 +546,6 @@ public bool IsRequired(bool requireParametersWithoutDefault)
}
else if (ApiParameter.ModelMetadata != null &&
ApiParameter.ModelMetadata.IsBindingRequired)

{
isRequired = true;
}
Expand Down
Loading