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
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
I have below json schema and generated java class using jsonschema2pojo library
AddressSchema.json
{
"$id": "https://example.com/address.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "An address similar to http://microformats.org/wiki/h-card",
"type": "object",
"properties": {
"address": {
"type": "string"
}
}
AddressSchema.java
public class AddressSchema {
private String address;
#JsonProperty("address")
public String getAddress() {
return address;
}
#JsonProperty("address")
public void setAddress(String address) {
this.address = address;
}
}
My requirement is to generate class with different values in #JsonProperty on setter and getter like below. Is there any way to achieve this behavior?
public class AddressSchema {
private String address;
#JsonProperty("address")
public String getAddress() {
return address;
}
#JsonProperty("addr") //different value in the setter
public void setAddress(String address) {
this.address = address;
}
}
I believe you are trying to use the same class to parse a certain json with different name addr and have it return with different name address. I don't see how it might be possible without using two classes and a mapper to map the values. As humans we'd think address and addr are pretty similar and there must be mechanisms to map them while address and name are totally different I wouldn't ask for them to be mapped. But for computers it would be a difficult feature to provide. Hope you get my point.
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
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:
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.