@tsed/schema 8.0.1 → 8.0.3

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 (292) hide show
  1. package/package.json +11 -10
  2. package/readme.md +6 -6
  3. package/src/components/async-api/channelsMapper.ts +81 -0
  4. package/src/components/async-api/generate.ts +24 -0
  5. package/src/components/async-api/messageMapper.ts +62 -0
  6. package/src/components/async-api/payloadMapper.ts +61 -0
  7. package/src/components/async-api/responseMapper.ts +70 -0
  8. package/src/components/default/anyMapper.spec.ts +7 -0
  9. package/src/components/default/anyMapper.ts +33 -0
  10. package/src/components/default/classMapper.ts +55 -0
  11. package/src/components/default/discriminatorMappingMapper.ts +36 -0
  12. package/src/components/default/enumsMapper.spec.ts +64 -0
  13. package/src/components/default/enumsMapper.ts +17 -0
  14. package/src/components/default/genericsMapper.ts +70 -0
  15. package/src/components/default/inheritedClassMapper.ts +24 -0
  16. package/src/components/default/itemMapper.ts +8 -0
  17. package/src/components/default/lazyRefMapper.ts +21 -0
  18. package/src/components/default/mapMapper.ts +25 -0
  19. package/src/components/default/nullableMapper.ts +55 -0
  20. package/src/components/default/objectMapper.ts +36 -0
  21. package/src/components/default/ofMapper.ts +16 -0
  22. package/src/components/default/propertiesMapper.ts +25 -0
  23. package/src/components/default/requiredMapper.ts +69 -0
  24. package/src/components/default/schemaMapper.ts +151 -0
  25. package/src/components/index.ts +36 -0
  26. package/src/components/open-spec/discriminatorMappingMapper.ts +7 -0
  27. package/src/components/open-spec/enumsMapper.ts +20 -0
  28. package/src/components/open-spec/generate.ts +29 -0
  29. package/src/components/open-spec/nullableMapper.ts +51 -0
  30. package/src/components/open-spec/operationInFilesMapper.ts +32 -0
  31. package/src/components/open-spec/operationInParameterMapper.ts +56 -0
  32. package/src/components/open-spec/operationInParametersMapper.ts +9 -0
  33. package/src/components/open-spec/operationInQueryMapper.ts +74 -0
  34. package/src/components/open-spec/operationMapper.ts +65 -0
  35. package/src/components/open-spec/operationMediaMapper.ts +11 -0
  36. package/src/components/open-spec/operationRequestBodyMapper.ts +82 -0
  37. package/src/components/open-spec/operationResponseMapper.ts +26 -0
  38. package/src/components/open-spec/pathsMapper.ts +132 -0
  39. package/src/constants/OperationVerbs.ts +29 -0
  40. package/src/constants/httpStatusMessages.ts +21 -0
  41. package/src/constants/jsonSchemaProperties.ts +17 -0
  42. package/src/decorators/class/children.ts +22 -0
  43. package/src/decorators/class/discriminatorValue.ts +53 -0
  44. package/src/decorators/collections/collectionContains.ts +25 -0
  45. package/src/decorators/collections/collectionOf.spec.ts +327 -0
  46. package/src/decorators/collections/collectionOf.ts +182 -0
  47. package/src/decorators/collections/maxItems.spec.ts +40 -0
  48. package/src/decorators/collections/maxItems.ts +59 -0
  49. package/src/decorators/collections/maxProperties.spec.ts +109 -0
  50. package/src/decorators/collections/maxProperties.ts +82 -0
  51. package/src/decorators/collections/minItems.spec.ts +40 -0
  52. package/src/decorators/collections/minItems.ts +63 -0
  53. package/src/decorators/collections/minProperties.spec.ts +109 -0
  54. package/src/decorators/collections/minProperties.ts +81 -0
  55. package/src/decorators/collections/uniqueItems.spec.ts +28 -0
  56. package/src/decorators/collections/uniqueItems.ts +59 -0
  57. package/src/decorators/common/__snapshots__/groups.spec.ts.snap +321 -0
  58. package/src/decorators/common/additionalProperties.spec.ts +133 -0
  59. package/src/decorators/common/additionalProperties.ts +17 -0
  60. package/src/decorators/common/allOf.spec.ts +276 -0
  61. package/src/decorators/common/allOf.ts +19 -0
  62. package/src/decorators/common/allow.spec.ts +130 -0
  63. package/src/decorators/common/allow.ts +50 -0
  64. package/src/decorators/common/any.spec.ts +109 -0
  65. package/src/decorators/common/any.ts +45 -0
  66. package/src/decorators/common/anyOf.spec.ts +270 -0
  67. package/src/decorators/common/anyOf.ts +23 -0
  68. package/src/decorators/common/const.spec.ts +98 -0
  69. package/src/decorators/common/const.ts +68 -0
  70. package/src/decorators/common/customKey.spec.ts +105 -0
  71. package/src/decorators/common/customKey.ts +39 -0
  72. package/src/decorators/common/default.spec.ts +25 -0
  73. package/src/decorators/common/default.ts +47 -0
  74. package/src/decorators/common/defaultMsg.spec.ts +22 -0
  75. package/src/decorators/common/defaultMsg.ts +38 -0
  76. package/src/decorators/common/description.spec.ts +166 -0
  77. package/src/decorators/common/description.ts +77 -0
  78. package/src/decorators/common/discriminatorKey.ts +14 -0
  79. package/src/decorators/common/enum.spec.ts +305 -0
  80. package/src/decorators/common/enum.ts +111 -0
  81. package/src/decorators/common/errorMsg.spec.ts +57 -0
  82. package/src/decorators/common/errorMsg.ts +14 -0
  83. package/src/decorators/common/example.spec.ts +93 -0
  84. package/src/decorators/common/example.ts +18 -0
  85. package/src/decorators/common/examples.spec.ts +101 -0
  86. package/src/decorators/common/examples.ts +20 -0
  87. package/src/decorators/common/exclusiveMaximum.spec.ts +47 -0
  88. package/src/decorators/common/exclusiveMaximum.ts +75 -0
  89. package/src/decorators/common/exclusiveMinimum.spec.ts +47 -0
  90. package/src/decorators/common/exclusiveMinimum.ts +75 -0
  91. package/src/decorators/common/format.spec.ts +149 -0
  92. package/src/decorators/common/format.ts +486 -0
  93. package/src/decorators/common/forwardGroups.spec.ts +270 -0
  94. package/src/decorators/common/forwardGroups.ts +16 -0
  95. package/src/decorators/common/groups.spec.ts +968 -0
  96. package/src/decorators/common/groups.ts +66 -0
  97. package/src/decorators/common/hidden.spec.ts +52 -0
  98. package/src/decorators/common/hidden.ts +36 -0
  99. package/src/decorators/common/ignore.spec.ts +88 -0
  100. package/src/decorators/common/ignore.ts +66 -0
  101. package/src/decorators/common/integer.spec.ts +110 -0
  102. package/src/decorators/common/integer.ts +69 -0
  103. package/src/decorators/common/jsonEntityFn.spec.ts +16 -0
  104. package/src/decorators/common/jsonEntityFn.ts +33 -0
  105. package/src/decorators/common/maxLength.spec.ts +61 -0
  106. package/src/decorators/common/maxLength.ts +88 -0
  107. package/src/decorators/common/maximum.spec.ts +73 -0
  108. package/src/decorators/common/maximum.ts +146 -0
  109. package/src/decorators/common/minLength.spec.ts +186 -0
  110. package/src/decorators/common/minLength.ts +89 -0
  111. package/src/decorators/common/minimum.spec.ts +74 -0
  112. package/src/decorators/common/minimum.ts +145 -0
  113. package/src/decorators/common/multipleOf.spec.ts +33 -0
  114. package/src/decorators/common/multipleOf.ts +80 -0
  115. package/src/decorators/common/name.spec.ts +56 -0
  116. package/src/decorators/common/name.ts +50 -0
  117. package/src/decorators/common/nullable.spec.ts +818 -0
  118. package/src/decorators/common/nullable.ts +32 -0
  119. package/src/decorators/common/oneOf.spec.ts +301 -0
  120. package/src/decorators/common/oneOf.ts +24 -0
  121. package/src/decorators/common/optional.spec.ts +43 -0
  122. package/src/decorators/common/optional.ts +26 -0
  123. package/src/decorators/common/pattern.spec.ts +27 -0
  124. package/src/decorators/common/pattern.ts +102 -0
  125. package/src/decorators/common/property.ts +24 -0
  126. package/src/decorators/common/readOnly.spec.ts +135 -0
  127. package/src/decorators/common/readOnly.ts +17 -0
  128. package/src/decorators/common/recordOf.spec.ts +41 -0
  129. package/src/decorators/common/recordOf.ts +59 -0
  130. package/src/decorators/common/required.spec.ts +188 -0
  131. package/src/decorators/common/required.ts +50 -0
  132. package/src/decorators/common/requiredGroups.spec.ts +127 -0
  133. package/src/decorators/common/requiredGroups.ts +23 -0
  134. package/src/decorators/common/schema.spec.ts +23 -0
  135. package/src/decorators/common/schema.ts +64 -0
  136. package/src/decorators/common/title.spec.ts +20 -0
  137. package/src/decorators/common/title.ts +45 -0
  138. package/src/decorators/common/typeError.spec.ts +22 -0
  139. package/src/decorators/common/typeError.ts +15 -0
  140. package/src/decorators/common/writeOnly.spec.ts +105 -0
  141. package/src/decorators/common/writeOnly.ts +17 -0
  142. package/src/decorators/config/jsonEntityComponent.ts +16 -0
  143. package/src/decorators/generics/genericOf.spec.ts +246 -0
  144. package/src/decorators/generics/genericOf.ts +112 -0
  145. package/src/decorators/generics/generics.ts +22 -0
  146. package/src/decorators/operations/acceptMime.spec.ts +39 -0
  147. package/src/decorators/operations/acceptMime.ts +25 -0
  148. package/src/decorators/operations/consumes.spec.ts +89 -0
  149. package/src/decorators/operations/consumes.ts +37 -0
  150. package/src/decorators/operations/contentType.spec.ts +40 -0
  151. package/src/decorators/operations/contentType.ts +24 -0
  152. package/src/decorators/operations/deprecated.spec.ts +223 -0
  153. package/src/decorators/operations/deprecated.ts +49 -0
  154. package/src/decorators/operations/header.spec.ts +214 -0
  155. package/src/decorators/operations/header.ts +62 -0
  156. package/src/decorators/operations/in.spec.ts +321 -0
  157. package/src/decorators/operations/in.ts +141 -0
  158. package/src/decorators/operations/inFile.spec.ts +209 -0
  159. package/src/decorators/operations/inFile.ts +55 -0
  160. package/src/decorators/operations/location.spec.ts +51 -0
  161. package/src/decorators/operations/location.ts +26 -0
  162. package/src/decorators/operations/operation.ts +119 -0
  163. package/src/decorators/operations/operationId.spec.ts +43 -0
  164. package/src/decorators/operations/operationId.ts +22 -0
  165. package/src/decorators/operations/operationPath.spec.ts +42 -0
  166. package/src/decorators/operations/operationPath.ts +33 -0
  167. package/src/decorators/operations/partial.spec.ts +249 -0
  168. package/src/decorators/operations/partial.ts +14 -0
  169. package/src/decorators/operations/path.spec.ts +47 -0
  170. package/src/decorators/operations/path.ts +27 -0
  171. package/src/decorators/operations/produces.spec.ts +89 -0
  172. package/src/decorators/operations/produces.ts +44 -0
  173. package/src/decorators/operations/publish.spec.ts +26 -0
  174. package/src/decorators/operations/publish.ts +6 -0
  175. package/src/decorators/operations/redirect.spec.ts +146 -0
  176. package/src/decorators/operations/redirect.ts +82 -0
  177. package/src/decorators/operations/returns.spec.ts +1393 -0
  178. package/src/decorators/operations/returns.ts +662 -0
  179. package/src/decorators/operations/route.spec.ts +211 -0
  180. package/src/decorators/operations/route.ts +154 -0
  181. package/src/decorators/operations/security.spec.ts +162 -0
  182. package/src/decorators/operations/security.ts +83 -0
  183. package/src/decorators/operations/status.spec.ts +582 -0
  184. package/src/decorators/operations/status.ts +130 -0
  185. package/src/decorators/operations/subscribe.spec.ts +32 -0
  186. package/src/decorators/operations/subscribe.ts +6 -0
  187. package/src/decorators/operations/summary.spec.ts +45 -0
  188. package/src/decorators/operations/summary.ts +32 -0
  189. package/src/decorators/operations/tags.spec.ts +113 -0
  190. package/src/decorators/operations/tags.ts +53 -0
  191. package/src/decorators/operations/view.spec.ts +16 -0
  192. package/src/decorators/operations/view.ts +27 -0
  193. package/src/domain/DecoratorContext.ts +116 -0
  194. package/src/domain/JsonAliasMap.ts +19 -0
  195. package/src/domain/JsonClassStore.ts +35 -0
  196. package/src/domain/JsonDiscriminator.spec.ts +65 -0
  197. package/src/domain/JsonDiscriminator.ts +52 -0
  198. package/src/domain/JsonEntityStore.spec.ts +277 -0
  199. package/src/domain/JsonEntityStore.ts +284 -0
  200. package/src/domain/JsonFormatTypes.ts +17 -0
  201. package/src/domain/JsonLazyRef.ts +21 -0
  202. package/src/domain/JsonMap.spec.ts +15 -0
  203. package/src/domain/JsonMap.ts +33 -0
  204. package/src/domain/JsonMedia.ts +24 -0
  205. package/src/domain/JsonMethodStore.spec.ts +301 -0
  206. package/src/domain/JsonMethodStore.ts +212 -0
  207. package/src/domain/JsonOperation.spec.ts +126 -0
  208. package/src/domain/JsonOperation.ts +219 -0
  209. package/src/domain/JsonOperationPathsMap.ts +16 -0
  210. package/src/domain/JsonOperationRoute.spec.ts +109 -0
  211. package/src/domain/JsonOperationRoute.ts +75 -0
  212. package/src/domain/JsonParameter.ts +70 -0
  213. package/src/domain/JsonParameterStore.spec.ts +212 -0
  214. package/src/domain/JsonParameterStore.ts +138 -0
  215. package/src/domain/JsonParameterTypes.spec.ts +12 -0
  216. package/src/domain/JsonParameterTypes.ts +20 -0
  217. package/src/domain/JsonPropertyStore.spec.ts +177 -0
  218. package/src/domain/JsonPropertyStore.ts +92 -0
  219. package/src/domain/JsonRequestBody.ts +47 -0
  220. package/src/domain/JsonResponse.spec.ts +33 -0
  221. package/src/domain/JsonResponse.ts +64 -0
  222. package/src/domain/JsonSchema.spec.ts +1470 -0
  223. package/src/domain/JsonSchema.ts +1049 -0
  224. package/src/domain/SpecTypes.ts +6 -0
  225. package/src/hooks/alterIgnore.ts +25 -0
  226. package/src/hooks/alterOneOf.ts +67 -0
  227. package/src/hooks/alterRequiredGroups.ts +12 -0
  228. package/src/index.ts +185 -0
  229. package/src/interfaces/IgnoreCallback.ts +5 -0
  230. package/src/interfaces/JsonHookContext.ts +18 -0
  231. package/src/interfaces/JsonOpenSpec.ts +7 -0
  232. package/src/interfaces/JsonSchemaOptions.ts +35 -0
  233. package/src/registries/JsonSchemaMapperContainer.ts +52 -0
  234. package/src/registries/enumRegistries.ts +3 -0
  235. package/src/utils/__snapshots__/generateSpec.spec.ts.snap +109 -0
  236. package/src/utils/buildPath.ts +8 -0
  237. package/src/utils/concatParameters.ts +12 -0
  238. package/src/utils/concatPath.spec.ts +23 -0
  239. package/src/utils/concatPath.ts +24 -0
  240. package/src/utils/defineStatusModel.ts +11 -0
  241. package/src/utils/from.spec.ts +243 -0
  242. package/src/utils/from.ts +281 -0
  243. package/src/utils/generateSpec.spec.ts +1262 -0
  244. package/src/utils/generateSpec.ts +54 -0
  245. package/src/utils/generics.ts +83 -0
  246. package/src/utils/getComputedType.spec.ts +45 -0
  247. package/src/utils/getComputedType.ts +39 -0
  248. package/src/utils/getInheritedStores.spec.ts +47 -0
  249. package/src/utils/getInheritedStores.ts +23 -0
  250. package/src/utils/getJsonEntityStore.ts +36 -0
  251. package/src/utils/getJsonPathParameters.spec.ts +126 -0
  252. package/src/utils/getJsonPathParameters.ts +97 -0
  253. package/src/utils/getJsonSchema.spec.ts +695 -0
  254. package/src/utils/getJsonSchema.ts +69 -0
  255. package/src/utils/getJsonType.spec.ts +28 -0
  256. package/src/utils/getJsonType.ts +46 -0
  257. package/src/utils/getOperationsRoutes.spec.ts +284 -0
  258. package/src/utils/getOperationsRoutes.ts +56 -0
  259. package/src/utils/getOperationsStore.spec.ts +47 -0
  260. package/src/utils/getOperationsStores.ts +37 -0
  261. package/src/utils/getPropertiesStores.spec.ts +138 -0
  262. package/src/utils/getPropertiesStores.ts +66 -0
  263. package/src/utils/getSpec.spec.ts +610 -0
  264. package/src/utils/getSpec.ts +117 -0
  265. package/src/utils/getSpecType.spec.ts +16 -0
  266. package/src/utils/getSpecType.ts +19 -0
  267. package/src/utils/isSuccessStatus.spec.ts +22 -0
  268. package/src/utils/isSuccessStatus.ts +2 -0
  269. package/src/utils/mapHeaders.spec.ts +57 -0
  270. package/src/utils/mapHeaders.ts +33 -0
  271. package/src/utils/mapOpenSpec.ts +44 -0
  272. package/src/utils/mapOpenSpec2.ts +50 -0
  273. package/src/utils/mapOpenSpec3.ts +25 -0
  274. package/src/utils/mapOpenSpecInfo.ts +19 -0
  275. package/src/utils/mapOperationOptions.ts +34 -0
  276. package/src/utils/matchGroups.spec.ts +85 -0
  277. package/src/utils/matchGroups.ts +64 -0
  278. package/src/utils/mergeSpec.spec.ts +533 -0
  279. package/src/utils/mergeSpec.ts +54 -0
  280. package/src/utils/operationIdFormatter.spec.ts +11 -0
  281. package/src/utils/operationIdFormatter.ts +49 -0
  282. package/src/utils/ref.ts +65 -0
  283. package/src/utils/removeHiddenOperation.ts +5 -0
  284. package/src/utils/serializeEnumValues.ts +27 -0
  285. package/src/utils/somethingOf.ts +11 -0
  286. package/src/utils/toJsonMapCollection.spec.ts +18 -0
  287. package/src/utils/toJsonMapCollection.ts +12 -0
  288. package/src/utils/toJsonRegex.ts +7 -0
  289. package/src/utils/transformToOS2.spec.ts +177 -0
  290. package/src/utils/transformToOS2.ts +607 -0
  291. package/src/utils/withErrorMsg.spec.ts +16 -0
  292. package/src/utils/withErrorMsg.ts +56 -0
