The [Required] (RequiredAttribute) decorator has no effect.
public class Model
{
[Required]
public string Name { get; set; }
}
I can't get anything to marked as required. Can anyone share some guidance? This seems like it should be pretty basic, but there's not a mention of anything in the readme.
Sorta unacceptable to not have a way to indicate if a parameter is required or not. I'm trying to use the swagger doc to generate client libraries. Having everything required: false makes things much harder to use.
Are we talking about required on input model for validation?
Or data returned from Api calls? There's a difference between required and null-able as I have learned through my quest further down this page.
@ThisNoName Required on the input model for validation.
@vanillajonathan In my case, everything works fine with model Post FromBody. But not query parameters (Get w/ QueryString or Post w/o FromBody, core2 model binding will convert the latter into query parameters). There's another issue 480 floating around about Required not working on query string.
[HttpPost]
public Task<int> Create([FromBody] MessageCreateRequest request) { ... }
public class MessageCreateRequest
{
[Required] public string ToId { get; set; }
[Required] public string FromId { get; set; }
[Required, StringLength(90, MinimumLength = 10)] public string Subject { get; set; }
[Required] public string Body { get; set; }
}
MessageCreateRequest: {
description: "Message create or reply request",
required: [
"toId",
"fromId",
"subject",
"body"
],
type: "object",
properties: {
toId: {
description: "Msg ToId",
type: "string"
},
fromId: {
description: "FromId",
type: "string"
},
subject: {
description: "Subject up to 90 characters",
maxLength: 90,
minLength: 10,
type: "string"
},
body: {
type: "string"
}
}
}
@ThisNoName Ah thanks. I didn't use [FromBody]. Thanks.
Should be resolved as of v1.1.0
Tested this fix in v1.1.0, but the following .NET Core 2 endpoint:
public async Task<IActionResult> GetDeviceHistory(
[Required][FromQuery] string serialNumber,
[Required][FromQuery] string productFamily,
[Required][FromQuery] DateTime startDate,
[Required][FromQuery] DateTime endDate)
only marks the DateTime properties as required in the Swagger file. serialNumber and productFamily remain optional.
Almost there I guess?
Same for me too. Doesn't work with v1.1.0 on query parameters. Why this is closed?
Same for me.
public async Task<IActionResult> AdviseursZoeken([Required] string postcode, [Required] int straal)
Results in
"parameters": [
{
"name": "postcode",
"in": "query",
"description": "De postcode",
"required": false,
"type": "string"
},
{
"name": "straal",
"in": "query",
"description": "De straal waarbinnen gezocht moet worden gegeven de postcode",
"required": true,
"type": "integer",
"format": "int32"
}
],
I'm also getting this issue on v1.1.0
@domaindrivendev Ping!
Same problem on v1.1.0 AspNetCore
My workaround for this was to create a schema filter that will fill required array with names of properties marked with Required attribute.
public class SchemaFilter : ISchemaFilter
{
public void Apply(Schema schema, SchemaFilterContext context)
{
var type = context.SystemType;
if (schema?.Properties == null || type == null)
return;
var required = type.GetProperties()
?.Where(t => t.GetCustomAttribute<RequiredAttribute>() != null);
if (required == null || !required.Any())
return;
schema.Required = required.Select(p => p.Name).ToList();
}
}
@antoniaelek
how to use it? how to register it?