使用openapi-typescript-codegen,为什么这个Swagger模式会产生一个Record< string,any>的模型?

nr9pn0ug  于 2024-01-08  发布在  TypeScript
关注(0)|答案(2)|浏览(269)

我的swagger.json包含这个模式:

  1. "schemas": {
  2. "BuildFruitBody": {
  3. "properties": {
  4. "id": {
  5. "type": "number",
  6. "format": "double"
  7. },
  8. "name": {
  9. "type": "string"
  10. }
  11. },
  12. "type": "object",
  13. "additionalProperties": true
  14. }
  15. },

字符串
然而,当我通过npx openapi-typescript-codegen -i ./swagger.json -o src/services/api -c axios生成axios客户端时,生成的模型如下所示:

  1. /* generated using openapi-typescript-codegen -- do no edit */
  2. /* istanbul ignore file */
  3. /* tslint:disable */
  4. /* eslint-disable */
  5. export type BuildFruitBody = Record<string, any>;


我在这里做错了什么吗?为什么模型没有反映模式?
完整的swagger.json如下:

  1. {
  2. "components": {
  3. "examples": {},
  4. "headers": {},
  5. "parameters": {},
  6. "requestBodies": {},
  7. "responses": {},
  8. "schemas": {
  9. "BuildFruitBody": {
  10. "properties": {
  11. "id": {
  12. "type": "number",
  13. "format": "double"
  14. },
  15. "name": {
  16. "type": "string"
  17. }
  18. },
  19. "type": "object",
  20. "additionalProperties": true
  21. }
  22. },
  23. "securitySchemes": {}
  24. },
  25. "info": {
  26. "title": "backend",
  27. "contact": {}
  28. },
  29. "openapi": "3.0.0",
  30. "paths": {
  31. "/swagger.json": {
  32. "get": {
  33. "operationId": "Get",
  34. "responses": {
  35. "200": {
  36. "description": "Ok",
  37. "content": {
  38. "application/json": {
  39. "schema": {
  40. "type": "string"
  41. }
  42. }
  43. }
  44. }
  45. },
  46. "tags": [
  47. "swagger"
  48. ],
  49. "security": [],
  50. "parameters": []
  51. }
  52. },
  53. "/fruit": {
  54. "post": {
  55. "operationId": "Get",
  56. "responses": {
  57. "200": {
  58. "description": "Ok",
  59. "content": {
  60. "application/json": {
  61. "schema": {
  62. "type": "string"
  63. }
  64. }
  65. }
  66. }
  67. },
  68. "tags": [
  69. "fruit"
  70. ],
  71. "security": [],
  72. "parameters": [],
  73. "requestBody": {
  74. "required": true,
  75. "content": {
  76. "application/json": {
  77. "schema": {
  78. "$ref": "#/components/schemas/BuildFruitBody"
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. },
  86. "servers": [
  87. {
  88. "url": "/"
  89. }
  90. ]
  91. }

b5buobof

b5buobof1#

我是不是做错了什么?
据我所知不在代码范围内。
为什么模型不反映模式?
事实上,它确实反映了图式,但也许是心智模型以不同的方式反映了图式模型,或者是尚未被很好地理解?
当在JSON模式中给定以下内容时(参见additionalProperties JSON模式核心草案-2020-12)

  1. "type": "object",
  2. "additionalProperties": true

字符串
Record<Keys, Type>实用程序类型

  1. Record<string, any>


看起来是合法的,因为在JSON文本中,属性/特性名称是JSON字符串,可以是以下七个中的任何一个:JSON对象、JSON数组、JSON字符串、JSON编号、"true""false""null"(参见json.org)。
在TypeScript中,这些属性值可以表示为any
我能给予的最好的建议是将其置于自动化测试之下,然后面对第一次测试意外失败(或者没有因为预期的原因失败)的问题,然后修复它。
模式或期望值取决于您的要求。
正如 * defraggled * 根据以下备注所确认的:
啊,是的,设置additionalProperties: false解决了这个问题。谢谢
该要求确实是没有额外的属性(可能被排除在确认之外)。
这是一个强烈的信号,表明它可能已经被排除在模式之外,additionalProperties最近已经从JSON模式验证移动到JSON模式核心(DRAFT-2020-12)。

展开查看全部
c9x0cxw0

c9x0cxw02#

您可能偶然发现了JSON Schema的预期行为,但不是您自己的预期,因为additionalProperties:true * 不会 * 限制其他属性在您的数据示例中发送。
additionalProperties: true或缺少additionalProperties定义作为约束是等价的。
这两个模式是等效的,并且将匹配Record<string, any>

  1. {
  2. "BuildFruitBody": {
  3. "properties": {
  4. "id": {
  5. "type": "number",
  6. "format": "double"
  7. },
  8. "name": {
  9. "type": "string"
  10. }
  11. },
  12. "type": "object",
  13. "additionalProperties": true
  14. }
  15. }

个字符
JSON Schema是一种基于约束的数据验证语言。它只验证提供的约束。由于您定义了additionalProperties: true,因此Record<string,any>的TypeScript模型是正确的注解,因为属性不受您使用additionalProperties:true的约束
只有当您将模式约束为已定义的属性时,才会找到预期的TypeScript定义

  1. {
  2. "BuildFruitBody": {
  3. "properties": {
  4. "id": {
  5. "type": "number",
  6. "format": "double"
  7. },
  8. "name": {
  9. "type": "string"
  10. }
  11. },
  12. "type": "object",
  13. "additionalProperties": false
  14. }
  15. }
  1. /* generated using openapi-typescript-codegen -- do no edit */
  2. /* istanbul ignore file */
  3. /* tslint:disable */
  4. /* eslint-disable */
  5. export type BuildFruitBody = {
  6. id?: number;
  7. name?: string;
  8. };

的字符串

展开查看全部

相关问题