@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,1262 @@
1
+ import {BodyParams} from "@tsed/platform-params";
2
+ import fs from "fs-extra";
3
+ import {join} from "path";
4
+
5
+ import {validateSpec} from "../../test/helpers/validateSpec.js";
6
+ import {CollectionOf} from "../decorators/collections/collectionOf.js";
7
+ import {AnyOf} from "../decorators/common/anyOf.js";
8
+ import {Description} from "../decorators/common/description.js";
9
+ import {Min} from "../decorators/common/minimum.js";
10
+ import {Name} from "../decorators/common/name.js";
11
+ import {Property} from "../decorators/common/property.js";
12
+ import {Required} from "../decorators/common/required.js";
13
+ import {Consumes} from "../decorators/operations/consumes.js";
14
+ import {In} from "../decorators/operations/in.js";
15
+ import {OperationPath} from "../decorators/operations/operationPath.js";
16
+ import {Path} from "../decorators/operations/path.js";
17
+ import {Returns} from "../decorators/operations/returns.js";
18
+ import {Post} from "../decorators/operations/route.js";
19
+ import {SpecTypes} from "../domain/SpecTypes.js";
20
+ import {generateSpec} from "./generateSpec.js";
21
+
22
+ const rootDir = import.meta.dirname; // automatically replaced by import.meta.dirname on build
23
+
24
+ describe("generateSpec()", () => {
25
+ describe("OS 3.0.1", () => {
26
+ it("should generate spec with options", () => {
27
+ // WHEN
28
+ @Path("/controller1")
29
+ class Controller1 {
30
+ @OperationPath("GET", "/:id?")
31
+ method(@In("path") @Name("id") id: string) {}
32
+ }
33
+
34
+ @Path("/controller2")
35
+ class Controller2 {
36
+ @OperationPath("GET", "/:id?")
37
+ method(@In("path") @Name("id") id: string) {}
38
+ }
39
+
40
+ const result = generateSpec({
41
+ tokens: [
42
+ {token: Controller1, rootPath: "/rest"},
43
+ {token: Controller2, rootPath: "/rest"}
44
+ ],
45
+ specVersion: "3.0.1",
46
+ fileSpec: fs.readJSONSync(join(rootDir, "__mock__", "spec.json"))
47
+ });
48
+
49
+ expect(result).toEqual({
50
+ info: {
51
+ contact: {
52
+ email: "apiteam@swagger.io"
53
+ },
54
+ description:
55
+ "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.",
56
+ license: {
57
+ name: "Apache 2.0",
58
+ url: "http://www.apache.org/licenses/LICENSE-2.0.html"
59
+ },
60
+ termsOfService: "http://swagger.io/terms/",
61
+ title: "Swagger Petstore",
62
+ version: "1.0.0"
63
+ },
64
+ openapi: "3.0.1",
65
+ paths: {
66
+ "/rest/controller1": {
67
+ get: {
68
+ operationId: "controller1Method",
69
+ parameters: [],
70
+ responses: {
71
+ "200": {
72
+ description: "Success"
73
+ }
74
+ },
75
+ tags: ["Controller1"]
76
+ }
77
+ },
78
+ "/rest/controller1/{id}": {
79
+ get: {
80
+ operationId: "controller1MethodById",
81
+ parameters: [
82
+ {
83
+ in: "path",
84
+ name: "id",
85
+ required: true,
86
+ schema: {
87
+ type: "string"
88
+ }
89
+ }
90
+ ],
91
+ responses: {
92
+ "200": {
93
+ description: "Success"
94
+ }
95
+ },
96
+ tags: ["Controller1"]
97
+ }
98
+ },
99
+ "/rest/controller2": {
100
+ get: {
101
+ operationId: "controller2Method",
102
+ parameters: [],
103
+ responses: {
104
+ "200": {
105
+ description: "Success"
106
+ }
107
+ },
108
+ tags: ["Controller2"]
109
+ }
110
+ },
111
+ "/rest/controller2/{id}": {
112
+ get: {
113
+ operationId: "controller2MethodById",
114
+ parameters: [
115
+ {
116
+ in: "path",
117
+ name: "id",
118
+ required: true,
119
+ schema: {
120
+ type: "string"
121
+ }
122
+ }
123
+ ],
124
+ responses: {
125
+ "200": {
126
+ description: "Success"
127
+ }
128
+ },
129
+ tags: ["Controller2"]
130
+ }
131
+ }
132
+ },
133
+ tags: [
134
+ {
135
+ name: "Controller1"
136
+ },
137
+ {
138
+ name: "Controller2"
139
+ }
140
+ ]
141
+ });
142
+ });
143
+ it("should generate spec and correctly merge shared model with custom schema", () => {
144
+ class Model {
145
+ @AnyOf(Number, Boolean, String, {type: "array", items: {type: "number"}}, {type: "array", items: {type: "string"}})
146
+ test: number | boolean | string | number[] | string[];
147
+ }
148
+
149
+ @Path("/controller1")
150
+ class Controller1 {
151
+ @Post("/post")
152
+ method(@BodyParams() body: Model) {}
153
+ }
154
+
155
+ @Path("/controller2")
156
+ class Controller2 {
157
+ @Post("/post")
158
+ method(@BodyParams() body: Model) {}
159
+ }
160
+
161
+ const result = generateSpec({
162
+ tokens: [
163
+ {token: Controller1, rootPath: "/rest"},
164
+ {token: Controller2, rootPath: "/rest"}
165
+ ],
166
+ specVersion: "3.0.1",
167
+ fileSpec: fs.readJSONSync(join(rootDir, "__mock__", "spec.json"))
168
+ });
169
+
170
+ expect(result).toEqual({
171
+ info: {
172
+ contact: {
173
+ email: "apiteam@swagger.io"
174
+ },
175
+ description:
176
+ "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.",
177
+ license: {
178
+ name: "Apache 2.0",
179
+ url: "http://www.apache.org/licenses/LICENSE-2.0.html"
180
+ },
181
+ termsOfService: "http://swagger.io/terms/",
182
+ title: "Swagger Petstore",
183
+ version: "1.0.0"
184
+ },
185
+ openapi: "3.0.1",
186
+ paths: {
187
+ "/rest/controller1/post": {
188
+ post: {
189
+ operationId: "controller1Method",
190
+ parameters: [],
191
+ requestBody: {
192
+ content: {
193
+ "application/json": {
194
+ schema: {
195
+ $ref: "#/components/schemas/Model"
196
+ }
197
+ }
198
+ },
199
+ required: false
200
+ },
201
+ responses: {
202
+ "200": {
203
+ description: "Success"
204
+ }
205
+ },
206
+ tags: ["Controller1"]
207
+ }
208
+ },
209
+ "/rest/controller2/post": {
210
+ post: {
211
+ operationId: "controller2Method",
212
+ parameters: [],
213
+ requestBody: {
214
+ content: {
215
+ "application/json": {
216
+ schema: {
217
+ $ref: "#/components/schemas/Model"
218
+ }
219
+ }
220
+ },
221
+ required: false
222
+ },
223
+ responses: {
224
+ "200": {
225
+ description: "Success"
226
+ }
227
+ },
228
+ tags: ["Controller2"]
229
+ }
230
+ }
231
+ },
232
+ tags: [
233
+ {
234
+ name: "Controller1"
235
+ },
236
+ {
237
+ name: "Controller2"
238
+ }
239
+ ],
240
+ components: {
241
+ schemas: {
242
+ Model: {
243
+ type: "object",
244
+ properties: {
245
+ test: {
246
+ anyOf: [
247
+ {type: "number"},
248
+ {type: "boolean"},
249
+ {type: "string"},
250
+ {type: "array", items: {type: "number"}},
251
+ {type: "array", items: {type: "string"}}
252
+ ]
253
+ }
254
+ }
255
+ }
256
+ }
257
+ }
258
+ });
259
+ });
260
+ it("should generate spec with sorted paths", () => {
261
+ // WHEN
262
+ @Path("/controller2")
263
+ class Controller2 {
264
+ @OperationPath("GET", "/:id?")
265
+ method(@In("path") @Name("id") id: string) {}
266
+ }
267
+
268
+ @Path("/controller1")
269
+ class Controller1 {
270
+ @OperationPath("GET", "/")
271
+ method2() {}
272
+
273
+ @OperationPath("GET", "/:id?")
274
+ method(@In("path") @Name("id") id: string) {}
275
+
276
+ @OperationPath("POST", "/test")
277
+ method5() {}
278
+ }
279
+
280
+ const result = generateSpec({
281
+ sortPaths: true,
282
+ tokens: [
283
+ {token: Controller2, rootPath: "/rest"},
284
+ {token: Controller1, rootPath: "/rest"}
285
+ ],
286
+ specVersion: "3.0.1",
287
+ specPath: join(rootDir, "__mock__", "spec.json")
288
+ });
289
+
290
+ expect(result).toMatchSnapshot();
291
+ });
292
+ });
293
+ describe("Swagger 2", () => {
294
+ it("should generate spec with options", () => {
295
+ // WHEN
296
+ @Path("/controller1")
297
+ class Controller1 {
298
+ @OperationPath("GET", "/:id?")
299
+ method(@In("path") @Name("id") id: string) {}
300
+ }
301
+
302
+ @Path("/controller2")
303
+ class Controller2 {
304
+ @OperationPath("GET", "/:id?")
305
+ method(@In("path") @Name("id") id: string) {}
306
+ }
307
+
308
+ const result = generateSpec({
309
+ tokens: [
310
+ {token: Controller1, rootPath: "/rest"},
311
+ {token: Controller2, rootPath: "/rest"}
312
+ ],
313
+ fileSpec: fs.readJSONSync(join(rootDir, "__mock__", "spec.json"))
314
+ });
315
+
316
+ expect(result).toEqual({
317
+ consumes: ["application/json"],
318
+ info: {
319
+ contact: {
320
+ email: "apiteam@swagger.io"
321
+ },
322
+ description:
323
+ "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.",
324
+ license: {
325
+ name: "Apache 2.0",
326
+ url: "http://www.apache.org/licenses/LICENSE-2.0.html"
327
+ },
328
+ termsOfService: "http://swagger.io/terms/",
329
+ title: "Swagger Petstore",
330
+ version: "1.0.0"
331
+ },
332
+ paths: {
333
+ "/rest/controller1": {
334
+ get: {
335
+ operationId: "controller1Method",
336
+ parameters: [],
337
+ responses: {
338
+ "200": {
339
+ description: "Success"
340
+ }
341
+ },
342
+ tags: ["Controller1"]
343
+ }
344
+ },
345
+ "/rest/controller1/{id}": {
346
+ get: {
347
+ operationId: "controller1MethodById",
348
+ parameters: [
349
+ {
350
+ in: "path",
351
+ name: "id",
352
+ required: true,
353
+ type: "string"
354
+ }
355
+ ],
356
+ responses: {
357
+ "200": {
358
+ description: "Success"
359
+ }
360
+ },
361
+ tags: ["Controller1"]
362
+ }
363
+ },
364
+ "/rest/controller2": {
365
+ get: {
366
+ operationId: "controller2Method",
367
+ parameters: [],
368
+ responses: {
369
+ "200": {
370
+ description: "Success"
371
+ }
372
+ },
373
+ tags: ["Controller2"]
374
+ }
375
+ },
376
+ "/rest/controller2/{id}": {
377
+ get: {
378
+ operationId: "controller2MethodById",
379
+ parameters: [
380
+ {
381
+ in: "path",
382
+ name: "id",
383
+ required: true,
384
+ type: "string"
385
+ }
386
+ ],
387
+ responses: {
388
+ "200": {
389
+ description: "Success"
390
+ }
391
+ },
392
+ tags: ["Controller2"]
393
+ }
394
+ }
395
+ },
396
+ produces: ["application/json"],
397
+ securityDefinitions: {
398
+ calendar_auth: {
399
+ authorizationUrl: "http://petstore.swagger.io/oauth/dialog",
400
+ flow: "implicit",
401
+ scopes: {
402
+ "read:calendar": "read your pets",
403
+ "write:calendar": "modify pets in your account"
404
+ },
405
+ type: "oauth2"
406
+ },
407
+ global_auth: {
408
+ authorizationUrl: "http://petstore.swagger.io/oauth/dialog",
409
+ flow: "implicit",
410
+ scopes: {
411
+ "read:global": "read your pets",
412
+ "write:global": "modify pets in your account"
413
+ },
414
+ type: "oauth2"
415
+ }
416
+ },
417
+ swagger: "2.0",
418
+ tags: [
419
+ {
420
+ name: "Controller1"
421
+ },
422
+ {
423
+ name: "Controller2"
424
+ }
425
+ ]
426
+ });
427
+ });
428
+ it("should generate spec with given data", () => {
429
+ const result = generateSpec({
430
+ tokens: [],
431
+ spec: {
432
+ produces: ["application/json", "application/octet-stream", "application/xml"]
433
+ }
434
+ });
435
+
436
+ expect(result).toEqual({
437
+ swagger: "2.0",
438
+ info: {
439
+ title: "Api documentation",
440
+ version: "1.0.0"
441
+ },
442
+ produces: ["application/json", "application/octet-stream", "application/xml"],
443
+ consumes: ["application/json"]
444
+ });
445
+ });
446
+ it("should generated default spec", () => {
447
+ // @ts-ignore
448
+ const result = generateSpec({tokens: []});
449
+ expect(result).toEqual({
450
+ consumes: ["application/json"],
451
+ info: {
452
+ title: "Api documentation",
453
+ version: "1.0.0"
454
+ },
455
+ produces: ["application/json"],
456
+ swagger: "2.0"
457
+ });
458
+ });
459
+ it("PATH - should declare all schema correctly", async () => {
460
+ // WHEN
461
+ class Controller {
462
+ @OperationPath("GET", "/")
463
+ method(@In("path") @Name("basic") basic: string) {}
464
+ }
465
+
466
+ // THEN
467
+ const spec = generateSpec({
468
+ tokens: [{token: Controller}],
469
+ specType: SpecTypes.SWAGGER
470
+ });
471
+
472
+ expect(await validateSpec(spec)).toBe(true);
473
+ });
474
+ it("PATH - should declare all schema correctly with expression", async () => {
475
+ // WHEN
476
+ class Controller {
477
+ @OperationPath("GET", "/:id?")
478
+ method(@In("path") id: string) {}
479
+ }
480
+
481
+ // THEN
482
+ const spec = generateSpec({
483
+ tokens: [{token: Controller}],
484
+ specType: SpecTypes.SWAGGER
485
+ });
486
+
487
+ expect(await validateSpec(spec)).toBe(true);
488
+ expect(spec).toEqual({
489
+ consumes: ["application/json"],
490
+ info: {
491
+ title: "Api documentation",
492
+ version: "1.0.0"
493
+ },
494
+ paths: {
495
+ "/": {
496
+ get: {
497
+ operationId: "controllerMethod",
498
+ parameters: [],
499
+ responses: {
500
+ "200": {
501
+ description: "Success"
502
+ }
503
+ },
504
+ tags: ["Controller"]
505
+ }
506
+ },
507
+ "/{id}": {
508
+ get: {
509
+ operationId: "controllerMethodById",
510
+ parameters: [
511
+ {
512
+ in: "path",
513
+ name: "id",
514
+ required: true,
515
+ type: "string"
516
+ }
517
+ ],
518
+ responses: {
519
+ "200": {
520
+ description: "Success"
521
+ }
522
+ },
523
+ tags: ["Controller"]
524
+ }
525
+ }
526
+ },
527
+ produces: ["application/json"],
528
+ swagger: "2.0",
529
+ tags: [
530
+ {
531
+ name: "Controller"
532
+ }
533
+ ]
534
+ });
535
+ });
536
+ it("QUERY - should declare all schema correctly (query)", () => {
537
+ // WHEN
538
+ class Controller {
539
+ @OperationPath("GET", "/:id")
540
+ method(@In("query") @Name("basic") basic: string) {}
541
+ }
542
+
543
+ // THEN
544
+ const spec = generateSpec({tokens: [{token: Controller}], specType: SpecTypes.SWAGGER});
545
+
546
+ expect(spec).toEqual({
547
+ consumes: ["application/json"],
548
+ info: {
549
+ title: "Api documentation",
550
+ version: "1.0.0"
551
+ },
552
+ paths: {
553
+ "/{id}": {
554
+ get: {
555
+ operationId: "controllerMethod",
556
+ parameters: [
557
+ {
558
+ in: "path",
559
+ name: "id",
560
+ required: true,
561
+ type: "string"
562
+ },
563
+ {
564
+ in: "query",
565
+ name: "basic",
566
+ required: false,
567
+ type: "string"
568
+ }
569
+ ],
570
+ responses: {
571
+ "200": {
572
+ description: "Success"
573
+ }
574
+ },
575
+ tags: ["Controller"]
576
+ }
577
+ }
578
+ },
579
+ produces: ["application/json"],
580
+ swagger: "2.0",
581
+ tags: [
582
+ {
583
+ name: "Controller"
584
+ }
585
+ ]
586
+ });
587
+ });
588
+ it("QUERY - should declare all schema correctly (query - model)", () => {
589
+ // WHEN
590
+ class QueryModel {
591
+ @Property()
592
+ id: string;
593
+
594
+ @Property()
595
+ name: string;
596
+ }
597
+
598
+ class Controller {
599
+ @OperationPath("GET", "/:id")
600
+ method(@In("query") basic: QueryModel) {}
601
+ }
602
+
603
+ // THEN
604
+ const spec = generateSpec({
605
+ tokens: [{token: Controller}],
606
+ specType: SpecTypes.SWAGGER
607
+ });
608
+
609
+ expect(spec).toEqual({
610
+ consumes: ["application/json"],
611
+ info: {
612
+ title: "Api documentation",
613
+ version: "1.0.0"
614
+ },
615
+ paths: {
616
+ "/{id}": {
617
+ get: {
618
+ operationId: "controllerMethod",
619
+ parameters: [
620
+ {
621
+ in: "path",
622
+ name: "id",
623
+ required: true,
624
+ type: "string"
625
+ },
626
+ {
627
+ in: "query",
628
+ name: "id",
629
+ required: false,
630
+ type: "string"
631
+ },
632
+ {
633
+ in: "query",
634
+ name: "name",
635
+ required: false,
636
+ type: "string"
637
+ }
638
+ ],
639
+ responses: {
640
+ "200": {
641
+ description: "Success"
642
+ }
643
+ },
644
+ tags: ["Controller"]
645
+ }
646
+ }
647
+ },
648
+ produces: ["application/json"],
649
+ swagger: "2.0",
650
+ tags: [
651
+ {
652
+ name: "Controller"
653
+ }
654
+ ]
655
+ });
656
+ });
657
+ it("QUERY - should declare all schema correctly (query - array string)", () => {
658
+ // WHEN
659
+ class Controller {
660
+ @OperationPath("GET", "/:id")
661
+ method(@In("query") @Name("basic") basic: string[]) {}
662
+ }
663
+
664
+ // THEN
665
+ const spec = generateSpec({
666
+ tokens: [{token: Controller}],
667
+ specType: SpecTypes.SWAGGER
668
+ });
669
+
670
+ expect(spec).toEqual({
671
+ consumes: ["application/json"],
672
+ info: {
673
+ title: "Api documentation",
674
+ version: "1.0.0"
675
+ },
676
+ paths: {
677
+ "/{id}": {
678
+ get: {
679
+ operationId: "controllerMethod",
680
+ parameters: [
681
+ {
682
+ in: "path",
683
+ name: "id",
684
+ required: true,
685
+ type: "string"
686
+ },
687
+ {
688
+ collectionFormat: "multi",
689
+ in: "query",
690
+ items: {
691
+ type: "object"
692
+ },
693
+ name: "basic",
694
+ required: false,
695
+ type: "array"
696
+ }
697
+ ],
698
+ responses: {
699
+ "200": {
700
+ description: "Success"
701
+ }
702
+ },
703
+ tags: ["Controller"]
704
+ }
705
+ }
706
+ },
707
+ produces: ["application/json"],
708
+ swagger: "2.0",
709
+ tags: [
710
+ {
711
+ name: "Controller"
712
+ }
713
+ ]
714
+ });
715
+ });
716
+ it("QUERY - should declare all schema correctly (query - Map)", () => {
717
+ // WHEN
718
+ class Controller {
719
+ @OperationPath("GET", "/:id")
720
+ method(@In("query") @Name("basic") @CollectionOf(String) basic: Map<string, string>) {}
721
+ }
722
+
723
+ // THEN
724
+ const spec = generateSpec({
725
+ tokens: [{token: Controller}],
726
+ specType: SpecTypes.SWAGGER
727
+ });
728
+
729
+ expect(spec).toEqual({
730
+ consumes: ["application/json"],
731
+ info: {
732
+ title: "Api documentation",
733
+ version: "1.0.0"
734
+ },
735
+ paths: {
736
+ "/{id}": {
737
+ get: {
738
+ operationId: "controllerMethod",
739
+ parameters: [
740
+ {
741
+ in: "path",
742
+ name: "id",
743
+ required: true,
744
+ type: "string"
745
+ },
746
+ {
747
+ additionalProperties: {
748
+ type: "string"
749
+ },
750
+ in: "query",
751
+ name: "basic",
752
+ required: false,
753
+ type: "object"
754
+ }
755
+ ],
756
+ responses: {
757
+ "200": {
758
+ description: "Success"
759
+ }
760
+ },
761
+ tags: ["Controller"]
762
+ }
763
+ }
764
+ },
765
+ produces: ["application/json"],
766
+ swagger: "2.0",
767
+ tags: [
768
+ {
769
+ name: "Controller"
770
+ }
771
+ ]
772
+ });
773
+ });
774
+ it("BODY - should declare all schema correctly (model)", () => {
775
+ class MyModel {
776
+ @Property()
777
+ prop: string;
778
+ }
779
+
780
+ class Controller {
781
+ @Consumes("application/json")
782
+ @OperationPath("POST", "/")
783
+ method(@In("body") @Required() num: MyModel) {}
784
+ }
785
+
786
+ // THEN
787
+ const spec = generateSpec({tokens: [{token: Controller}], specType: SpecTypes.SWAGGER});
788
+
789
+ expect(spec).toEqual({
790
+ consumes: ["application/json"],
791
+ definitions: {
792
+ MyModel: {
793
+ properties: {
794
+ prop: {
795
+ type: "string"
796
+ }
797
+ },
798
+ type: "object"
799
+ }
800
+ },
801
+ info: {
802
+ title: "Api documentation",
803
+ version: "1.0.0"
804
+ },
805
+ paths: {
806
+ "/": {
807
+ post: {
808
+ consumes: ["application/json"],
809
+ operationId: "controllerMethod",
810
+ parameters: [
811
+ {
812
+ in: "body",
813
+ name: "body",
814
+ required: true,
815
+ schema: {
816
+ $ref: "#/definitions/MyModel"
817
+ }
818
+ }
819
+ ],
820
+ responses: {
821
+ "200": {
822
+ description: "Success"
823
+ }
824
+ },
825
+ tags: ["Controller"]
826
+ }
827
+ }
828
+ },
829
+ produces: ["application/json"],
830
+ swagger: "2.0",
831
+ tags: [
832
+ {
833
+ name: "Controller"
834
+ }
835
+ ]
836
+ });
837
+ });
838
+ it("BODY - should declare all schema correctly (Array - model)", () => {
839
+ // WHEN
840
+ class Product {
841
+ @Property()
842
+ title: string;
843
+ }
844
+
845
+ class Controller {
846
+ @OperationPath("POST", "/")
847
+ method(@In("body") @CollectionOf(Product) products: Product[]) {
848
+ return null;
849
+ }
850
+ }
851
+
852
+ // THEN
853
+ const spec = generateSpec({tokens: [{token: Controller}], specType: SpecTypes.SWAGGER});
854
+
855
+ expect(spec).toEqual({
856
+ consumes: ["application/json"],
857
+ definitions: {
858
+ Product: {
859
+ properties: {
860
+ title: {
861
+ type: "string"
862
+ }
863
+ },
864
+ type: "object"
865
+ }
866
+ },
867
+ info: {
868
+ title: "Api documentation",
869
+ version: "1.0.0"
870
+ },
871
+ paths: {
872
+ "/": {
873
+ post: {
874
+ consumes: ["application/json"],
875
+ operationId: "controllerMethod",
876
+ parameters: [
877
+ {
878
+ in: "body",
879
+ name: "body",
880
+ required: false,
881
+ schema: {
882
+ items: {
883
+ $ref: "#/definitions/Product"
884
+ },
885
+ type: "array"
886
+ }
887
+ }
888
+ ],
889
+ responses: {
890
+ "200": {
891
+ description: "Success"
892
+ }
893
+ },
894
+ tags: ["Controller"]
895
+ }
896
+ }
897
+ },
898
+ produces: ["application/json"],
899
+ swagger: "2.0",
900
+ tags: [
901
+ {
902
+ name: "Controller"
903
+ }
904
+ ]
905
+ });
906
+ });
907
+ it("BODY - should declare all schema correctly (Map - model)", () => {
908
+ // WHEN
909
+ class Product {
910
+ @Property()
911
+ title: string;
912
+ }
913
+
914
+ class Controller {
915
+ @OperationPath("POST", "/")
916
+ method(@In("body") @CollectionOf(Product) products: Map<string, Product>) {
917
+ return null;
918
+ }
919
+ }
920
+
921
+ // THEN
922
+ const spec = generateSpec({tokens: [{token: Controller}], specType: SpecTypes.SWAGGER});
923
+
924
+ expect(spec).toEqual({
925
+ consumes: ["application/json"],
926
+ definitions: {
927
+ Product: {
928
+ properties: {
929
+ title: {
930
+ type: "string"
931
+ }
932
+ },
933
+ type: "object"
934
+ }
935
+ },
936
+ info: {
937
+ title: "Api documentation",
938
+ version: "1.0.0"
939
+ },
940
+ paths: {
941
+ "/": {
942
+ post: {
943
+ consumes: ["application/json"],
944
+ operationId: "controllerMethod",
945
+ parameters: [
946
+ {
947
+ in: "body",
948
+ name: "body",
949
+ required: false,
950
+ schema: {
951
+ additionalProperties: {
952
+ $ref: "#/definitions/Product"
953
+ },
954
+ type: "object"
955
+ }
956
+ }
957
+ ],
958
+ responses: {
959
+ "200": {
960
+ description: "Success"
961
+ }
962
+ },
963
+ tags: ["Controller"]
964
+ }
965
+ }
966
+ },
967
+ produces: ["application/json"],
968
+ swagger: "2.0",
969
+ tags: [
970
+ {
971
+ name: "Controller"
972
+ }
973
+ ]
974
+ });
975
+ });
976
+ it("BODY - should declare all schema correctly (inline)", () => {
977
+ class Controller {
978
+ @Consumes("application/json")
979
+ @OperationPath("POST", "/")
980
+ method(@In("body") @Required() @Name("num") @Min(0) num: number, @In("body") @Required() @Name("test") @Min(0) num2: number) {}
981
+ }
982
+
983
+ // THEN
984
+ const spec = generateSpec({tokens: [{token: Controller}], specType: SpecTypes.SWAGGER});
985
+
986
+ expect(spec).toEqual({
987
+ consumes: ["application/json"],
988
+ info: {
989
+ title: "Api documentation",
990
+ version: "1.0.0"
991
+ },
992
+ paths: {
993
+ "/": {
994
+ post: {
995
+ consumes: ["application/json"],
996
+ operationId: "controllerMethod",
997
+ parameters: [
998
+ {
999
+ in: "body",
1000
+ name: "body",
1001
+ required: true,
1002
+ schema: {
1003
+ properties: {
1004
+ num: {
1005
+ minimum: 0,
1006
+ type: "number"
1007
+ },
1008
+ test: {
1009
+ minimum: 0,
1010
+ type: "number"
1011
+ }
1012
+ },
1013
+ required: ["num", "test"],
1014
+ type: "object"
1015
+ }
1016
+ }
1017
+ ],
1018
+ responses: {
1019
+ "200": {
1020
+ description: "Success"
1021
+ }
1022
+ },
1023
+ tags: ["Controller"]
1024
+ }
1025
+ }
1026
+ },
1027
+ produces: ["application/json"],
1028
+ swagger: "2.0",
1029
+ tags: [
1030
+ {
1031
+ name: "Controller"
1032
+ }
1033
+ ]
1034
+ });
1035
+ });
1036
+ it("BODY - should declare all schema correctly (Array - inline)", async () => {
1037
+ class Controller {
1038
+ @Consumes("application/json")
1039
+ @OperationPath("POST", "/")
1040
+ method(
1041
+ @In("body") @Required() @Name("num") @CollectionOf(Number) @Min(0) num: number[],
1042
+ @In("body") @Required() @Name("test") @Min(0) num2: number
1043
+ ) {}
1044
+ }
1045
+
1046
+ // THEN
1047
+ const spec = generateSpec({tokens: [{token: Controller}], specType: SpecTypes.SWAGGER});
1048
+ expect(await validateSpec(spec)).toBe(true);
1049
+ expect(spec).toEqual({
1050
+ consumes: ["application/json"],
1051
+ info: {
1052
+ title: "Api documentation",
1053
+ version: "1.0.0"
1054
+ },
1055
+ paths: {
1056
+ "/": {
1057
+ post: {
1058
+ consumes: ["application/json"],
1059
+ operationId: "controllerMethod",
1060
+ parameters: [
1061
+ {
1062
+ in: "body",
1063
+ name: "body",
1064
+ required: true,
1065
+ schema: {
1066
+ properties: {
1067
+ num: {
1068
+ items: {
1069
+ minimum: 0,
1070
+ type: "number"
1071
+ },
1072
+ type: "array"
1073
+ },
1074
+ test: {
1075
+ minimum: 0,
1076
+ type: "number"
1077
+ }
1078
+ },
1079
+ required: ["num", "test"],
1080
+ type: "object"
1081
+ }
1082
+ }
1083
+ ],
1084
+ responses: {
1085
+ "200": {
1086
+ description: "Success"
1087
+ }
1088
+ },
1089
+ tags: ["Controller"]
1090
+ }
1091
+ }
1092
+ },
1093
+ produces: ["application/json"],
1094
+ swagger: "2.0",
1095
+ tags: [
1096
+ {
1097
+ name: "Controller"
1098
+ }
1099
+ ]
1100
+ });
1101
+ });
1102
+ it("RESPONSE - should declare all schema correctly", () => {
1103
+ // WHEN
1104
+ @Name("AliasController")
1105
+ @Description("Class description")
1106
+ class Controller {
1107
+ @OperationPath("POST", "/")
1108
+ @(Returns(200, String).Description("description"))
1109
+ method() {}
1110
+ }
1111
+
1112
+ // THEN
1113
+ const spec = generateSpec({tokens: [{token: Controller}], specType: SpecTypes.SWAGGER});
1114
+
1115
+ expect(spec).toEqual({
1116
+ consumes: ["application/json"],
1117
+ info: {
1118
+ title: "Api documentation",
1119
+ version: "1.0.0"
1120
+ },
1121
+ paths: {
1122
+ "/": {
1123
+ post: {
1124
+ operationId: "aliasControllerMethod",
1125
+ parameters: [],
1126
+ produces: ["application/octet-stream"],
1127
+ responses: {
1128
+ "200": {
1129
+ description: "description",
1130
+ schema: {
1131
+ type: "string"
1132
+ }
1133
+ }
1134
+ },
1135
+ tags: ["AliasController"]
1136
+ }
1137
+ }
1138
+ },
1139
+ produces: ["application/json"],
1140
+ swagger: "2.0",
1141
+ tags: [
1142
+ {
1143
+ description: "Class description",
1144
+ name: "AliasController"
1145
+ }
1146
+ ]
1147
+ });
1148
+ });
1149
+ it("RESPONSE - should declare an Array of string", () => {
1150
+ // WHEN
1151
+ class Controller {
1152
+ @OperationPath("POST", "/")
1153
+ @(Returns(200, Array).Of(String).Description("description"))
1154
+ method() {}
1155
+ }
1156
+
1157
+ // THEN
1158
+ const spec = generateSpec({tokens: [{token: Controller}], specType: SpecTypes.SWAGGER});
1159
+
1160
+ expect(spec).toEqual({
1161
+ consumes: ["application/json"],
1162
+ info: {
1163
+ title: "Api documentation",
1164
+ version: "1.0.0"
1165
+ },
1166
+ paths: {
1167
+ "/": {
1168
+ post: {
1169
+ operationId: "controllerMethod",
1170
+ parameters: [],
1171
+ produces: ["application/json"],
1172
+ responses: {
1173
+ "200": {
1174
+ description: "description",
1175
+ schema: {
1176
+ items: {
1177
+ type: "string"
1178
+ },
1179
+ type: "array"
1180
+ }
1181
+ }
1182
+ },
1183
+ tags: ["Controller"]
1184
+ }
1185
+ }
1186
+ },
1187
+ produces: ["application/json"],
1188
+ swagger: "2.0",
1189
+ tags: [
1190
+ {
1191
+ name: "Controller"
1192
+ }
1193
+ ]
1194
+ });
1195
+ });
1196
+
1197
+ it("HEADERS - should declare a return type with headers", () => {
1198
+ // WHEN
1199
+ class Controller {
1200
+ @OperationPath("POST", "/")
1201
+ @(Returns(200, String)
1202
+ .Description("description")
1203
+ .Header("x-token", "token")
1204
+ .Header("x-header", {
1205
+ value: ""
1206
+ })
1207
+ .Examples({test: "Examples"})
1208
+ .Schema({
1209
+ minLength: 3
1210
+ }))
1211
+ method() {}
1212
+ }
1213
+
1214
+ // THEN
1215
+ const spec = generateSpec({tokens: [{token: Controller}], specType: SpecTypes.SWAGGER});
1216
+
1217
+ expect(spec).toEqual({
1218
+ consumes: ["application/json"],
1219
+ info: {
1220
+ title: "Api documentation",
1221
+ version: "1.0.0"
1222
+ },
1223
+ paths: {
1224
+ "/": {
1225
+ post: {
1226
+ operationId: "controllerMethod",
1227
+ parameters: [],
1228
+ produces: ["application/octet-stream"],
1229
+ responses: {
1230
+ "200": {
1231
+ description: "description",
1232
+ headers: {
1233
+ "x-header": {
1234
+ example: "",
1235
+ type: "string"
1236
+ },
1237
+ "x-token": {
1238
+ example: "token",
1239
+ type: "string"
1240
+ }
1241
+ },
1242
+ schema: {
1243
+ minLength: 3,
1244
+ type: "string"
1245
+ }
1246
+ }
1247
+ },
1248
+ tags: ["Controller"]
1249
+ }
1250
+ }
1251
+ },
1252
+ produces: ["application/json"],
1253
+ swagger: "2.0",
1254
+ tags: [
1255
+ {
1256
+ name: "Controller"
1257
+ }
1258
+ ]
1259
+ });
1260
+ });
1261
+ });
1262
+ });