Swagger is a prescriptive and complete framework for building, describing, calling, and visualizing RESTful-style web services.
The overall goal is for the client and file system to update at the same speed as the server. The methods, parameters, and models of the file are tightly integrated into the server-side code, allowing APIs to stay in sync at all times. Swagger has never made it easier to deploy and use powerful APIs.
review
First, create a new project ASP.NET Core 3.1 and use nuget to install the Swashbuckle.AspNetCore package with the following command:
Configure the Swagger service in the startup.cs file as follows:
ConfigureServices method
Configure method
Create a new Test controller with the following code:
The Test Controller Create interface Product parameter model object is as follows:
After starting the project, access:http://localhost:18979/swagger/index.html, as shown in the figure below:
swagger.json The content is as follows:
{ "openapi": "3.0.1", "info": { "title": "My API", "version": "v1" }, "paths": { "/api/Test/Create": { "post": { "tags": [ "Test" ], "requestBody": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Product" } }, "text/json": { "schema": { "$ref": "#/components/schemas/Product" } }, "application/*+json": { "schema": { "$ref": "#/components/schemas/Product" } } } }, "responses": { "200": { "description": "Success", "content": { "text/plain": { "schema": { "type": "string" } }, "application/json": { "schema": { "type": "string" } }, "text/json": { "schema": { "type": "string" } } } } } } } }, "components": { "schemas": { "Information": { "type": "object", "properties": { "isDiscount": { "type": "boolean" }, "isVip": { "type": "boolean", "nullable": true } }, "additionalProperties": false }, "Product": { "type": "object", "properties": { "name": { "type": "string", "nullable": true }, "disabled": { "type": "boolean" }, "info": { "$ref": "#/components/schemas/Information" } }, "additionalProperties": false } } }
}
We found that both Boolean types and nullable Boolean types default to: true,In daily development, if the bool is not assigned, the default should be false, and the null type should default to null, we try to use swagger to simulate the commit, if the parameters are not noticed,Accidents are prone。 (Last week, I used swagger to simulate that a bool parameter was true by default, causing WeChat messages to be pushed to all people.)
How do I assign a Boolean type to false by default? How about null types being assigned null?
Create a new class that inherits from ISchemaFilter, and the code is as follows:
And when adding the swagger service, add the filter AddSwaggerGen as follows:
For some special bool fields, the default value needs to be set to true and can be added[DefaultValue(true)]The characteristics are as follows:
{ "name": "string", "disabled": true, "info": { "isDiscount": false, "isVip": null }
} swagger.json The file is as follows:
{ "openapi": "3.0.1", "info": { "title": "My API", "version": "v1" }, "paths": { "/api/Test/Create": { "post": { "tags": [ "Test" ], "requestBody": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Product" } }, "text/json": { "schema": { "$ref": "#/components/schemas/Product" } }, "application/*+json": { "schema": { "$ref": "#/components/schemas/Product" } } } }, "responses": { "200": { "description": "Success", "content": { "text/plain": { "schema": { "type": "string" } }, "application/json": { "schema": { "type": "string" } }, "text/json": { "schema": { "type": "string" } } } } } } } }, "components": { "schemas": { "Information": { "type": "object", "properties": { "isDiscount": { "type": "boolean", "default": false }, "isVip": { "type": "boolean", "default": null, "nullable": true } }, "additionalProperties": false }, "Product": { "type": "object", "properties": { "name": { "type": "string", "nullable": true }, "disabled": { "type": "boolean", "default": true }, "info": { "$ref": "#/components/schemas/Information" } }, "additionalProperties": false } } }
} (End)
|