This article is a mirror article of machine translation, please click here to jump to the original article.

View: 47519|Reply: 4

[ASP.NET] ASP.NET pit of the default parameters of the Swagger UI in Core(8).

[Copy link]
Posted on 5/8/2021 1:27:02 PM | | | |
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

ASP.NET Core (7) In-depth analysis of the framework source code
https://www.itsvse.com/thread-9601-1-1.html

ASP.NET Core (VI) DI manually obtains the method of injecting objects
https://www.itsvse.com/thread-9595-1-1.html

ASP.NET Core (five) is based on CAP distributed transactions
https://www.itsvse.com/thread-9593-1-1.html

ASP.NET Core(4) filter unified ModelState model validation
https://www.itsvse.com/thread-9589-1-1.html

ASP.NET Core (iii) Dynamically create instances using ActivatorUtilities
https://www.itsvse.com/thread-9488-1-1.html

ASP.NET Core (2) Restart the application by code
https://www.itsvse.com/thread-9480-1-1.html

ASP.NET Core (1) uses Redis caching
https://www.itsvse.com/thread-9393-1-1.html

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)






Previous:Java uses JDBC to connect sqlite URL issue
Next:Kafka manually sets the offset offset
Posted on 9/22/2021 8:45:47 PM |
Learn to learn...
Posted on 9/22/2021 8:59:49 PM |
Learning to learn, old iron, there are so many updates, I can't keep up
Posted on 5/16/2023 11:22:31 AM |
RE: ASP.NET Core(八) 之 Swagger UI 默认参数的坑 [修改]
Advanced mode
Posted on 5/16/2023 5:20:36 PM |
Learn to learn
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com