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

The [Required] attribute is not respected #523

Closed
vanillajonathan opened this issue Oct 25, 2017 · 14 comments
Closed

The [Required] attribute is not respected #523

vanillajonathan opened this issue Oct 25, 2017 · 14 comments

Comments

@vanillajonathan
Copy link
Contributor

The [Required] (RequiredAttribute) decorator has no effect.

    public class Model
    {
        [Required]
        public string Name { get; set; }
    }
@KyleGobel
Copy link

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.

@ThisNoName
Copy link

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.

@vanillajonathan
Copy link
Contributor Author

@ThisNoName Required on the input model for validation.

@ThisNoName
Copy link

@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"
		}
	}
}

@vanillajonathan
Copy link
Contributor Author

@ThisNoName Ah thanks. I didn't use [FromBody]. Thanks.

@domaindrivendev
Copy link
Owner

Should be resolved as of v1.1.0

@RubenDelange
Copy link

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?

@akrohn
Copy link

akrohn commented Dec 13, 2017

Same for me too. Doesn't work with v1.1.0 on query parameters. Why this is closed?

@rvanmaanen
Copy link

rvanmaanen commented Jan 25, 2018

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" } ],

@siada
Copy link

siada commented Jan 25, 2018

I'm also getting this issue on v1.1.0

@vanillajonathan
Copy link
Contributor Author

@domaindrivendev Ping!

@NoelKennedy
Copy link

Same problem on v1.1.0 AspNetCore

@antoniaelek
Copy link

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();
	}
}

@deadmann
Copy link

deadmann commented Nov 5, 2019

@antoniaelek
how to use it? how to register it?

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