@tsed/schema 8.0.0 → 8.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (291) hide show
  1. package/package.json +11 -10
  2. package/src/components/async-api/channelsMapper.ts +81 -0
  3. package/src/components/async-api/generate.ts +24 -0
  4. package/src/components/async-api/messageMapper.ts +62 -0
  5. package/src/components/async-api/payloadMapper.ts +61 -0
  6. package/src/components/async-api/responseMapper.ts +70 -0
  7. package/src/components/default/anyMapper.spec.ts +7 -0
  8. package/src/components/default/anyMapper.ts +33 -0
  9. package/src/components/default/classMapper.ts +55 -0
  10. package/src/components/default/discriminatorMappingMapper.ts +36 -0
  11. package/src/components/default/enumsMapper.spec.ts +64 -0
  12. package/src/components/default/enumsMapper.ts +17 -0
  13. package/src/components/default/genericsMapper.ts +70 -0
  14. package/src/components/default/inheritedClassMapper.ts +24 -0
  15. package/src/components/default/itemMapper.ts +8 -0
  16. package/src/components/default/lazyRefMapper.ts +21 -0
  17. package/src/components/default/mapMapper.ts +25 -0
  18. package/src/components/default/nullableMapper.ts +55 -0
  19. package/src/components/default/objectMapper.ts +36 -0
  20. package/src/components/default/ofMapper.ts +16 -0
  21. package/src/components/default/propertiesMapper.ts +25 -0
  22. package/src/components/default/requiredMapper.ts +69 -0
  23. package/src/components/default/schemaMapper.ts +151 -0
  24. package/src/components/index.ts +36 -0
  25. package/src/components/open-spec/discriminatorMappingMapper.ts +7 -0
  26. package/src/components/open-spec/enumsMapper.ts +20 -0
  27. package/src/components/open-spec/generate.ts +29 -0
  28. package/src/components/open-spec/nullableMapper.ts +51 -0
  29. package/src/components/open-spec/operationInFilesMapper.ts +32 -0
  30. package/src/components/open-spec/operationInParameterMapper.ts +56 -0
  31. package/src/components/open-spec/operationInParametersMapper.ts +9 -0
  32. package/src/components/open-spec/operationInQueryMapper.ts +74 -0
  33. package/src/components/open-spec/operationMapper.ts +65 -0
  34. package/src/components/open-spec/operationMediaMapper.ts +11 -0
  35. package/src/components/open-spec/operationRequestBodyMapper.ts +82 -0
  36. package/src/components/open-spec/operationResponseMapper.ts +26 -0
  37. package/src/components/open-spec/pathsMapper.ts +132 -0
  38. package/src/constants/OperationVerbs.ts +29 -0
  39. package/src/constants/httpStatusMessages.ts +21 -0
  40. package/src/constants/jsonSchemaProperties.ts +17 -0
  41. package/src/decorators/class/children.ts +22 -0
  42. package/src/decorators/class/discriminatorValue.ts +53 -0
  43. package/src/decorators/collections/collectionContains.ts +25 -0
  44. package/src/decorators/collections/collectionOf.spec.ts +327 -0
  45. package/src/decorators/collections/collectionOf.ts +182 -0
  46. package/src/decorators/collections/maxItems.spec.ts +40 -0
  47. package/src/decorators/collections/maxItems.ts +59 -0
  48. package/src/decorators/collections/maxProperties.spec.ts +109 -0
  49. package/src/decorators/collections/maxProperties.ts +82 -0
  50. package/src/decorators/collections/minItems.spec.ts +40 -0
  51. package/src/decorators/collections/minItems.ts +63 -0
  52. package/src/decorators/collections/minProperties.spec.ts +109 -0
  53. package/src/decorators/collections/minProperties.ts +81 -0
  54. package/src/decorators/collections/uniqueItems.spec.ts +28 -0
  55. package/src/decorators/collections/uniqueItems.ts +59 -0
  56. package/src/decorators/common/__snapshots__/groups.spec.ts.snap +321 -0
  57. package/src/decorators/common/additionalProperties.spec.ts +133 -0
  58. package/src/decorators/common/additionalProperties.ts +17 -0
  59. package/src/decorators/common/allOf.spec.ts +276 -0
  60. package/src/decorators/common/allOf.ts +19 -0
  61. package/src/decorators/common/allow.spec.ts +130 -0
  62. package/src/decorators/common/allow.ts +50 -0
  63. package/src/decorators/common/any.spec.ts +109 -0
  64. package/src/decorators/common/any.ts +45 -0
  65. package/src/decorators/common/anyOf.spec.ts +270 -0
  66. package/src/decorators/common/anyOf.ts +23 -0
  67. package/src/decorators/common/const.spec.ts +98 -0
  68. package/src/decorators/common/const.ts +68 -0
  69. package/src/decorators/common/customKey.spec.ts +105 -0
  70. package/src/decorators/common/customKey.ts +39 -0
  71. package/src/decorators/common/default.spec.ts +25 -0
  72. package/src/decorators/common/default.ts +47 -0
  73. package/src/decorators/common/defaultMsg.spec.ts +22 -0
  74. package/src/decorators/common/defaultMsg.ts +38 -0
  75. package/src/decorators/common/description.spec.ts +166 -0
  76. package/src/decorators/common/description.ts +77 -0
  77. package/src/decorators/common/discriminatorKey.ts +14 -0
  78. package/src/decorators/common/enum.spec.ts +305 -0
  79. package/src/decorators/common/enum.ts +111 -0
  80. package/src/decorators/common/errorMsg.spec.ts +57 -0
  81. package/src/decorators/common/errorMsg.ts +14 -0
  82. package/src/decorators/common/example.spec.ts +93 -0
  83. package/src/decorators/common/example.ts +18 -0
  84. package/src/decorators/common/examples.spec.ts +101 -0
  85. package/src/decorators/common/examples.ts +20 -0
  86. package/src/decorators/common/exclusiveMaximum.spec.ts +47 -0
  87. package/src/decorators/common/exclusiveMaximum.ts +75 -0
  88. package/src/decorators/common/exclusiveMinimum.spec.ts +47 -0
  89. package/src/decorators/common/exclusiveMinimum.ts +75 -0
  90. package/src/decorators/common/format.spec.ts +149 -0
  91. package/src/decorators/common/format.ts +486 -0
  92. package/src/decorators/common/forwardGroups.spec.ts +270 -0
  93. package/src/decorators/common/forwardGroups.ts +16 -0
  94. package/src/decorators/common/groups.spec.ts +968 -0
  95. package/src/decorators/common/groups.ts +66 -0
  96. package/src/decorators/common/hidden.spec.ts +52 -0
  97. package/src/decorators/common/hidden.ts +36 -0
  98. package/src/decorators/common/ignore.spec.ts +88 -0
  99. package/src/decorators/common/ignore.ts +66 -0
  100. package/src/decorators/common/integer.spec.ts +110 -0
  101. package/src/decorators/common/integer.ts +69 -0
  102. package/src/decorators/common/jsonEntityFn.spec.ts +16 -0
  103. package/src/decorators/common/jsonEntityFn.ts +33 -0
  104. package/src/decorators/common/maxLength.spec.ts +61 -0
  105. package/src/decorators/common/maxLength.ts +88 -0
  106. package/src/decorators/common/maximum.spec.ts +73 -0
  107. package/src/decorators/common/maximum.ts +146 -0
  108. package/src/decorators/common/minLength.spec.ts +186 -0
  109. package/src/decorators/common/minLength.ts +89 -0
  110. package/src/decorators/common/minimum.spec.ts +74 -0
  111. package/src/decorators/common/minimum.ts +145 -0
  112. package/src/decorators/common/multipleOf.spec.ts +33 -0
  113. package/src/decorators/common/multipleOf.ts +80 -0
  114. package/src/decorators/common/name.spec.ts +56 -0
  115. package/src/decorators/common/name.ts +50 -0
  116. package/src/decorators/common/nullable.spec.ts +818 -0
  117. package/src/decorators/common/nullable.ts +32 -0
  118. package/src/decorators/common/oneOf.spec.ts +301 -0
  119. package/src/decorators/common/oneOf.ts +24 -0
  120. package/src/decorators/common/optional.spec.ts +43 -0
  121. package/src/decorators/common/optional.ts +26 -0
  122. package/src/decorators/common/pattern.spec.ts +27 -0
  123. package/src/decorators/common/pattern.ts +102 -0
  124. package/src/decorators/common/property.ts +24 -0
  125. package/src/decorators/common/readOnly.spec.ts +135 -0
  126. package/src/decorators/common/readOnly.ts +17 -0
  127. package/src/decorators/common/recordOf.spec.ts +41 -0
  128. package/src/decorators/common/recordOf.ts +59 -0
  129. package/src/decorators/common/required.spec.ts +188 -0
  130. package/src/decorators/common/required.ts +50 -0
  131. package/src/decorators/common/requiredGroups.spec.ts +127 -0
  132. package/src/decorators/common/requiredGroups.ts +23 -0
  133. package/src/decorators/common/schema.spec.ts +23 -0
  134. package/src/decorators/common/schema.ts +64 -0
  135. package/src/decorators/common/title.spec.ts +20 -0
  136. package/src/decorators/common/title.ts +45 -0
  137. package/src/decorators/common/typeError.spec.ts +22 -0
  138. package/src/decorators/common/typeError.ts +15 -0
  139. package/src/decorators/common/writeOnly.spec.ts +105 -0
  140. package/src/decorators/common/writeOnly.ts +17 -0
  141. package/src/decorators/config/jsonEntityComponent.ts +16 -0
  142. package/src/decorators/generics/genericOf.spec.ts +246 -0
  143. package/src/decorators/generics/genericOf.ts +112 -0
  144. package/src/decorators/generics/generics.ts +22 -0
  145. package/src/decorators/operations/acceptMime.spec.ts +39 -0
  146. package/src/decorators/operations/acceptMime.ts +25 -0
  147. package/src/decorators/operations/consumes.spec.ts +89 -0
  148. package/src/decorators/operations/consumes.ts +37 -0
  149. package/src/decorators/operations/contentType.spec.ts +40 -0
  150. package/src/decorators/operations/contentType.ts +24 -0
  151. package/src/decorators/operations/deprecated.spec.ts +223 -0
  152. package/src/decorators/operations/deprecated.ts +49 -0
  153. package/src/decorators/operations/header.spec.ts +214 -0
  154. package/src/decorators/operations/header.ts +62 -0
  155. package/src/decorators/operations/in.spec.ts +321 -0
  156. package/src/decorators/operations/in.ts +141 -0
  157. package/src/decorators/operations/inFile.spec.ts +209 -0
  158. package/src/decorators/operations/inFile.ts +55 -0
  159. package/src/decorators/operations/location.spec.ts +51 -0
  160. package/src/decorators/operations/location.ts +26 -0
  161. package/src/decorators/operations/operation.ts +119 -0
  162. package/src/decorators/operations/operationId.spec.ts +43 -0
  163. package/src/decorators/operations/operationId.ts +22 -0
  164. package/src/decorators/operations/operationPath.spec.ts +42 -0
  165. package/src/decorators/operations/operationPath.ts +33 -0
  166. package/src/decorators/operations/partial.spec.ts +249 -0
  167. package/src/decorators/operations/partial.ts +14 -0
  168. package/src/decorators/operations/path.spec.ts +47 -0
  169. package/src/decorators/operations/path.ts +27 -0
  170. package/src/decorators/operations/produces.spec.ts +89 -0
  171. package/src/decorators/operations/produces.ts +44 -0
  172. package/src/decorators/operations/publish.spec.ts +26 -0
  173. package/src/decorators/operations/publish.ts +6 -0
  174. package/src/decorators/operations/redirect.spec.ts +146 -0
  175. package/src/decorators/operations/redirect.ts +82 -0
  176. package/src/decorators/operations/returns.spec.ts +1393 -0
  177. package/src/decorators/operations/returns.ts +662 -0
  178. package/src/decorators/operations/route.spec.ts +211 -0
  179. package/src/decorators/operations/route.ts +154 -0
  180. package/src/decorators/operations/security.spec.ts +162 -0
  181. package/src/decorators/operations/security.ts +83 -0
  182. package/src/decorators/operations/status.spec.ts +582 -0
  183. package/src/decorators/operations/status.ts +130 -0
  184. package/src/decorators/operations/subscribe.spec.ts +32 -0
  185. package/src/decorators/operations/subscribe.ts +6 -0
  186. package/src/decorators/operations/summary.spec.ts +45 -0
  187. package/src/decorators/operations/summary.ts +32 -0
  188. package/src/decorators/operations/tags.spec.ts +113 -0
  189. package/src/decorators/operations/tags.ts +53 -0
  190. package/src/decorators/operations/view.spec.ts +16 -0
  191. package/src/decorators/operations/view.ts +27 -0
  192. package/src/domain/DecoratorContext.ts +116 -0
  193. package/src/domain/JsonAliasMap.ts +19 -0
  194. package/src/domain/JsonClassStore.ts +35 -0
  195. package/src/domain/JsonDiscriminator.spec.ts +65 -0
  196. package/src/domain/JsonDiscriminator.ts +52 -0
  197. package/src/domain/JsonEntityStore.spec.ts +277 -0
  198. package/src/domain/JsonEntityStore.ts +284 -0
  199. package/src/domain/JsonFormatTypes.ts +17 -0
  200. package/src/domain/JsonLazyRef.ts +21 -0
  201. package/src/domain/JsonMap.spec.ts +15 -0
  202. package/src/domain/JsonMap.ts +33 -0
  203. package/src/domain/JsonMedia.ts +24 -0
  204. package/src/domain/JsonMethodStore.spec.ts +301 -0
  205. package/src/domain/JsonMethodStore.ts +212 -0
  206. package/src/domain/JsonOperation.spec.ts +126 -0
  207. package/src/domain/JsonOperation.ts +219 -0
  208. package/src/domain/JsonOperationPathsMap.ts +16 -0
  209. package/src/domain/JsonOperationRoute.spec.ts +109 -0
  210. package/src/domain/JsonOperationRoute.ts +75 -0
  211. package/src/domain/JsonParameter.ts +70 -0
  212. package/src/domain/JsonParameterStore.spec.ts +212 -0
  213. package/src/domain/JsonParameterStore.ts +138 -0
  214. package/src/domain/JsonParameterTypes.spec.ts +12 -0
  215. package/src/domain/JsonParameterTypes.ts +20 -0
  216. package/src/domain/JsonPropertyStore.spec.ts +177 -0
  217. package/src/domain/JsonPropertyStore.ts +92 -0
  218. package/src/domain/JsonRequestBody.ts +47 -0
  219. package/src/domain/JsonResponse.spec.ts +33 -0
  220. package/src/domain/JsonResponse.ts +64 -0
  221. package/src/domain/JsonSchema.spec.ts +1470 -0
  222. package/src/domain/JsonSchema.ts +1049 -0
  223. package/src/domain/SpecTypes.ts +6 -0
  224. package/src/hooks/alterIgnore.ts +25 -0
  225. package/src/hooks/alterOneOf.ts +67 -0
  226. package/src/hooks/alterRequiredGroups.ts +12 -0
  227. package/src/index.ts +185 -0
  228. package/src/interfaces/IgnoreCallback.ts +5 -0
  229. package/src/interfaces/JsonHookContext.ts +18 -0
  230. package/src/interfaces/JsonOpenSpec.ts +7 -0
  231. package/src/interfaces/JsonSchemaOptions.ts +35 -0
  232. package/src/registries/JsonSchemaMapperContainer.ts +52 -0
  233. package/src/registries/enumRegistries.ts +3 -0
  234. package/src/utils/__snapshots__/generateSpec.spec.ts.snap +109 -0
  235. package/src/utils/buildPath.ts +8 -0
  236. package/src/utils/concatParameters.ts +12 -0
  237. package/src/utils/concatPath.spec.ts +23 -0
  238. package/src/utils/concatPath.ts +24 -0
  239. package/src/utils/defineStatusModel.ts +11 -0
  240. package/src/utils/from.spec.ts +243 -0
  241. package/src/utils/from.ts +281 -0
  242. package/src/utils/generateSpec.spec.ts +1262 -0
  243. package/src/utils/generateSpec.ts +54 -0
  244. package/src/utils/generics.ts +83 -0
  245. package/src/utils/getComputedType.spec.ts +45 -0
  246. package/src/utils/getComputedType.ts +39 -0
  247. package/src/utils/getInheritedStores.spec.ts +47 -0
  248. package/src/utils/getInheritedStores.ts +23 -0
  249. package/src/utils/getJsonEntityStore.ts +36 -0
  250. package/src/utils/getJsonPathParameters.spec.ts +126 -0
  251. package/src/utils/getJsonPathParameters.ts +97 -0
  252. package/src/utils/getJsonSchema.spec.ts +695 -0
  253. package/src/utils/getJsonSchema.ts +69 -0
  254. package/src/utils/getJsonType.spec.ts +28 -0
  255. package/src/utils/getJsonType.ts +46 -0
  256. package/src/utils/getOperationsRoutes.spec.ts +284 -0
  257. package/src/utils/getOperationsRoutes.ts +56 -0
  258. package/src/utils/getOperationsStore.spec.ts +47 -0
  259. package/src/utils/getOperationsStores.ts +37 -0
  260. package/src/utils/getPropertiesStores.spec.ts +138 -0
  261. package/src/utils/getPropertiesStores.ts +66 -0
  262. package/src/utils/getSpec.spec.ts +610 -0
  263. package/src/utils/getSpec.ts +117 -0
  264. package/src/utils/getSpecType.spec.ts +16 -0
  265. package/src/utils/getSpecType.ts +19 -0
  266. package/src/utils/isSuccessStatus.spec.ts +22 -0
  267. package/src/utils/isSuccessStatus.ts +2 -0
  268. package/src/utils/mapHeaders.spec.ts +57 -0
  269. package/src/utils/mapHeaders.ts +33 -0
  270. package/src/utils/mapOpenSpec.ts +44 -0
  271. package/src/utils/mapOpenSpec2.ts +50 -0
  272. package/src/utils/mapOpenSpec3.ts +25 -0
  273. package/src/utils/mapOpenSpecInfo.ts +19 -0
  274. package/src/utils/mapOperationOptions.ts +34 -0
  275. package/src/utils/matchGroups.spec.ts +85 -0
  276. package/src/utils/matchGroups.ts +64 -0
  277. package/src/utils/mergeSpec.spec.ts +533 -0
  278. package/src/utils/mergeSpec.ts +54 -0
  279. package/src/utils/operationIdFormatter.spec.ts +11 -0
  280. package/src/utils/operationIdFormatter.ts +49 -0
  281. package/src/utils/ref.ts +65 -0
  282. package/src/utils/removeHiddenOperation.ts +5 -0
  283. package/src/utils/serializeEnumValues.ts +27 -0
  284. package/src/utils/somethingOf.ts +11 -0
  285. package/src/utils/toJsonMapCollection.spec.ts +18 -0
  286. package/src/utils/toJsonMapCollection.ts +12 -0
  287. package/src/utils/toJsonRegex.ts +7 -0
  288. package/src/utils/transformToOS2.spec.ts +177 -0
  289. package/src/utils/transformToOS2.ts +607 -0
  290. package/src/utils/withErrorMsg.spec.ts +16 -0
  291. package/src/utils/withErrorMsg.ts +56 -0
