1

I am using a swagger 4.0 with Asp.net core 2.2 Web Api. I want to tell the swagger UI on post/put method that some of the body content parameters are required. As I know that if the parameter is query parameter then it is required. Done some research https://swagger.io/docs/specification/2-0/describing-request-body/ and found that the body content can be made required

swagger document

Some of the research from other link

How to mark a property as required in Swagger, without ASP.NET model validation?

How can I tell Swashbuckle that the body content is required?

Controller code

[HttpPost]
        public async Task<IActionResult> Create([FromBody]LearningApplication model)
        {
            if (!ModelState.IsValid) return InvalidModelState(ModelState);

            // create the record,no need to provide resource auth as only admin can action this controller
            await _learningApplicationManager.CreateAsync(model, LearningApplicationValidator.OnCreateRuleset);

            return Created(url, model);
        }

Model

public class LearningApplication : BaseAuditableWithLogicalDeleteAggregate<Guid>
    {

        [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter the name of the course or professional Learning session")]
        [DataMember]
        public string CourseName { get; set; }

        [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter the professional Learning provider: ie. TGS, AGSV, etc")]
        [DataMember]
        public string Provider { get; set; }

        [DataMember]
        public string EventWebsite { get; set; }

        [DataMember]
        public string RegistrationExpenses { get; set; }

        [Required(ErrorMessage = "Please provide the Start Date & time")]
        [DataMember]
        public DateTime? StartDateTime { get; set; }
}

Current Swagger display

current swagger

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
San Jaisy
  • 15,327
  • 34
  • 171
  • 290
  • Are you comparing a JSON Schema with a JSON object? The parameter is required, and a JSON object will not tell you that – Camilo Terevinto Feb 13 '19 at 00:02
  • No, I am not comparing JSON Schema with a JSON object. Just I want to show the required attribute. – San Jaisy Feb 13 '19 at 00:38
  • That's exactly shown in a JSON Schema, not in a JSON object. An object will only have data, a schema will tell you how the data should be formatted. Since "required" is an attribute and not a value, you cannot know that from an object – Camilo Terevinto Feb 13 '19 at 00:40
  • Ok, but is it possible to generate a JSON Schema from the code in SwashBuckle? – Pieter van Kampen Jun 24 '19 at 08:33

0 Answers0