Does Swashbuckle and Swagger allow specifying maximum string lengths? [duplicate] - asp.net-web-api2

I am using ASP.NET and Swagger that exposes a complex type that accepts a POST. It has a number of string fields that have different restricted lengths. How can I reflect that in the Swagger UI?

You can annotate the properties with the StringLengthAttribute from System.ComponentModel.DataAnnotations.
For instance:
[StringLength(10)]
public String Name {get;set;}
will become:
"name": {
"minLength": 0,
"maxLength": 10,
"type": "string"
}
And this:
[StringLength(10, MinimumLength = 5)]
public String Name {get;set;}
becomes:
"name": {
"minLength": 5,
"maxLength": 10,
"type": "string"
}
Besides StringLength Swashbuckle also supports the Range and RegularExpression attributes.
Update
MaxLength does not work. StringLength does. However, discovering this information in Swagger UI is a bit clumsy. You have to navigate to the Model of your object and then hover over the property:

Related

What is an efficient way to generate vb classes from json schemas?

Is there any way to generate vb classes from jsonschema files like we can generate classes from wsdls and xsds using wsdl.exe in one go.
I don't want to use Edit > Paste special > paste JSON as class feature of Visual Studio because I tried for one file and it did not give me the result I am expecting and also there are about 15 schema files so want a generic way.
On using Edit > Paste special > paste JSON as class feature of Visual Studio,
The schema have is:
{
"title": "MyObject",
"type": "object",
"properties": {
"description": {
"type": "string"
},
"name": {
"type": "string"
},
"id": {
"type": "string"
}
},
"required": [ "id", "description", "name" ]
}
The generated classes:
Public Class Rootobject
Public Property title As String
Public Property type As String
Public Property properties As Properties
Public Property required() As String
End Class
Public Class Properties
Public Property description As Description
Public Property name As Name
Public Property id As Id
End Class
Public Class Description
Public Property type As String
End Class
Public Class Name
Public Property type As String
End Class
Public Class Id
Public Property type As String
End Class

Asp.net Core Web Api Override Model Validation

I am creating an asp.net core web api application.
Where I am try to validate my models using fluent validation.
this is my model and validator.
public class Data
{
public string Name { get; set; }
public int Age { get; set; }
}
public class DataValidator : AbstractValidator<Data>
{
public DataValidator()
{
RuleFor(x => x.Name)
.NotEmpty()
.MaximumLength(5);
RuleFor(x => x.Age)
.LessThan(80);
}
}
Everything works fine.
Fluent validation returns all the validations together except following case.
When my request contains following JSON, Fluent Validation doesn't get hit.
Asp.net core model validation is take place.
in that case I am getting single validation error.
{
"name": 123,
"Age" : 100
}
I got following validation message.
The JSON value could not be converted to System.String. Path
How to override above default message?
Is there any way to handle above validation in Fluent Validation?
I want both 'name' and 'age' validation messages together.
Let's look at your binding model:
public class Data
{
public string Name { get; set; }
public int Age { get; set; }
}
Here, Name is a string while Age is an int.
Your validator is just fine, otherwise you should get a compilation error when you build the app.
Now, let's look at the JSON:
{
"name": 123,
"Age" : 100
}
Instead of using name, you should use Name. Plus, the value of Name should be a string, i.e. "123" instead of 123. i.e.
{
"Name": "123",
"Age": 100
}
After that, you should be able to get the expected validation errors.

Swashbuckle wraps number examples with double quotes from VS XML comments

I have a class representing a body for an endpoint, in that class is an Amount property, it looks like this:
/// <summary>
/// The transaction amount; it must be greater than 0
/// </summary>
/// <example>1.23</example>
[JsonProperty("Amount")]
[Required]
public decimal Amount { get; set; }
The problem I'm having is that when Swashbuckle generates the JSON for this Request body example, it ends up wrapping the Amount value with double quotes like this:
{
"Amount": "1.23"
}
I want it to look like a number, like this:
{
"Amount": 1.23
}
If I give no XML example tag, the Request Body example generated in the SwaggerUI is just 0.0 with no double quotes, like this:
{
"Amount": 0.0
}
The portion of the XML comments that is generated from this example doesn't have quotes:
<member name="P:MyAPI.Models.SaleRequest.Amount">
<summary>
The transaction amount; it must be greater than 0
</summary>
<example>1.23</example>
</member>
But the JSON generated from Swashbuckle does:
"SaleRequest": {
"required": [
"Amount"
],
"type": "object",
"properties": {
"Amount": {
"format": "decimal",
"description": "The transaction amount; it must be greater than 0",
"type": "number",
"example": "1.23"
}
}
}
Is there a way I can tell Swashbuckle not to wrap this XML example tag for a number property with double quotes? I've been searching for a while and can't find anything. I've tried c.MapType<decimal>(() => new Schema { Type = "number", Format = "decimal" }); from another post when configuring swagger but that didn't change anything. I didn't see any attributes I could apply to the property in the source code either. I'm using Swashbuckle 4.0.1

Data annotations in Swagger

I am using ASP.NET and Swagger that exposes a complex type that accepts a POST. It has a number of string fields that have different restricted lengths. How can I reflect that in the Swagger UI?
You can annotate the properties with the StringLengthAttribute from System.ComponentModel.DataAnnotations.
For instance:
[StringLength(10)]
public String Name {get;set;}
will become:
"name": {
"minLength": 0,
"maxLength": 10,
"type": "string"
}
And this:
[StringLength(10, MinimumLength = 5)]
public String Name {get;set;}
becomes:
"name": {
"minLength": 5,
"maxLength": 10,
"type": "string"
}
Besides StringLength Swashbuckle also supports the Range and RegularExpression attributes.
Update
MaxLength does not work. StringLength does. However, discovering this information in Swagger UI is a bit clumsy. You have to navigate to the Model of your object and then hover over the property:

How can I pass a single FromBody string param to a WEB API method?

Here is my Web API method signature:
[HttpPut]
public TraceItemNoBreadCrumbs UpdateTraceItem(int traceId, string itemType, string itemId, [FromBody]string notes)
I use Postman to test this and in the input body if I just pass:
"My Notes"
and then inspect the incoming param it is correct.
But the front end post needs to pass in like:
data:
{ "notes": "My Notes" }
But when I pass it in, now notes = "notes", not "My Notes" (the first string it sees apparently).
I only have problems with a single string.
In another method I pass in [FromBody] myListObject
like
[
{ "prop1": "value", "prop2": "value" }
{ "prop1": "value", "prop2": "value" }
]
and it works perfectly.
I just need to fix this for our front end guys.
Instead of taking in a string, I had to model the string inside of an object.
So it looks like:
[HttpPut]
public TraceItemNoBreadCrumbs UpdateTraceItem(int traceId, string itemType, string itemId, [FromBody]TraceItemNote notes)
Where TraceItemNote looks like:
public class TraceItemNote
{
public string Notes { get; set; }
}
I think because we are sending up JSON objects, they have to have a C# object counterpart.
Since its coming from the body, you don't need to format it as an json object. just pass "my notes" in the body by itself.