@@ -0,0 +1,321 @@
1
+ import {execMapper, getSpec, In, JsonEntityStore, Name, number, OperationPath, Path, SpecTypes} from "../../index.js";
2
+
3
+ describe("In", () => {
4
+ it("should declare all schema correctly (param)", () => {
5
+ // WHEN
6
+ class Controller {
7
+ method(@In("path") @Name("basic") basic: string) {}
8
+ }
9
+
10
+ // THEN
11
+ getSpec(Controller, {
12
+ specType: SpecTypes.SWAGGER
13
+ });
14
+
15
+ const paramSchema = JsonEntityStore.from(Controller, "method", 0);
16
+ const methodSchema = paramSchema.parent;
17
+ const operation = execMapper("operation", [methodSchema.operation], {});
18
+
19
+ expect(operation).toEqual({
20
+ parameters: [
21
+ {
22
+ in: "path",
23
+ name: "basic",
24
+ required: true,
25
+ schema: {
26
+ type: "string"
27
+ }
28
+ }
29
+ ],
30
+ responses: {
31
+ "200": {
32
+ description: "Success"
33
+ }
34
+ }
35
+ });
36
+ });
37
+ it("should declare all schema correctly (method)", () => {
38
+ // WHEN
39
+ class Controller {
40
+ @(In("header").Type(String).Name("Authorization").Required().Description("description"))
41
+ method(@In("path") @Name("basic") basic: string) {}
42
+ }
43
+
44
+ // THEN
45
+ getSpec(Controller, {
46
+ specType: SpecTypes.SWAGGER
47
+ });
48
+
49
+ const paramSchema = JsonEntityStore.from(Controller, "method", 0);
50
+ const methodSchema = paramSchema.parent;
51
+ const operation = execMapper("operation", [methodSchema.operation], {});
52
+
53
+ expect(operation).toEqual({
54
+ parameters: [
55
+ {
56
+ in: "path",
57
+ name: "basic",
58
+ required: true,
59
+ schema: {
60
+ type: "string"
61
+ }
62
+ },
63
+ {
64
+ description: "description",
65
+ in: "header",
66
+ name: "Authorization",
67
+ required: true,
68
+ schema: {
69
+ type: "string"
70
+ }
71
+ }
72
+ ],
73
+ responses: {
74
+ "200": {
75
+ description: "Success"
76
+ }
77
+ }
78
+ });
79
+ });
80
+ it("should declare all schema correctly (class)", () => {
81
+ // WHEN
82
+ class Controller {
83
+ @(In("header").Type(String).Name("Authorization").Required().Description("description"))
84
+ method(@In("path") @Name("basic") basic: string) {}
85
+ }
86
+
87
+ // THEN
88
+ getSpec(Controller, {
89
+ specType: SpecTypes.SWAGGER
90
+ });
91
+
92
+ const paramSchema = JsonEntityStore.from(Controller, "method", 0);
93
+ const methodSchema = paramSchema.parent;
94
+ const operation = execMapper("operation", [methodSchema.operation], {});
95
+
96
+ expect(operation).toEqual({
97
+ parameters: [
98
+ {
99
+ in: "path",
100
+ name: "basic",
101
+ required: true,
102
+ schema: {
103
+ type: "string"
104
+ }
105
+ },
106
+ {
107
+ description: "description",
108
+ in: "header",
109
+ name: "Authorization",
110
+ required: true,
111
+ schema: {
112
+ type: "string"
113
+ }
114
+ }
115
+ ],
116
+ responses: {
117
+ "200": {
118
+ description: "Success"
119
+ }
120
+ }
121
+ });
122
+ });
123
+ it("should declare a pattern", () => {
124
+ // WHEN
125
+ @Path("/:parentId")
126
+ @(In("path")
127
+ .Type(String)
128
+ .Name("parentId")
129
+ .Required()
130
+ .Description("description")
131
+ .Pattern(/^[0-9a-fA-F]{24}$/))
132
+ class Controller {
133
+ @OperationPath("GET", "/:path")
134
+ method(@In("path") @Name("basic") basic: string) {}
135
+ }
136
+
137
+ // THEN
138
+ const spec = getSpec(Controller, {
139
+ specType: SpecTypes.OPENAPI
140
+ });
141
+
142
+ const paramSchema = JsonEntityStore.from(Controller, "method", 0);
143
+ const methodSchema = paramSchema.parent;
144
+ const operation = execMapper("operation", [methodSchema.operation!], {
145
+ specType: SpecTypes.OPENAPI
146
+ });
147
+
148
+ expect(operation).toEqual({
149
+ parameters: [
150
+ {
151
+ in: "path",
152
+ name: "basic",
153
+ required: true,
154
+ schema: {
155
+ type: "string"
156
+ }
157
+ },
158
+ {
159
+ description: "description",
160
+ in: "path",
161
+ name: "parentId",
162
+ required: true,
163
+ schema: {
164
+ pattern: "^[0-9a-fA-F]{24}$",
165
+ type: "string"
166
+ }
167
+ }
168
+ ],
169
+ responses: {
170
+ "200": {
171
+ description: "Success"
172
+ }
173
+ }
174
+ });
175
+ expect(spec).toEqual({
176
+ paths: {
177
+ "/{parentId}/{path}": {
178
+ get: {
179
+ operationId: "controllerMethod",
180
+ parameters: [
181
+ {
182
+ description: "description",
183
+ in: "path",
184
+ name: "parentId",
185
+ required: true,
186
+ schema: {
187
+ type: "string",
188
+ pattern: "^[0-9a-fA-F]{24}$"
189
+ }
190
+ },
191
+ {
192
+ in: "path",
193
+ name: "path",
194
+ required: true,
195
+ schema: {
196
+ type: "string"
197
+ }
198
+ }
199
+ ],
200
+ responses: {
201
+ "200": {
202
+ description: "Success"
203
+ }
204
+ },
205
+ tags: ["Controller"]
206
+ }
207
+ }
208
+ },
209
+ tags: [
210
+ {
211
+ name: "Controller"
212
+ }
213
+ ]
214
+ });
215
+ });
216
+ it("should declare schema", () => {
217
+ // WHEN
218
+ @Path("/:parentId")
219
+ @(In("path").Name("parentId").Required().Description("description").Schema(number().integer().minimum(2)))
220
+ class Controller {
221
+ @OperationPath("GET", "/:path")
222
+ method(@In("path") @Name("basic") basic: string) {}
223
+ }
224
+
225
+ // THEN
226
+ const spec = getSpec(Controller, {
227
+ specType: SpecTypes.OPENAPI
228
+ });
229
+
230
+ const paramSchema = JsonEntityStore.from(Controller, "method", 0);
231
+ const methodSchema = paramSchema.parent;
232
+ const operation = execMapper("operation", [methodSchema.operation!], {
233
+ specType: SpecTypes.OPENAPI
234
+ });
235
+
236
+ expect(operation).toEqual({
237
+ parameters: [
238
+ {
239
+ in: "path",
240
+ name: "basic",
241
+ required: true,
242
+ schema: {
243
+ type: "string"
244
+ }
245
+ },
246
+ {
247
+ description: "description",
248
+ in: "path",
249
+ name: "parentId",
250
+ required: true,
251
+ schema: {
252
+ minimum: 2,
253
+ multipleOf: 1,
254
+ type: "integer"
255
+ }
256
+ }
257
+ ],
258
+ responses: {
259
+ "200": {
260
+ description: "Success"
261
+ }
262
+ }
263
+ });
264
+ expect(spec).toEqual({
265
+ paths: {
266
+ "/{parentId}/{path}": {
267
+ get: {
268
+ operationId: "controllerMethod",
269
+ parameters: [
270
+ {
271
+ description: "description",
272
+ in: "path",
273
+ name: "parentId",
274
+ required: true,
275
+ schema: {
276
+ minimum: 2,
277
+ multipleOf: 1,
278
+ type: "integer"
279
+ }
280
+ },
281
+ {
282
+ in: "path",
283
+ name: "path",
284
+ required: true,
285
+ schema: {
286
+ type: "string"
287
+ }
288
+ }
289
+ ],
290
+ responses: {
291
+ "200": {
292
+ description: "Success"
293
+ }
294
+ },
295
+ tags: ["Controller"]
296
+ }
297
+ }
298
+ },
299
+ tags: [
300
+ {
301
+ name: "Controller"
302
+ }
303
+ ]
304
+ });
305
+ });
306
+ it("should throw error for unsupported usage", () => {
307
+ class Test {
308
+ test() {}
309
+ }
310
+
311
+ let actualError: any;
312
+ try {
313
+ // @ts-ignore
314
+ In("tags")(Test.prototype, "test");
315
+ } catch (er) {
316
+ actualError = er;
317
+ }
318
+
319
+ expect(actualError.message).toEqual("In cannot be used as property decorator on Test.test");
320
+ });
321
+ });
@@ -0,0 +1,141 @@
1
+ import {decorateMethodsOf, DecoratorTypes, Type, UnsupportedDecoratorType} from "@tsed/core";
2
+
3
+ import {JsonEntityStore} from "../../domain/JsonEntityStore.js";
4
+ import {JsonMethodStore} from "../../domain/JsonMethodStore.js";
5
+ import {JsonParameter} from "../../domain/JsonParameter.js";
6
+ import {JsonParameterStore} from "../../domain/JsonParameterStore.js";
7
+ import {JsonParameterTypes} from "../../domain/JsonParameterTypes.js";
8
+ import {JsonSchema, JsonSchemaObject} from "../../domain/JsonSchema.js";
9
+
10
+ export interface InChainedDecorators {
11
+ <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> | void;
12
+
13
+ (target: Object, propertyKey: string | symbol, parameterIndex: number): void;
14
+
15
+ (target: Function): void;
16
+
17
+ /**
18
+ * Type of this in parameter
19
+ * @param type
20
+ */
21
+ Type(type: Type<any>): this;
22
+
23
+ /**
24
+ *
25
+ * @param name
26
+ */
27
+ Name(name: string): this;
28
+
29
+ /**
30
+ *
31
+ * @param description
32
+ */
33
+ Description(description: string): this;
34
+
35
+ /**
36
+ *
37
+ * @param required
38
+ */
39
+ Required(required?: boolean): this;
40
+
41
+ /**
42
+ * Add pattern constraint. Only available for OPENAPI.
43
+ * @param pattern
44
+ */
45
+ Pattern(pattern: string | RegExp): this;
46
+
47
+ /**
48
+ * Add custom schema.
49
+ * @param schema
50
+ */
51
+ Schema(schema: Partial<JsonSchemaObject | JsonSchema>): this;
52
+ }
53
+
54
+ /**
55
+ * Add a input parameter.
56
+ *
57
+ * ::: warning
58
+ * Don't use decorator with Ts.ED application to decorate parameters. Use @@BodyParams@@, @@PathParams@@, etc... instead.
59
+ * But you can use this decorator on Method, to add extra in parameters like Authorization header.
60
+ *
61
+ * ```typescript
62
+ * @Controller("/")
63
+ * class MyController {
64
+ * @Get("/")
65
+ * @In("header").Type(String).Name("Authorization").Required()
66
+ * method() {
67
+ * }
68
+ * }
69
+ * ```
70
+ * :::
71
+ *
72
+ * @param inType
73
+ * @decorator
74
+ * @swagger
75
+ * @schema
76
+ * @input
77
+ * @operation
78
+ */
79
+ export function In(inType: JsonParameterTypes | string): InChainedDecorators {
80
+ const jsonParameter = new JsonParameter();
81
+ const schema: any = {};
82
+
83
+ const decorator = (target: any, propertyKey?: string | symbol, index?: PropertyDescriptor | number) => {
84
+ const store = JsonEntityStore.from(target, propertyKey, index);
85
+
86
+ switch (store.decoratorType) {
87
+ case DecoratorTypes.PARAM:
88
+ (store as JsonParameterStore).parameter.in(inType);
89
+ break;
90
+ case DecoratorTypes.METHOD:
91
+ jsonParameter.in(inType);
92
+ (store as JsonMethodStore).operation.addParameter(-1, jsonParameter);
93
+
94
+ jsonParameter.schema(JsonSchema.from(schema));
95
+ break;
96
+
97
+ case DecoratorTypes.CLASS:
98
+ decorateMethodsOf(target, decorator);
99
+ break;
100
+
101
+ default:
102
+ throw new UnsupportedDecoratorType(In, [target, propertyKey, index]);
103
+ }
104
+ };
105
+
106
+ decorator.Type = (type: Type<any>) => {
107
+ schema.type = type;
108
+
109
+ return decorator;
110
+ };
111
+
112
+ decorator.Name = (name: string) => {
113
+ jsonParameter.name(name);
114
+
115
+ return decorator;
116
+ };
117
+
118
+ decorator.Description = (description: string) => {
119
+ jsonParameter.description(description);
120
+
121
+ return decorator;
122
+ };
123
+
124
+ decorator.Required = (required: boolean = true) => {
125
+ jsonParameter.required(required);
126
+
127
+ return decorator;
128
+ };
129
+
130
+ decorator.Pattern = (pattern: string | RegExp) => {
131
+ return decorator.Schema({pattern: pattern.toString()});
132
+ };
133
+
134
+ decorator.Schema = (_schema: any) => {
135
+ Object.assign(schema, _schema.toJSON ? _schema.toJSON() : _schema);
136
+
137
+ return decorator;
138
+ };
139
+
140
+ return decorator;
141
+ }
@@ -0,0 +1,209 @@
1
+ import {generateSpec, getSpec, OperationPath, Path, SpecTypes} from "../../index.js";
2
+ import {InFile} from "./inFile.js";
3
+
4
+ describe("@File()", () => {
5
+ describe("one file", () => {
6
+ @Path("/")
7
+ class TestController {
8
+ @OperationPath("POST", "/")
9
+ test(@InFile("file1") file: any) {}
10
+ }
11
+
12
+ it("should set endpoint metadata - OS2", async () => {
13
+ expect(await generateSpec({tokens: [{token: TestController}], specType: SpecTypes.SWAGGER})).toEqual({
14
+ consumes: ["application/json"],
15
+ info: {
16
+ title: "Api documentation",
17
+ version: "1.0.0"
18
+ },
19
+ paths: {
20
+ "/": {
21
+ post: {
22
+ consumes: ["multipart/form-data"],
23
+ operationId: "testControllerTest",
24
+ parameters: [
25
+ {
26
+ format: "binary",
27
+ in: "formData",
28
+ name: "file1",
29
+ type: "string"
30
+ }
31
+ ],
32
+ produces: ["application/octet-stream"],
33
+ responses: {
34
+ "400": {
35
+ description:
36
+ "<File too long | Too many parts | Too many files | Field name too long | Field value too long | Too many fields | Unexpected field> [fieldName] Example: File too long file1",
37
+ schema: {
38
+ type: "object"
39
+ }
40
+ }
41
+ },
42
+ tags: ["TestController"]
43
+ }
44
+ }
45
+ },
46
+ produces: ["application/json"],
47
+ swagger: "2.0",
48
+ tags: [
49
+ {
50
+ name: "TestController"
51
+ }
52
+ ]
53
+ });
54
+ });
55
+
56
+ it("should set endpoint metadata - OS3", () => {
57
+ expect(getSpec(TestController, {specType: SpecTypes.OPENAPI})).toEqual({
58
+ paths: {
59
+ "/": {
60
+ post: {
61
+ operationId: "testControllerTest",
62
+ parameters: [],
63
+ requestBody: {
64
+ content: {
65
+ "multipart/form-data": {
66
+ schema: {
67
+ properties: {
68
+ file1: {
69
+ format: "binary",
70
+ type: "string"
71
+ }
72
+ },
73
+ type: "object"
74
+ }
75
+ }
76
+ },
77
+ required: false
78
+ },
79
+ responses: {
80
+ "400": {
81
+ content: {
82
+ "*/*": {
83
+ schema: {
84
+ type: "object"
85
+ }
86
+ }
87
+ },
88
+ description:
89
+ "<File too long | Too many parts | Too many files | Field name too long | Field value too long | Too many fields | Unexpected field> [fieldName] Example: File too long file1"
90
+ }
91
+ },
92
+ tags: ["TestController"]
93
+ }
94
+ }
95
+ },
96
+ tags: [
97
+ {
98
+ name: "TestController"
99
+ }
100
+ ]
101
+ });
102
+ });
103
+ });
104
+ describe("many files", () => {
105
+ @Path("/")
106
+ class TestController {
107
+ @OperationPath("POST", "/")
108
+ test(@InFile("file1") file: any[]) {}
109
+ }
110
+
111
+ it("should set endpoint metadata - OS2", async () => {
112
+ expect(await generateSpec({tokens: [{token: TestController}], specType: SpecTypes.SWAGGER})).toEqual({
113
+ consumes: ["application/json"],
114
+ info: {
115
+ title: "Api documentation",
116
+ version: "1.0.0"
117
+ },
118
+ paths: {
119
+ "/": {
120
+ post: {
121
+ consumes: ["multipart/form-data"],
122
+ operationId: "testControllerTest",
123
+ parameters: [
124
+ {
125
+ collectionFormat: "csv",
126
+ in: "formData",
127
+ items: {
128
+ format: "binary",
129
+ type: "string"
130
+ },
131
+ name: "file1",
132
+ type: "array"
133
+ }
134
+ ],
135
+ produces: ["application/octet-stream"],
136
+ responses: {
137
+ "400": {
138
+ description:
139
+ "<File too long | Too many parts | Too many files | Field name too long | Field value too long | Too many fields | Unexpected field> [fieldName] Example: File too long file1",
140
+ schema: {
141
+ type: "object"
142
+ }
143
+ }
144
+ },
145
+ tags: ["TestController"]
146
+ }
147
+ }
148
+ },
149
+ produces: ["application/json"],
150
+ swagger: "2.0",
151
+ tags: [
152
+ {
153
+ name: "TestController"
154
+ }
155
+ ]
156
+ });
157
+ });
158
+ it("should set endpoint metadata - OS3", () => {
159
+ expect(getSpec(TestController, {specType: SpecTypes.OPENAPI})).toEqual({
160
+ paths: {
161
+ "/": {
162
+ post: {
163
+ operationId: "testControllerTest",
164
+ parameters: [],
165
+ requestBody: {
166
+ content: {
167
+ "multipart/form-data": {
168
+ schema: {
169
+ properties: {
170
+ file1: {
171
+ items: {
172
+ format: "binary",
173
+ type: "string"
174
+ },
175
+ type: "array"
176
+ }
177
+ },
178
+ type: "object"
179
+ }
180
+ }
181
+ },
182
+ required: false
183
+ },
184
+ responses: {
185
+ "400": {
186
+ content: {
187
+ "*/*": {
188
+ schema: {
189
+ type: "object"
190
+ }
191
+ }
192
+ },
193
+ description:
194
+ "<File too long | Too many parts | Too many files | Field name too long | Field value too long | Too many fields | Unexpected field> [fieldName] Example: File too long file1"
195
+ }
196
+ },
197
+ tags: ["TestController"]
198
+ }
199
+ }
200
+ },
201
+ tags: [
202
+ {
203
+ name: "TestController"
204
+ }
205
+ ]
206
+ });
207
+ });
208
+ });
209
+ });