如何在OpenAPI 2.0(Swagger 2.0)中拥有多个身体参数?

mfuanj7w  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(230)

我想在请求体中有多个参数,如下所示,但Swagger编辑器说“不允许有多个体参数”。如何在一个POST体中同时拥有对象和原语类型?
我期待criteria对象和两个原语字段(offsetlimit),如下所示:

  1. {
  2. "criteria": {
  3. "key": "CBC",
  4. "code":
  5. },
  6. "offset": 5,
  7. "limit" : 5
  8. }

OpenAPI定义:

  1. "paths": {
  2. "/user": {
  3. "post": {
  4. "tags": [
  5. "user Service"
  6. ],
  7. "summary": "Returns a list of user",
  8. "operationId": "searchUsers",
  9. "produces": [
  10. "application/json"
  11. ],
  12. "parameters": [
  13. {
  14. "in": "body",
  15. "name": "criteria",
  16. "required": true,
  17. "schema": {
  18. "$ref": "#/definitions/Criteria"
  19. }
  20. },
  21. {
  22. "name": "offset",
  23. "type": "integer",
  24. "description": "The offset for pagination.",
  25. "format": "int32",
  26. "default": 0,
  27. "minimum": 0,
  28. "required": false,
  29. "in": "body"
  30. },
  31. {
  32. "name": "limit",
  33. "type": "integer",
  34. "description": "The maximum number user",
  35. "format": "int32",
  36. "minimum": 1,
  37. "maximum": 30,
  38. "default": 30,
  39. "required": false,
  40. "in": "body"
  41. }
  42. ],
  43. "responses": {
  44. "200": {
  45. "description": "Success",
  46. "schema": {
  47. "$ref": "#/definitions/user"
  48. }
  49. },
  50. definitions:
  51. "Criteria": {
  52. "type": "object",
  53. "required": [
  54. "key"
  55. ],
  56. "properties": {
  57. "key": {
  58. "type": "string",
  59. "description": "search key.",
  60. "pattern": "[A-Za-z0-9-]+",
  61. "minLength": 1,
  62. "maxLength": 30
  63. },
  64. "code": {
  65. "type": "string",
  66. "description": "emp code filter on.",
  67. "pattern": "^[A-Za-z0-9]+",
  68. "minLength": 0,
  69. "maxLength": 9
  70. }
  71. }
  72. },

如何同时保持对象类型和基元类型?

6yoyoihd

6yoyoihd1#

你就快到了
这一部分介绍了如何在OpenAPI 2.0中定义请求主体:

  1. "parameters": [
  2. {
  3. "in": "body",
  4. "name": "criteria",
  5. "required": true,
  6. "schema": {
  7. "$ref": "#/definitions/Criteria"
  8. }
  9. },

也就是说,它被定义为一个in: body参数,但是schema必须描述 * 整个请求主体对象 *,而不是单个字段。
正确的版本是:

  1. "paths": {
  2. "/user": {
  3. "post": {
  4. ...
  5. "parameters": [
  6. {
  7. "in": "body",
  8. "name": "criteria",
  9. "required": true,
  10. "schema": {
  11. "$ref": "#/definitions/UserSearchParams"
  12. }
  13. }
  14. ],
  15. "responses": {
  16. ...
  17. }
  18. }
  19. }
  20. },
  21. "definitions": {
  22. "UserSearchParams": {
  23. "type": "object",
  24. "properties": {
  25. "criteria": {
  26. "$ref": "#/definitions/Criteria"
  27. },
  28. "offset": {
  29. "type": "integer",
  30. "description": "The offset for pagination.",
  31. "format": "int32",
  32. "default": 0,
  33. "minimum": 0
  34. },
  35. "limit": {
  36. "type": "integer",
  37. "description": "The maximum number user",
  38. "format": "int32",
  39. "minimum": 1,
  40. "maximum": 30,
  41. "default": 30
  42. }
  43. }
  44. },
  45. "Criteria": {
  46. "type": "object",
  47. "required": [
  48. "key"
  49. ],
  50. "properties": {
  51. "key": {
  52. "type": "string",
  53. "description": "search key.",
  54. "pattern": "[A-Za-z0-9-]+",
  55. "minLength": 1,
  56. "maxLength": 30
  57. },
  58. "code": {
  59. "type": "string",
  60. "description": "emp code filter on.",
  61. "pattern": "^[A-Za-z0-9]+",
  62. "minLength": 0,
  63. "maxLength": 9
  64. }
  65. }
  66. },
  67. ...
  68. }
  69. }
展开查看全部

相关问题