I use version 5.0.0.
I added a class that I want the user to provide in the body. Here is the class:
using System;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using System.Text.Json;
namespace API.Models
{
[DataContract]
public partial class LoginData : IEquatable<LoginData>
{
[Required]
[DataMember(Name = "userId")]
public string UserId;
[Required]
[DataMember(Name = "password")]
public string Password;
public string ToJson()
{
return JsonSerializer.Serialize(this, new JsonSerializerOptions() {WriteIndented = true});
}
public bool Equals(LoginData other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return UserId == other.UserId && Password == other.Password;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((LoginData) obj);
}
public override int GetHashCode()
{
return HashCode.Combine(UserId, Password);
}
}
}
Here is the relevant part of my Controller:
using System.ComponentModel.DataAnnotations;
using API.Attributes;
using API.Models;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
namespace API.Controller
{
public partial class DefaultApiController
{
/// <summary>
/// </summary>
/// <param name="loginData"></param>
/// <response code="200">Everything went fine.</response>
[HttpPut]
[Route("/v1/auth/")]
[ValidateModelState]
[SwaggerOperation("Auth")]
public IActionResult Authenticate([FromBody] [Required] LoginData loginData)
{
var bla = loginData.UserId;
return new JsonResult(loginData);
}
}
}
Somehow this results in an empty "LoginData" object:
"components": {
"schemas": {
"LoginData": {
"type": "object",
"nullable": true
},
So all properties are missing. Any idea why that is? :thinking:
It looks like you're using the new serializer - System.Text.Json. However, this serializer _does not_ support fields. As you only have fields in your DTO (as opposed to properties) and Swashbuckle honors serializer behavior by design, a DTO description with empty properties is an accurate reflection.
Have you confirmed that your endpoint actually works as expected, independently of Swashbuckle? If I'm correct that you are in fact using the System.Text.Json serializer that ships as the default with ASP.NET Core 3.0 and beyond, I would expect your action to return an empty JSON object.
However, if my assumption is wrong and you're actually using the Newtonsoft serializer, which _does_ support fields, then you'll need to configure Swashbuckle to honor Newtonsoft behavior rather than STJ behavior as described here
Oh maaan, I completely missed the { get; set; } comparing the other models bit by bit, but missing this detail. :smile: Now it works fine.
Yes, I use the new System.Text.Json serializer. :+1:
Most helpful comment
It looks like you're using the new serializer -
System.Text.Json. However, this serializer _does not_ support fields. As you only have fields in your DTO (as opposed to properties) and Swashbuckle honors serializer behavior by design, a DTO description with empty properties is an accurate reflection.Have you confirmed that your endpoint actually works as expected, independently of Swashbuckle? If I'm correct that you are in fact using the
System.Text.Jsonserializer that ships as the default with ASP.NET Core 3.0 and beyond, I would expect your action to return an empty JSON object.However, if my assumption is wrong and you're actually using the
Newtonsoftserializer, which _does_ support fields, then you'll need to configure Swashbuckle to honor Newtonsoft behavior rather than STJ behavior as described here