@@ -0,0 +1,582 @@
1
+ import "@tsed/platform-exceptions";
2
+
3
+ import {SpecTypes} from "../../domain/SpecTypes.js";
4
+ import {getSpec} from "../../utils/getSpec.js";
5
+ import {CollectionOf} from "../collections/collectionOf.js";
6
+ import {Property} from "../common/property.js";
7
+ import {Generics} from "../generics/generics.js";
8
+ import {OperationPath} from "./operationPath.js";
9
+ import {Status} from "./status.js";
10
+
11
+ describe("@Status", () => {
12
+ it("should declare a return type", () => {
13
+ // WHEN
14
+ class Controller {
15
+ @OperationPath("POST", "/")
16
+ @(Status(200, String).Description("description"))
17
+ method() {}
18
+ }
19
+
20
+ // THEN
21
+ const spec = getSpec(Controller);
22
+
23
+ expect(spec).toEqual({
24
+ paths: {
25
+ "/": {
26
+ post: {
27
+ operationId: "controllerMethod",
28
+ parameters: [],
29
+ responses: {
30
+ "200": {
31
+ content: {
32
+ "*/*": {
33
+ schema: {
34
+ type: "string"
35
+ }
36
+ }
37
+ },
38
+ description: "description"
39
+ }
40
+ },
41
+ tags: ["Controller"]
42
+ }
43
+ }
44
+ },
45
+ tags: [
46
+ {
47
+ name: "Controller"
48
+ }
49
+ ]
50
+ });
51
+ });
52
+ it("should declare a return type (Status().Type())", () => {
53
+ // WHEN
54
+ class Controller {
55
+ @OperationPath("POST", "/")
56
+ @(Status(200).Type(String).Description("description"))
57
+ method() {}
58
+ }
59
+
60
+ // THEN
61
+ const spec = getSpec(Controller);
62
+
63
+ expect(spec).toEqual({
64
+ paths: {
65
+ "/": {
66
+ post: {
67
+ operationId: "controllerMethod",
68
+ parameters: [],
69
+ responses: {
70
+ "200": {
71
+ content: {
72
+ "*/*": {
73
+ schema: {
74
+ type: "string"
75
+ }
76
+ }
77
+ },
78
+ description: "description"
79
+ }
80
+ },
81
+ tags: ["Controller"]
82
+ }
83
+ }
84
+ },
85
+ tags: [
86
+ {
87
+ name: "Controller"
88
+ }
89
+ ]
90
+ });
91
+ });
92
+ it("should declare a return type with headers", () => {
93
+ // WHEN
94
+ class Controller {
95
+ @OperationPath("POST", "/")
96
+ @(Status(200, String)
97
+ .Description("description")
98
+ .Header("x-token", "token")
99
+ .Header("x-header", {
100
+ value: ""
101
+ })
102
+ .Examples({test: "Examples"})
103
+ .Schema({
104
+ minLength: 3
105
+ }))
106
+ method() {}
107
+ }
108
+
109
+ // THEN
110
+ const spec = getSpec(Controller);
111
+
112
+ expect(spec).toEqual({
113
+ paths: {
114
+ "/": {
115
+ post: {
116
+ operationId: "controllerMethod",
117
+ parameters: [],
118
+ responses: {
119
+ "200": {
120
+ content: {
121
+ "*/*": {
122
+ examples: {
123
+ test: "Examples"
124
+ },
125
+ schema: {
126
+ minLength: 3,
127
+ type: "string"
128
+ }
129
+ }
130
+ },
131
+ description: "description",
132
+ headers: {
133
+ "x-header": {
134
+ example: "",
135
+ schema: {
136
+ type: "string"
137
+ }
138
+ },
139
+ "x-token": {
140
+ example: "token",
141
+ schema: {
142
+ type: "string"
143
+ }
144
+ }
145
+ }
146
+ }
147
+ },
148
+ tags: ["Controller"]
149
+ }
150
+ }
151
+ },
152
+ tags: [
153
+ {
154
+ name: "Controller"
155
+ }
156
+ ]
157
+ });
158
+ });
159
+ it("should declare a return type with content-type", () => {
160
+ // WHEN
161
+ class Controller {
162
+ @OperationPath("POST", "/")
163
+ @(Status(200, String).Description("description").ContentType("text/html").Examples("Examples"))
164
+ method() {}
165
+ }
166
+
167
+ // THEN
168
+ const spec = getSpec(Controller, {specType: SpecTypes.OPENAPI});
169
+
170
+ expect(spec).toEqual({
171
+ tags: [
172
+ {
173
+ name: "Controller"
174
+ }
175
+ ],
176
+ paths: {
177
+ "/": {
178
+ post: {
179
+ operationId: "controllerMethod",
180
+ parameters: [],
181
+ responses: {
182
+ "200": {
183
+ content: {
184
+ "text/html": {
185
+ schema: {
186
+ type: "string"
187
+ },
188
+ examples: ["Examples"]
189
+ }
190
+ },
191
+ description: "description"
192
+ }
193
+ },
194
+ tags: ["Controller"]
195
+ }
196
+ }
197
+ }
198
+ });
199
+ });
200
+ it("should declare error response", () => {
201
+ // WHEN
202
+ class Controller {
203
+ @OperationPath("POST", "/")
204
+ @(Status(400).Description("Bad request"))
205
+ @Status(401)
206
+ @(Status(200).Description("Success"))
207
+ method() {}
208
+ }
209
+
210
+ // THEN
211
+ const spec = getSpec(Controller);
212
+
213
+ expect(spec).toEqual({
214
+ components: {
215
+ schemas: {
216
+ BadRequest: {
217
+ properties: {
218
+ errors: {
219
+ description: "A list of related errors",
220
+ items: {
221
+ $ref: "#/components/schemas/GenericError"
222
+ },
223
+ type: "array"
224
+ },
225
+ message: {
226
+ description: "An error message",
227
+ minLength: 1,
228
+ type: "string"
229
+ },
230
+ name: {
231
+ default: "BAD_REQUEST",
232
+ description: "The error name",
233
+ example: "BAD_REQUEST",
234
+ minLength: 1,
235
+ type: "string"
236
+ },
237
+ stack: {
238
+ description: "The stack trace (only in development mode)",
239
+ type: "string"
240
+ },
241
+ status: {
242
+ default: 400,
243
+ description: "The status code of the exception",
244
+ example: 400,
245
+ type: "number"
246
+ }
247
+ },
248
+ required: ["name", "message", "status"],
249
+ type: "object"
250
+ },
251
+ GenericError: {
252
+ additionalProperties: true,
253
+ properties: {
254
+ message: {
255
+ description: "An error message",
256
+ minLength: 1,
257
+ type: "string"
258
+ },
259
+ name: {
260
+ description: "The error name",
261
+ minLength: 1,
262
+ type: "string"
263
+ }
264
+ },
265
+ required: ["name", "message"],
266
+ type: "object"
267
+ },
268
+ Unauthorized: {
269
+ properties: {
270
+ errors: {
271
+ description: "A list of related errors",
272
+ items: {
273
+ $ref: "#/components/schemas/GenericError"
274
+ },
275
+ type: "array"
276
+ },
277
+ message: {
278
+ description: "An error message",
279
+ minLength: 1,
280
+ type: "string"
281
+ },
282
+ name: {
283
+ default: "UNAUTHORIZED",
284
+ description: "The error name",
285
+ example: "UNAUTHORIZED",
286
+ minLength: 1,
287
+ type: "string"
288
+ },
289
+ stack: {
290
+ description: "The stack trace (only in development mode)",
291
+ type: "string"
292
+ },
293
+ status: {
294
+ default: 401,
295
+ description: "The status code of the exception",
296
+ example: 401,
297
+ type: "number"
298
+ }
299
+ },
300
+ required: ["name", "message", "status"],
301
+ type: "object"
302
+ }
303
+ }
304
+ },
305
+ paths: {
306
+ "/": {
307
+ post: {
308
+ operationId: "controllerMethod",
309
+ parameters: [],
310
+ responses: {
311
+ "200": {
312
+ content: {
313
+ "*/*": {
314
+ schema: {
315
+ type: "object"
316
+ }
317
+ }
318
+ },
319
+ description: "Success"
320
+ },
321
+ "400": {
322
+ content: {
323
+ "application/json": {
324
+ schema: {
325
+ $ref: "#/components/schemas/BadRequest"
326
+ }
327
+ }
328
+ },
329
+ description: "Bad request"
330
+ },
331
+ "401": {
332
+ content: {
333
+ "application/json": {
334
+ schema: {
335
+ $ref: "#/components/schemas/Unauthorized"
336
+ }
337
+ }
338
+ },
339
+ description: "Unauthorized"
340
+ }
341
+ },
342
+ tags: ["Controller"]
343
+ }
344
+ }
345
+ },
346
+ tags: [
347
+ {
348
+ name: "Controller"
349
+ }
350
+ ]
351
+ });
352
+ });
353
+ it("should throw an error when using of with String", () => {
354
+ // WHEN
355
+ let actualError: any;
356
+ try {
357
+ class Controller {
358
+ @OperationPath("POST", "/")
359
+ @(Status(200, String).Of(Array).Description("description"))
360
+ method() {}
361
+ }
362
+ } catch (er) {
363
+ actualError = er;
364
+ }
365
+
366
+ expect(actualError.message).toBe("Returns.Of cannot be used with the following primitive classes: String, Number, Boolean");
367
+ });
368
+ it("should throw an error when using of with Collection", () => {
369
+ // WHEN
370
+ let actualError: any;
371
+ try {
372
+ class Controller {
373
+ @OperationPath("POST", "/")
374
+ @(Status(200, Array).Nested(Set).Description("description"))
375
+ method() {}
376
+ }
377
+ } catch (er) {
378
+ actualError = er;
379
+ }
380
+
381
+ expect(actualError.message).toBe("Returns.Nested cannot be used with the following classes: Map, Set, Array, String, Number, Boolean");
382
+ });
383
+ it("should declare an Array of string", () => {
384
+ // WHEN
385
+ class Controller {
386
+ @OperationPath("POST", "/")
387
+ @(Status(200, Array).Of(String).Description("description"))
388
+ method() {}
389
+ }
390
+
391
+ // THEN
392
+ const spec = getSpec(Controller);
393
+
394
+ expect(spec).toEqual({
395
+ paths: {
396
+ "/": {
397
+ post: {
398
+ operationId: "controllerMethod",
399
+ parameters: [],
400
+ responses: {
401
+ "200": {
402
+ content: {
403
+ "application/json": {
404
+ schema: {
405
+ items: {
406
+ type: "string"
407
+ },
408
+ type: "array"
409
+ }
410
+ }
411
+ },
412
+ description: "description"
413
+ }
414
+ },
415
+ tags: ["Controller"]
416
+ }
417
+ }
418
+ },
419
+ tags: [
420
+ {
421
+ name: "Controller"
422
+ }
423
+ ]
424
+ });
425
+ });
426
+ it("should declare an Array of Model", () => {
427
+ // WHEN
428
+ class Model {
429
+ @Property()
430
+ id: string;
431
+ }
432
+
433
+ class Controller {
434
+ @OperationPath("POST", "/")
435
+ @(Status(200, Array).Of(Model).Description("description"))
436
+ method() {}
437
+ }
438
+
439
+ // THEN
440
+ const spec = getSpec(Controller);
441
+
442
+ expect(spec).toEqual({
443
+ components: {
444
+ schemas: {
445
+ Model: {
446
+ properties: {
447
+ id: {
448
+ type: "string"
449
+ }
450
+ },
451
+ type: "object"
452
+ }
453
+ }
454
+ },
455
+ paths: {
456
+ "/": {
457
+ post: {
458
+ operationId: "controllerMethod",
459
+ parameters: [],
460
+ responses: {
461
+ "200": {
462
+ content: {
463
+ "application/json": {
464
+ schema: {
465
+ items: {
466
+ $ref: "#/components/schemas/Model"
467
+ },
468
+ type: "array"
469
+ }
470
+ }
471
+ },
472
+ description: "description"
473
+ }
474
+ },
475
+ tags: ["Controller"]
476
+ }
477
+ }
478
+ },
479
+ tags: [
480
+ {
481
+ name: "Controller"
482
+ }
483
+ ]
484
+ });
485
+ });
486
+ it("should declare an Generic of Model", () => {
487
+ // WHEN
488
+ @Generics("T")
489
+ class Pagination<T> {
490
+ @CollectionOf("T")
491
+ data: T[];
492
+
493
+ @Property()
494
+ totalCount: number;
495
+ }
496
+
497
+ @Generics("T")
498
+ class Submission<T> {
499
+ @Property()
500
+ _id: string;
501
+
502
+ @Property("T")
503
+ data: T;
504
+ }
505
+
506
+ class Product {
507
+ @Property()
508
+ title: string;
509
+ }
510
+
511
+ class Controller {
512
+ @OperationPath("POST", "/")
513
+ @(Status(200, Pagination).Of(Submission).Nested(Product).Description("description"))
514
+ method(): Promise<Pagination<Submission<Product>> | null> {
515
+ return null as never;
516
+ }
517
+ }
518
+
519
+ // THEN
520
+ const spec = getSpec(Controller);
521
+
522
+ expect(spec).toEqual({
523
+ components: {
524
+ schemas: {
525
+ Product: {
526
+ properties: {
527
+ title: {
528
+ type: "string"
529
+ }
530
+ },
531
+ type: "object"
532
+ }
533
+ }
534
+ },
535
+ paths: {
536
+ "/": {
537
+ post: {
538
+ operationId: "controllerMethod",
539
+ parameters: [],
540
+ responses: {
541
+ "200": {
542
+ content: {
543
+ "application/json": {
544
+ schema: {
545
+ properties: {
546
+ data: {
547
+ items: {
548
+ properties: {
549
+ _id: {
550
+ type: "string"
551
+ },
552
+ data: {
553
+ $ref: "#/components/schemas/Product"
554
+ }
555
+ },
556
+ type: "object"
557
+ },
558
+ type: "array"
559
+ },
560
+ totalCount: {
561
+ type: "number"
562
+ }
563
+ },
564
+ type: "object"
565
+ }
566
+ }
567
+ },
568
+ description: "description"
569
+ }
570
+ },
571
+ tags: ["Controller"]
572
+ }
573
+ }
574
+ },
575
+ tags: [
576
+ {
577
+ name: "Controller"
578
+ }
579
+ ]
580
+ });
581
+ });
582
+ });
@@ -0,0 +1,130 @@
1
+ import {Type} from "@tsed/core";
2
+
3
+ import {Returns, ReturnsChainedDecorators} from "./returns.js";
4
+
5
+ /**
6
+ * Add responses documentation for a specific status code.
7
+ *
8
+ * ## Usage
9
+ *
10
+ * Ts.ED v6 API introducing the chaining decorator concept.
11
+ * Now a decorator like Returns can be used with another decorators like Description.
12
+ *
13
+ * ```typescript
14
+ * import {Returns} from "@tsed/schema";
15
+ *
16
+ * @Controller("/")
17
+ * class MyController {
18
+ * @Status(404, String).Description("Not Found")
19
+ * @Status(200, Model).Description("Success")
20
+ * async myMethod(): Promise<Model> {}
21
+ * }
22
+ * ```
23
+ *
24
+ * ::: tip
25
+ * TypeScript and your IDE will discover automatically the chained decorators. But for more details you can look on @@ReturnsChainedDecorators@@ interface, to know
26
+ * what chained decorators are available under Returns decorator.
27
+ * :::
28
+ *
29
+ * This example will produce this documentation in swagger:
30
+ *
31
+ * ```json
32
+ * {
33
+ * "responses": {
34
+ * "404": {
35
+ * "description": "Description",
36
+ * "schema": {"type": "string"}
37
+ * },
38
+ * "2OO": {
39
+ * "description": "Description",
40
+ * "schema": {"$ref": "..."}
41
+ * }
42
+ * }
43
+ * }
44
+ * ```
45
+ *
46
+ * ### Declaring a generic model
47
+ *
48
+ * Sometime, it might be useful to use generic models. TypeScript doesn't store the generic type in the metadata. This is why we need to
49
+ * declare explicitly the generic models with the decorators.
50
+ *
51
+ * One of the generic's usage, can be a paginated list. With Returns decorator it's now possible to declare generic type and generate the appropriate OpenSpec documentation.
52
+ *
53
+ * Starting with the pagination model, by using @@Generics@@ and @@CollectionOf@@:
54
+ *
55
+ * ```typescript
56
+ * @Generics("T")
57
+ * class Pagination<T> {
58
+ * @CollectionOf("T")
59
+ * data: T[];
60
+ *
61
+ * @Property()
62
+ * totalCount: number;
63
+ * }
64
+ * ```
65
+ *
66
+ * Now, we need a model to be used with the generic Pagination model:
67
+ *
68
+ * ```typescript
69
+ * class Product {
70
+ * @Property()
71
+ * id: string;
72
+ *
73
+ * @Property()
74
+ * title: string;
75
+ * }
76
+ * ```
77
+ *
78
+ * Finally, we can use our models on a method as following:
79
+ *
80
+ * ```typescript
81
+ * class Controller {
82
+ * @OperationPath("POST", "/")
83
+ * @Status(200, Pagination).Of(Product).Description("description")
84
+ * async method(): Promise<Pagination<Product> | null> {
85
+ * return null;
86
+ * }
87
+ * }
88
+ * ```
89
+ *
90
+ * ### Declaring a nested generics models <Badge text="6+"/>
91
+ *
92
+ * It's also possible to declare a nested generics models in order to have this type `Pagination<Submission<Product>>`:
93
+ *
94
+ * ```typescript
95
+ * import {Post, Generics, Property, Returns} from "@tsed/schema";
96
+ *
97
+ * class Controller {
98
+ * @Post("/")
99
+ * @Status(200, Pagination).Of(Submission).Nested(Product).Description("description")
100
+ * async method(): Promise<Pagination<Submission<Product>> | null> {
101
+ * return null;
102
+ * }
103
+ * }
104
+ * ```
105
+ *
106
+ * And here is the Submission model:
107
+ *
108
+ * ```typescript
109
+ * import {Generics, Property} from "@tsed/schema";
110
+ *
111
+ * @Generics("T")
112
+ * class Submission<T> {
113
+ * @Property()
114
+ * _id: string;
115
+ * @Property("T")
116
+ * data: T;
117
+ * }
118
+ * ```
119
+ *
120
+ * @param status
121
+ * @param model
122
+ * @decorator
123
+ * @swagger
124
+ * @schema
125
+ * @response
126
+ * @operation
127
+ */
128
+ export function Status(status: string | number, model?: Type<any> | any): ReturnsChainedDecorators {
129
+ return Returns(status, model);
130
+ }