Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

json.net - Swagger not recognizing JsonProperty("PropertyName") as request property

I have a web api in .net core3.1 and I am using swagger.

One of my request data class is as below.

public class SNMPv1ReqData{
    [JsonProperty("snmpV1Info")]
    [MinLength(1)]
    [MaxLength(2000)]
    [Required]
    public List<SNMPv1Info> SNMPv1InfoLst { get; set; }
}

public class SNMPv1Info{
    [JsonProperty("host")]
    [Required]
    public string HostName { get; set; }

    [JsonProperty("snmpV1Setting")]
    public SNMPv1Setting SNMPv1Setting { get; set; }

    [JsonProperty("oid")]
    [MinLength(1)]
    [MaxLength(1000)]
    [Required]
    public string[] OID { get; set; }
}

In swagger request is shown as below as it is mentioned in JsonProperty("PropertyName")

 {   
   "snmpV1Info": [
     {
        "host": "string",
        "snmpV1Setting": {
          "retryCount": 0,
          "timeout": 0,
          "port": 0,
          "communityName": "string"
        },
        "oid": [
         "string"
        ]
     }   
   ] 
 }

But when I send request it is showing the below error.

{
  "title": "One or more validation errors occurred.",
  "status": 400,
  "errors": {
    "SNMPv1InfoLst": [
      "The SNMPv1InfoLst field is required."
    ]
  }
}

SNMPv1InfoLst is variable name but I want to use "snmpV1Info" in api request which is mentioned in JsonProperty("snmpV1Info") also showing "snmpV1Info" in swagger request.

Startup.cs

services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1.0.0", new Microsoft.OpenApi.Models.OpenApiInfo
                {
                    Title = "WebAPI",
                    Version = "v1.0",
                    Description = "Edge ASP.NET Core Web API",
                });
                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });

services.AddSwaggerGenNewtonsoftSupport();

Swashbuckle.AspNetCore : 5.6.3 Swashbuckle.AspNetCore.Newtonsoft : 5.6.3 .Net Core3.1

I am not able to find the cause of this problem. Can anyone help me ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...