@typespec/http-server-csharp 0.58.0-alpha.28-dev.4 → 0.58.0-alpha.29-dev.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 (279) hide show
  1. package/dist/src/cli/cli.js +118 -154
  2. package/dist/src/cli/cli.js.map +1 -1
  3. package/dist/src/components/controller-action/controller-action.d.ts +16 -0
  4. package/dist/src/components/controller-action/controller-action.d.ts.map +1 -0
  5. package/dist/src/components/controller-action/controller-action.js +251 -0
  6. package/dist/src/components/controller-action/controller-action.js.map +1 -0
  7. package/dist/src/components/controller-action/controller-action.test.d.ts +2 -0
  8. package/dist/src/components/controller-action/controller-action.test.d.ts.map +1 -0
  9. package/dist/src/components/controller-action/controller-action.test.js +106 -0
  10. package/dist/src/components/controller-action/controller-action.test.js.map +1 -0
  11. package/dist/src/components/controller-action/parameter-binding.d.ts +17 -0
  12. package/dist/src/components/controller-action/parameter-binding.d.ts.map +1 -0
  13. package/dist/src/components/controller-action/parameter-binding.js +77 -0
  14. package/dist/src/components/controller-action/parameter-binding.js.map +1 -0
  15. package/dist/src/components/controller-action/response-analysis.d.ts +10 -0
  16. package/dist/src/components/controller-action/response-analysis.d.ts.map +1 -0
  17. package/dist/src/components/controller-action/response-analysis.js +61 -0
  18. package/dist/src/components/controller-action/response-analysis.js.map +1 -0
  19. package/dist/src/components/controllers/controllers.d.ts +18 -0
  20. package/dist/src/components/controllers/controllers.d.ts.map +1 -0
  21. package/dist/src/components/controllers/controllers.js +64 -0
  22. package/dist/src/components/controllers/controllers.js.map +1 -0
  23. package/dist/src/components/controllers/controllers.test.d.ts +2 -0
  24. package/dist/src/components/controllers/controllers.test.d.ts.map +1 -0
  25. package/dist/src/components/controllers/controllers.test.js +84 -0
  26. package/dist/src/components/controllers/controllers.test.js.map +1 -0
  27. package/dist/src/components/csharp-file.d.ts +15 -0
  28. package/dist/src/components/csharp-file.d.ts.map +1 -0
  29. package/dist/src/components/csharp-file.js +28 -0
  30. package/dist/src/components/csharp-file.js.map +1 -0
  31. package/dist/src/components/enums/enums.d.ts +28 -0
  32. package/dist/src/components/enums/enums.d.ts.map +1 -0
  33. package/dist/src/components/enums/enums.js +158 -0
  34. package/dist/src/components/enums/enums.js.map +1 -0
  35. package/dist/src/components/enums/enums.test.d.ts +2 -0
  36. package/dist/src/components/enums/enums.test.d.ts.map +1 -0
  37. package/dist/src/components/enums/enums.test.js +112 -0
  38. package/dist/src/components/enums/enums.test.js.map +1 -0
  39. package/dist/src/components/interfaces/interfaces.d.ts +17 -0
  40. package/dist/src/components/interfaces/interfaces.d.ts.map +1 -0
  41. package/dist/src/components/interfaces/interfaces.js +153 -0
  42. package/dist/src/components/interfaces/interfaces.js.map +1 -0
  43. package/dist/src/components/interfaces/interfaces.test.d.ts +2 -0
  44. package/dist/src/components/interfaces/interfaces.test.d.ts.map +1 -0
  45. package/dist/src/components/interfaces/interfaces.test.js +76 -0
  46. package/dist/src/components/interfaces/interfaces.test.js.map +1 -0
  47. package/dist/src/components/models/anonymous-models.d.ts +9 -0
  48. package/dist/src/components/models/anonymous-models.d.ts.map +1 -0
  49. package/dist/src/components/models/anonymous-models.js +37 -0
  50. package/dist/src/components/models/anonymous-models.js.map +1 -0
  51. package/dist/src/components/models/error-models.d.ts +5 -0
  52. package/dist/src/components/models/error-models.d.ts.map +1 -0
  53. package/dist/src/components/models/error-models.js +84 -0
  54. package/dist/src/components/models/error-models.js.map +1 -0
  55. package/dist/src/components/models/model-helpers.d.ts +40 -0
  56. package/dist/src/components/models/model-helpers.d.ts.map +1 -0
  57. package/dist/src/components/models/model-helpers.js +239 -0
  58. package/dist/src/components/models/model-helpers.js.map +1 -0
  59. package/dist/src/components/models/models.d.ts +15 -0
  60. package/dist/src/components/models/models.d.ts.map +1 -0
  61. package/dist/src/components/models/models.js +234 -0
  62. package/dist/src/components/models/models.js.map +1 -0
  63. package/dist/src/components/models/models.test.d.ts +2 -0
  64. package/dist/src/components/models/models.test.d.ts.map +1 -0
  65. package/dist/src/components/models/models.test.js +151 -0
  66. package/dist/src/components/models/models.test.js.map +1 -0
  67. package/dist/src/components/project/csproj.d.ts +12 -0
  68. package/dist/src/components/project/csproj.d.ts.map +1 -0
  69. package/dist/src/components/project/csproj.js +35 -0
  70. package/dist/src/components/project/csproj.js.map +1 -0
  71. package/dist/src/components/project/csproj.test.d.ts +2 -0
  72. package/dist/src/components/project/csproj.test.d.ts.map +1 -0
  73. package/dist/src/components/project/csproj.test.js +42 -0
  74. package/dist/src/components/project/csproj.test.js.map +1 -0
  75. package/dist/src/components/project/launch-settings.d.ts +16 -0
  76. package/dist/src/components/project/launch-settings.d.ts.map +1 -0
  77. package/dist/src/components/project/launch-settings.js +73 -0
  78. package/dist/src/components/project/launch-settings.js.map +1 -0
  79. package/dist/src/components/project/launch-settings.test.d.ts +2 -0
  80. package/dist/src/components/project/launch-settings.test.d.ts.map +1 -0
  81. package/dist/src/components/project/launch-settings.test.js +48 -0
  82. package/dist/src/components/project/launch-settings.test.js.map +1 -0
  83. package/dist/src/components/project/program.d.ts +14 -0
  84. package/dist/src/components/project/program.d.ts.map +1 -0
  85. package/dist/src/components/project/program.js +75 -0
  86. package/dist/src/components/project/program.js.map +1 -0
  87. package/dist/src/components/project/program.test.d.ts +2 -0
  88. package/dist/src/components/project/program.test.d.ts.map +1 -0
  89. package/dist/src/components/project/program.test.js +50 -0
  90. package/dist/src/components/project/program.test.js.map +1 -0
  91. package/dist/src/components/render-root.d.ts +14 -0
  92. package/dist/src/components/render-root.d.ts.map +1 -0
  93. package/dist/src/components/render-root.js +122 -0
  94. package/dist/src/components/render-root.js.map +1 -0
  95. package/dist/src/components/request-models.d.ts +15 -0
  96. package/dist/src/components/request-models.d.ts.map +1 -0
  97. package/dist/src/components/request-models.js +97 -0
  98. package/dist/src/components/request-models.js.map +1 -0
  99. package/dist/src/components/scaffolding/documentation.d.ts +12 -0
  100. package/dist/src/components/scaffolding/documentation.d.ts.map +1 -0
  101. package/dist/src/{lib/doc.js → components/scaffolding/documentation.js} +56 -46
  102. package/dist/src/components/scaffolding/documentation.js.map +1 -0
  103. package/dist/src/components/scaffolding/mock-helpers.d.ts +10 -0
  104. package/dist/src/components/scaffolding/mock-helpers.d.ts.map +1 -0
  105. package/dist/src/components/scaffolding/mock-helpers.js +268 -0
  106. package/dist/src/components/scaffolding/mock-helpers.js.map +1 -0
  107. package/dist/src/components/scaffolding/mock-implementations.d.ts +14 -0
  108. package/dist/src/components/scaffolding/mock-implementations.d.ts.map +1 -0
  109. package/dist/src/components/scaffolding/mock-implementations.js +152 -0
  110. package/dist/src/components/scaffolding/mock-implementations.js.map +1 -0
  111. package/dist/src/components/scaffolding/mock-return-utils.d.ts +11 -0
  112. package/dist/src/components/scaffolding/mock-return-utils.d.ts.map +1 -0
  113. package/dist/src/components/scaffolding/mock-return-utils.js +62 -0
  114. package/dist/src/components/scaffolding/mock-return-utils.js.map +1 -0
  115. package/dist/src/components/scaffolding/mock-scaffolding.d.ts +6 -0
  116. package/dist/src/components/scaffolding/mock-scaffolding.d.ts.map +1 -0
  117. package/dist/src/components/scaffolding/mock-scaffolding.js +5 -0
  118. package/dist/src/components/scaffolding/mock-scaffolding.js.map +1 -0
  119. package/dist/src/components/scaffolding/mock-scaffolding.test.d.ts +2 -0
  120. package/dist/src/components/scaffolding/mock-scaffolding.test.d.ts.map +1 -0
  121. package/dist/src/components/scaffolding/mock-scaffolding.test.js +59 -0
  122. package/dist/src/components/scaffolding/mock-scaffolding.test.js.map +1 -0
  123. package/dist/src/components/serialization/base64-url-json-converter.d.ts +7 -0
  124. package/dist/src/components/serialization/base64-url-json-converter.d.ts.map +1 -0
  125. package/dist/src/components/serialization/base64-url-json-converter.js +58 -0
  126. package/dist/src/components/serialization/base64-url-json-converter.js.map +1 -0
  127. package/dist/src/components/serialization/constraint-attributes.d.ts +7 -0
  128. package/dist/src/components/serialization/constraint-attributes.d.ts.map +1 -0
  129. package/dist/src/components/serialization/constraint-attributes.js +556 -0
  130. package/dist/src/components/serialization/constraint-attributes.js.map +1 -0
  131. package/dist/src/components/serialization/http-service-exception-filter.d.ts +8 -0
  132. package/dist/src/components/serialization/http-service-exception-filter.d.ts.map +1 -0
  133. package/dist/src/components/serialization/http-service-exception-filter.js +71 -0
  134. package/dist/src/components/serialization/http-service-exception-filter.js.map +1 -0
  135. package/dist/src/components/serialization/json-converters.d.ts +7 -0
  136. package/dist/src/components/serialization/json-converters.d.ts.map +1 -0
  137. package/dist/src/components/serialization/json-converters.js +22 -0
  138. package/dist/src/components/serialization/json-converters.js.map +1 -0
  139. package/dist/src/components/serialization/json-converters.test.d.ts +2 -0
  140. package/dist/src/components/serialization/json-converters.test.d.ts.map +1 -0
  141. package/dist/src/components/serialization/json-converters.test.js +136 -0
  142. package/dist/src/components/serialization/json-converters.test.js.map +1 -0
  143. package/dist/src/components/serialization/json-serialization-provider.d.ts +7 -0
  144. package/dist/src/components/serialization/json-serialization-provider.d.ts.map +1 -0
  145. package/dist/src/components/serialization/json-serialization-provider.js +90 -0
  146. package/dist/src/components/serialization/json-serialization-provider.js.map +1 -0
  147. package/dist/src/components/serialization/time-span-duration-converter.d.ts +7 -0
  148. package/dist/src/components/serialization/time-span-duration-converter.d.ts.map +1 -0
  149. package/dist/src/components/serialization/time-span-duration-converter.js +44 -0
  150. package/dist/src/components/serialization/time-span-duration-converter.js.map +1 -0
  151. package/dist/src/components/serialization/unix-epoch-date-time-converter.d.ts +7 -0
  152. package/dist/src/components/serialization/unix-epoch-date-time-converter.d.ts.map +1 -0
  153. package/dist/src/components/serialization/unix-epoch-date-time-converter.js +71 -0
  154. package/dist/src/components/serialization/unix-epoch-date-time-converter.js.map +1 -0
  155. package/dist/src/components/type-expression/type-expression.d.ts +24 -0
  156. package/dist/src/components/type-expression/type-expression.d.ts.map +1 -0
  157. package/dist/src/components/type-expression/type-expression.js +223 -0
  158. package/dist/src/components/type-expression/type-expression.js.map +1 -0
  159. package/dist/src/components/type-expression/type-expression.test.d.ts +2 -0
  160. package/dist/src/components/type-expression/type-expression.test.d.ts.map +1 -0
  161. package/dist/src/components/type-expression/type-expression.test.js +207 -0
  162. package/dist/src/components/type-expression/type-expression.test.js.map +1 -0
  163. package/dist/src/context/emitter-options-context.d.ts +13 -0
  164. package/dist/src/context/emitter-options-context.d.ts.map +1 -0
  165. package/dist/src/context/emitter-options-context.js +15 -0
  166. package/dist/src/context/emitter-options-context.js.map +1 -0
  167. package/dist/src/diagnostics.d.ts +8 -0
  168. package/dist/src/diagnostics.d.ts.map +1 -0
  169. package/dist/src/diagnostics.js +164 -0
  170. package/dist/src/diagnostics.js.map +1 -0
  171. package/dist/src/emitter.d.ts +8 -0
  172. package/dist/src/emitter.d.ts.map +1 -0
  173. package/dist/src/emitter.js +180 -0
  174. package/dist/src/emitter.js.map +1 -0
  175. package/dist/src/{lib/index.d.ts → index.d.ts} +1 -1
  176. package/dist/src/index.d.ts.map +1 -0
  177. package/dist/src/{lib/index.js → index.js} +1 -1
  178. package/dist/src/index.js.map +1 -0
  179. package/dist/src/{lib/lib.d.ts → lib.d.ts} +4 -14
  180. package/dist/src/lib.d.ts.map +1 -0
  181. package/dist/src/lib.js +133 -0
  182. package/dist/src/lib.js.map +1 -0
  183. package/dist/src/output-writer.d.ts +11 -0
  184. package/dist/src/output-writer.d.ts.map +1 -0
  185. package/dist/src/output-writer.js +72 -0
  186. package/dist/src/output-writer.js.map +1 -0
  187. package/dist/src/service-discovery.d.ts +13 -0
  188. package/dist/src/service-discovery.d.ts.map +1 -0
  189. package/dist/src/service-discovery.js +93 -0
  190. package/dist/src/service-discovery.js.map +1 -0
  191. package/dist/src/service-resolution.d.ts +34 -0
  192. package/dist/src/service-resolution.d.ts.map +1 -0
  193. package/dist/src/service-resolution.js +244 -0
  194. package/dist/src/service-resolution.js.map +1 -0
  195. package/dist/src/testing/index.d.ts +2 -0
  196. package/dist/src/testing/index.d.ts.map +1 -0
  197. package/dist/src/testing/index.js +2 -0
  198. package/dist/src/testing/index.js.map +1 -0
  199. package/dist/src/utils/attributes.d.ts +8 -0
  200. package/dist/src/utils/attributes.d.ts.map +1 -0
  201. package/dist/src/utils/attributes.js +224 -0
  202. package/dist/src/utils/attributes.js.map +1 -0
  203. package/dist/src/utils/csharp-libs.d.ts +87 -0
  204. package/dist/src/utils/csharp-libs.d.ts.map +1 -0
  205. package/dist/src/utils/csharp-libs.js +90 -0
  206. package/dist/src/utils/csharp-libs.js.map +1 -0
  207. package/dist/src/utils/doc-comments.d.ts +2 -0
  208. package/dist/src/utils/doc-comments.d.ts.map +1 -0
  209. package/dist/src/utils/doc-comments.js +4 -0
  210. package/dist/src/utils/doc-comments.js.map +1 -0
  211. package/dist/src/utils/http-helpers.d.ts +19 -0
  212. package/dist/src/utils/http-helpers.d.ts.map +1 -0
  213. package/dist/src/utils/http-helpers.js +70 -0
  214. package/dist/src/utils/http-helpers.js.map +1 -0
  215. package/dist/src/utils/http-helpers.test.d.ts +2 -0
  216. package/dist/src/utils/http-helpers.test.d.ts.map +1 -0
  217. package/dist/src/utils/http-helpers.test.js +68 -0
  218. package/dist/src/utils/http-helpers.test.js.map +1 -0
  219. package/dist/src/utils/namespace-utils.d.ts +13 -0
  220. package/dist/src/utils/namespace-utils.d.ts.map +1 -0
  221. package/dist/src/utils/namespace-utils.js +55 -0
  222. package/dist/src/utils/namespace-utils.js.map +1 -0
  223. package/dist/src/utils/naming.d.ts +31 -0
  224. package/dist/src/utils/naming.d.ts.map +1 -0
  225. package/dist/src/utils/naming.js +87 -0
  226. package/dist/src/utils/naming.js.map +1 -0
  227. package/dist/src/utils/naming.test.d.ts +2 -0
  228. package/dist/src/utils/naming.test.d.ts.map +1 -0
  229. package/dist/src/utils/naming.test.js +86 -0
  230. package/dist/src/utils/naming.test.js.map +1 -0
  231. package/dist/src/utils/port.d.ts +5 -0
  232. package/dist/src/utils/port.d.ts.map +1 -0
  233. package/dist/src/utils/port.js +33 -0
  234. package/dist/src/utils/port.js.map +1 -0
  235. package/dist/src/utils/return-type-helpers.d.ts +8 -0
  236. package/dist/src/utils/return-type-helpers.d.ts.map +1 -0
  237. package/dist/src/utils/return-type-helpers.js +59 -0
  238. package/dist/src/utils/return-type-helpers.js.map +1 -0
  239. package/package.json +34 -26
  240. package/dist/src/lib/attributes.d.ts +0 -43
  241. package/dist/src/lib/attributes.d.ts.map +0 -1
  242. package/dist/src/lib/attributes.js +0 -422
  243. package/dist/src/lib/attributes.js.map +0 -1
  244. package/dist/src/lib/boilerplate.d.ts +0 -6
  245. package/dist/src/lib/boilerplate.d.ts.map +0 -1
  246. package/dist/src/lib/boilerplate.js +0 -792
  247. package/dist/src/lib/boilerplate.js.map +0 -1
  248. package/dist/src/lib/doc.d.ts +0 -5
  249. package/dist/src/lib/doc.d.ts.map +0 -1
  250. package/dist/src/lib/doc.js.map +0 -1
  251. package/dist/src/lib/index.d.ts.map +0 -1
  252. package/dist/src/lib/index.js.map +0 -1
  253. package/dist/src/lib/interfaces.d.ts +0 -183
  254. package/dist/src/lib/interfaces.d.ts.map +0 -1
  255. package/dist/src/lib/interfaces.js +0 -287
  256. package/dist/src/lib/interfaces.js.map +0 -1
  257. package/dist/src/lib/lib.d.ts.map +0 -1
  258. package/dist/src/lib/lib.js +0 -146
  259. package/dist/src/lib/lib.js.map +0 -1
  260. package/dist/src/lib/project.d.ts +0 -5
  261. package/dist/src/lib/project.d.ts.map +0 -1
  262. package/dist/src/lib/project.js +0 -101
  263. package/dist/src/lib/project.js.map +0 -1
  264. package/dist/src/lib/scaffolding.d.ts +0 -22
  265. package/dist/src/lib/scaffolding.d.ts.map +0 -1
  266. package/dist/src/lib/scaffolding.js +0 -461
  267. package/dist/src/lib/scaffolding.js.map +0 -1
  268. package/dist/src/lib/service.d.ts +0 -4
  269. package/dist/src/lib/service.d.ts.map +0 -1
  270. package/dist/src/lib/service.js +0 -1208
  271. package/dist/src/lib/service.js.map +0 -1
  272. package/dist/src/lib/type-helpers.d.ts +0 -44
  273. package/dist/src/lib/type-helpers.d.ts.map +0 -1
  274. package/dist/src/lib/type-helpers.js +0 -106
  275. package/dist/src/lib/type-helpers.js.map +0 -1
  276. package/dist/src/lib/utils.d.ts +0 -120
  277. package/dist/src/lib/utils.d.ts.map +0 -1
  278. package/dist/src/lib/utils.js +0 -1580
  279. package/dist/src/lib/utils.js.map +0 -1
@@ -1,792 +0,0 @@
1
- import { LibrarySourceFile } from "./interfaces.js";
2
- export const GeneratedFileHeader = `// Generated by @typespec/http-server-csharp
3
- // <auto-generated />`;
4
- export const GeneratedFileHeaderWithNullable = `// Generated by @typespec/http-server-csharp
5
- // <auto-generated />
6
- #nullable enable`;
7
- export function getSerializationSourceFiles(emitter) {
8
- const sourceFiles = [];
9
- sourceFiles.push(new LibrarySourceFile({
10
- filename: "TimeSpanDurationConverter.cs",
11
- emitter: emitter,
12
- getContents: getTimeSpanDurationConverter,
13
- }), new LibrarySourceFile({
14
- filename: "Base64UrlJsonConverter.cs",
15
- emitter: emitter,
16
- getContents: getBase64UrlJsonConverter,
17
- }), new LibrarySourceFile({
18
- filename: "UnixEpochDateTimeConverter.cs",
19
- emitter: emitter,
20
- getContents: getUnixEpochDateTimeConverter,
21
- }), new LibrarySourceFile({
22
- filename: "UnixEpochDateTimeOffsetConverter.cs",
23
- emitter: emitter,
24
- getContents: getUnixEpochDateTimeOffsetConverter,
25
- }), new LibrarySourceFile({
26
- filename: "NumericConstraintAttribute.cs",
27
- emitter: emitter,
28
- getContents: getNumericConstraintConverter,
29
- }), new LibrarySourceFile({
30
- filename: "StringConstraintAttribute.cs",
31
- emitter: emitter,
32
- getContents: getStringConstraintConverter,
33
- }), new LibrarySourceFile({
34
- filename: "ArrayConstraintAttribute.cs",
35
- emitter: emitter,
36
- getContents: getArrayConstraintConverter,
37
- }), new LibrarySourceFile({
38
- filename: "StringArrayConstraintAttribute.cs",
39
- emitter: emitter,
40
- getContents: getStringArrayConstraintConverter,
41
- }), new LibrarySourceFile({
42
- filename: "NumericArrayConstraintAttribute.cs",
43
- emitter: emitter,
44
- getContents: getNumericArrayConstraintConverter,
45
- }), new LibrarySourceFile({
46
- filename: "IJsonSerializationProvider.cs",
47
- emitter: emitter,
48
- getContents: getJsonProviderInterface,
49
- }), new LibrarySourceFile({
50
- filename: "JsonSerializationProvider.cs",
51
- emitter: emitter,
52
- getContents: getJsonProvider,
53
- }), new LibrarySourceFile({
54
- filename: "HttpServiceException.cs",
55
- emitter: emitter,
56
- getContents: getHttpServiceException,
57
- }));
58
- return sourceFiles;
59
- }
60
- function getNumericConstraintConverter() {
61
- return `${GeneratedFileHeaderWithNullable}
62
-
63
- using System.Numerics;
64
- using System.Text.Json;
65
- using System.Text.Json.Serialization;
66
-
67
- namespace TypeSpec.Helpers.JsonConverters
68
- {
69
- /// <summary>
70
- /// Provides numeric constraint validation at serialization time
71
- /// </summary>
72
- /// <typeparam name="T"></typeparam>
73
- public class NumericConstraintAttribute<T> : JsonConverterAttribute where T: struct, INumber<T>
74
- {
75
-
76
- T? _minValue = null, _maxValue = null;
77
- public NumericConstraintAttribute()
78
- {
79
- }
80
-
81
- /// <summary>
82
- /// Provides the minimum value
83
- /// </summary>
84
- public T MinValue { get { return _minValue.HasValue ? _minValue.Value : default(T); } set { _minValue = value; } }
85
- /// <summary>
86
- /// Provides the maximum allowed value
87
- /// </summary>
88
- public T MaxValue { get { return _maxValue.HasValue ? _maxValue.Value : default(T); } set { _maxValue = value; } }
89
- /// <summary>
90
- /// If true, then values greater than but not equal to the minimum value are allowed
91
- /// </summary>
92
- public bool MinValueExclusive { get; set; }
93
- /// <summary>
94
- /// If true, values less than, but not equal to the provided maximum are allowed
95
- /// </summary>
96
- public bool MaxValueExclusive { get; set; }
97
-
98
- public override JsonConverter? CreateConverter(Type typeToConvert)
99
- {
100
- return new NumericJsonConverter<T>(_minValue, _maxValue, MinValueExclusive, MaxValueExclusive);
101
- }
102
- }
103
-
104
- public class NumericJsonConverter<T> : JsonConverter<T> where T : struct, INumber<T>
105
- {
106
- string _rangeString;
107
- public NumericJsonConverter(T? minValue = null, T? maxValue = null, bool? minValueExclusive = false, bool? maxValueExclusive = false, JsonSerializerOptions? options = null)
108
- {
109
- MinValue = minValue;
110
- MaxValue = maxValue;
111
- MinValueExclusive = minValueExclusive.HasValue ? minValueExclusive.Value : false;
112
- MaxValueExclusive = maxValueExclusive.HasValue ? maxValueExclusive.Value : false;
113
- _rangeString = $"{(MinValue.HasValue ? (MinValueExclusive ? $"({MinValue}" : $"[{MinValue}") : $"[{typeof(T).Name}.Min")}, {(MaxValue.HasValue ? (MaxValueExclusive ? $"{MaxValue})" : $"{MaxValue}]") : $"{typeof(T).Name}.Max]")}";
114
- if ( options != null)
115
- {
116
- InnerConverter = options.GetConverter(typeof(T)) as JsonConverter<T>;
117
- }
118
- }
119
-
120
- protected T? MinValue { get; }
121
- protected bool MinValueExclusive { get; }
122
- protected T? MaxValue { get; }
123
-
124
- protected bool MaxValueExclusive { get; }
125
-
126
- private JsonConverter<T>? InnerConverter { get; set; }
127
-
128
- private JsonConverter<T> GetInnerConverter(JsonSerializerOptions options)
129
- {
130
- if (InnerConverter == null)
131
- {
132
- InnerConverter = (JsonConverter<T>)options.GetConverter(typeof(T));
133
- }
134
-
135
- return InnerConverter;
136
- }
137
- public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
138
- {
139
- var inner = GetInnerConverter(options);
140
- T candidate = inner.Read(ref reader, typeToConvert, options);
141
- ValidateRange(candidate);
142
- return candidate;
143
- }
144
-
145
- public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
146
- {
147
- ValidateRange(value);
148
- GetInnerConverter(options).Write(writer, value, options);
149
- }
150
-
151
- protected virtual void ValidateRange(T value)
152
- {
153
- if ((MinValue.HasValue && (value < MinValue.Value || (value == MinValue.Value && MinValueExclusive)))
154
- || (MaxValue.HasValue && (value > MaxValue.Value || (value == MaxValue.Value && MaxValueExclusive))))
155
- throw new JsonException($"{value} is outside the allowed range of {_rangeString}");
156
- }
157
- }
158
- }`;
159
- }
160
- function getStringConstraintConverter() {
161
- return `${GeneratedFileHeaderWithNullable}
162
-
163
- using System.Text.Json;
164
- using System.Text.Json.Serialization;
165
-
166
- namespace TypeSpec.Helpers.JsonConverters
167
- {
168
- /// <summary>
169
- /// Provides constraints for a string values property
170
- /// </summary>
171
- public class StringConstraint : JsonConverterAttribute
172
- {
173
- int? _minLength = null, _maxLength = null;
174
- public StringConstraint()
175
- {
176
- }
177
-
178
- /// <summary>
179
- /// The minimum length of the string
180
- /// </summary>
181
- public int MinLength { get { return _minLength.HasValue ? _minLength.Value : 0; } set { _minLength = value; } }
182
- /// <summary>
183
- /// The maximum length of the string
184
- /// </summary>
185
- public int MaxLength { get { return _maxLength.HasValue ? _maxLength.Value : 0; } set { _maxLength = value; } }
186
- /// <summary>
187
- /// The pattern that the string must match
188
- /// </summary>
189
- public string? Pattern { get; set; }
190
-
191
- public override JsonConverter? CreateConverter(Type typeToConvert)
192
- {
193
- return new StringJsonConverter(_minLength, _maxLength, Pattern);
194
- }
195
- }
196
-
197
- public class StringJsonConverter : JsonConverter<string>
198
- {
199
- public StringJsonConverter(int? minLength, int? maxLength, string? pattern, JsonSerializerOptions? options = null)
200
- {
201
- MinLength = minLength;
202
- MaxLength = maxLength;
203
- Pattern = pattern;
204
- if (options != null)
205
- {
206
- InnerConverter = options.GetConverter(typeof(string)) as JsonConverter<string>;
207
- }
208
- }
209
-
210
- protected int? MinLength { get; }
211
- protected int? MaxLength { get; }
212
- protected string? Pattern { get; }
213
-
214
- private JsonConverter<string>? InnerConverter { get; set; }
215
-
216
- private JsonConverter<string> GetInnerConverter(JsonSerializerOptions options)
217
- {
218
- if (InnerConverter == null)
219
- {
220
- InnerConverter = (JsonConverter<string>)options.GetConverter(typeof(string));
221
- }
222
-
223
- return InnerConverter;
224
- }
225
-
226
- public override bool CanConvert(Type typeToConvert)
227
- {
228
- return base.CanConvert(typeToConvert);
229
- }
230
-
231
- public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
232
- {
233
- var innerConverter = GetInnerConverter(options);
234
- string? candidate = innerConverter.Read(ref reader, typeToConvert, options);
235
- if (MinLength.HasValue && (candidate == null || candidate.Length < MinLength.Value))
236
- {
237
- throw new JsonException($"String length less than minimum length {MinLength.Value}");
238
- }
239
-
240
- if (candidate != null)
241
- {
242
- if (MaxLength.HasValue && candidate.Length > MaxLength.Value)
243
- {
244
- throw new JsonException($"String length greater than maximum length {MaxLength.Value}");
245
- }
246
-
247
- if (Pattern != null && !System.Text.RegularExpressions.Regex.IsMatch(candidate, Pattern))
248
- {
249
- throw new JsonException($"String does not match pattern {Pattern}");
250
- }
251
- }
252
-
253
- return candidate;
254
- }
255
-
256
- public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
257
- {
258
- if (MinLength.HasValue && (value == null || value.Length < MinLength.Value))
259
- {
260
- throw new JsonException($"String length less than minimum length {MinLength.Value}");
261
- }
262
-
263
- if (value != null)
264
- {
265
- if (MaxLength.HasValue && value.Length > MaxLength.Value)
266
- {
267
- throw new JsonException($"String length greater than maximum length {MaxLength.Value}");
268
- }
269
-
270
- if (Pattern != null && !System.Text.RegularExpressions.Regex.IsMatch(value, Pattern))
271
- {
272
- throw new JsonException($"String does not match pattern {Pattern}");
273
- }
274
-
275
- GetInnerConverter(options).Write(writer, value, options);
276
- }
277
- }
278
- }
279
- }`;
280
- }
281
- function getArrayConstraintConverter() {
282
- return `${GeneratedFileHeaderWithNullable}
283
-
284
- using System.Text.Json;
285
- using System.Text.Json.Serialization;
286
-
287
- namespace TypeSpec.Helpers.JsonConverters
288
- {
289
- /// <summary>
290
- /// Constrains the number of elements in an array
291
- /// </summary>
292
- /// <typeparam name="T">The element type of the array</typeparam>
293
- public class ArrayConstraintAttribute<T> : JsonConverterAttribute
294
- {
295
- int? _minItems = null, _maxItems = null;
296
- /// <summary>
297
- /// The smallest number of allowed items
298
- /// </summary>
299
- public int MinItems { get { return _minItems.HasValue ? _minItems.Value : 0; } set { _minItems = value; } }
300
- /// <summary>
301
- /// The largest number of allowed items
302
- /// </summary>
303
- public int MaxItems { get { return _maxItems.HasValue ? _maxItems.Value : 0; } set { _maxItems = value; } }
304
-
305
- public ArrayConstraintAttribute()
306
- {
307
-
308
- }
309
-
310
- public override JsonConverter? CreateConverter(Type typeToConvert)
311
- {
312
- if (typeof(ISet<T>).IsAssignableFrom(typeToConvert))
313
- {
314
- return new ConstrainedSetConverter<T>(_minItems, _maxItems);
315
- }
316
- else if (typeToConvert.IsArray && typeToConvert.GetElementType() == typeof(T))
317
- {
318
- return new ConstrainedStandardArrayConverter<T>(_minItems, _maxItems);
319
- }
320
- else
321
- {
322
- return new ConstrainedEnumerableConverter<T>(_minItems, _maxItems);
323
- }
324
- }
325
- }
326
-
327
- public abstract class ConstrainedCollectionConverter<T, TCollection> : JsonConverter<TCollection>
328
- {
329
- protected ConstrainedCollectionConverter(int? min, int? max)
330
- {
331
- _minItems = min;
332
- _maxItems = max;
333
- }
334
-
335
- protected int? _minItems, _maxItems;
336
- public JsonConverter<T>? InnerConverter { get; set; }
337
-
338
- public virtual Func<ConstrainedCollectionConverter<T, TCollection>, JsonSerializerOptions, JsonConverter<T>> InnerConverterFactory { get; set; } = ConverterHelpers.GetStandardInnerConverter<T, TCollection>;
339
-
340
- protected bool ValidateMin(int count)
341
- {
342
- return !_minItems.HasValue || count >= _minItems.Value;
343
- }
344
-
345
- protected bool ValidateMax(int count)
346
- {
347
- return !_maxItems.HasValue || count <= _maxItems.Value;
348
- }
349
-
350
- protected void ValidateRange(int count)
351
- {
352
- if (!ValidateMax(count) || !ValidateMin(count))
353
- {
354
- throw new JsonException($"Number of array elements not in range [{(_minItems.HasValue ? _minItems.Value : 0)}, {(_maxItems.HasValue ? _maxItems.Value : Array.MaxLength)}]");
355
- }
356
- }
357
- public override TCollection? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
358
- {
359
- var _innerConverter = InnerConverterFactory(this, options);
360
- if (reader.TokenType != JsonTokenType.StartArray) { throw new JsonException("Expected start of array"); }
361
- var list = new List<T>();
362
- int count = 0;
363
- while (reader.Read())
364
- {
365
- if (reader.TokenType == JsonTokenType.EndArray) { ValidateRange(count); break; }
366
- if (!ValidateMax(count)) { ValidateRange(count); break; }
367
- T value = _innerConverter.Read(ref reader, typeof(T), options)!;
368
- list.Add(value);
369
- count++;
370
- }
371
-
372
- return ConvertToCollection(list);
373
-
374
- }
375
-
376
- public override void Write(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options)
377
- {
378
- var _innerConverter = InnerConverterFactory(this, options);
379
- writer.WriteStartArray();
380
- foreach (var item in GetEnumerable(value))
381
- _innerConverter.Write(writer, item, options);
382
- writer.WriteEndArray();
383
- }
384
-
385
- protected abstract TCollection ConvertToCollection(List<T> list);
386
- protected abstract IEnumerable<T> GetEnumerable(TCollection collection);
387
- }
388
-
389
- public class ConstrainedEnumerableConverter<T> : ConstrainedCollectionConverter<T, IEnumerable<T>>
390
- {
391
- public ConstrainedEnumerableConverter(int? min, int? max) : base(min, max) { }
392
- protected override IEnumerable<T> ConvertToCollection(List<T> list) => list;
393
- protected override IEnumerable<T> GetEnumerable(IEnumerable<T> collection) => collection;
394
- }
395
-
396
- public class ConstrainedSetConverter<T> : ConstrainedCollectionConverter<T, ISet<T>>
397
- {
398
- public ConstrainedSetConverter(int? min, int? max) : base(min, max) { }
399
- protected override ISet<T> ConvertToCollection(List<T> list) => new HashSet<T>(list);
400
- protected override IEnumerable<T> GetEnumerable(ISet<T> collection) => collection;
401
- }
402
-
403
- public class ConstrainedStandardArrayConverter<T> : ConstrainedCollectionConverter<T, T[]>
404
- {
405
- public ConstrainedStandardArrayConverter(int? min, int? max) : base(min, max) { }
406
- protected override T[] ConvertToCollection(List<T> list) => list.ToArray();
407
- protected override IEnumerable<T> GetEnumerable(T[] collection) => collection;
408
- }
409
-
410
- internal static class ConverterHelpers
411
- {
412
- internal static JsonConverter<T> GetStandardInnerConverter<T, TCollection>(this ConstrainedCollectionConverter<T, TCollection> converter, JsonSerializerOptions options)
413
- {
414
- if (converter.InnerConverter == null)
415
- {
416
- converter.InnerConverter = (JsonConverter<T>)options.GetConverter(typeof(T));
417
- }
418
- return converter.InnerConverter;
419
- }
420
- }
421
- }`;
422
- }
423
- function getNumericArrayConstraintConverter() {
424
- return `${GeneratedFileHeaderWithNullable}
425
-
426
- using System.Numerics;
427
- using System.Text.Json;
428
- using System.Text.Json.Serialization;
429
-
430
- namespace TypeSpec.Helpers.JsonConverters
431
- {
432
- /// <summary>
433
- /// Constrains an array and the item types within it
434
- /// </summary>
435
- /// <typeparam name="T">The item type</typeparam>
436
- public class NumericArrayConstraintAttribute<T> : ArrayConstraintAttribute<T> where T: struct, INumber<T>
437
- {
438
- T? _minValue = null, _maxValue = null;
439
- public NumericArrayConstraintAttribute() : base()
440
- {
441
- }
442
-
443
- public T MinValue { get { return _minValue.HasValue ? _minValue.Value : default(T); } set { _minValue = value; } }
444
- public T MaxValue { get { return _maxValue.HasValue ? _maxValue.Value : default(T); } set { _maxValue = value; } }
445
- bool MinValueExclusive { get; set; }
446
- bool MaxValueExclusive { get; set; }
447
-
448
- public override JsonConverter? CreateConverter(Type typeToConvert)
449
- {
450
- var result = base.CreateConverter(typeToConvert);
451
- var resultSet = result as ConstrainedSetConverter<T>;
452
- if (resultSet != null) {
453
- resultSet.InnerConverterFactory = (c, o) => new NumericJsonConverter<T>(MinValue, MaxValue, MinValueExclusive, MaxValueExclusive, o);
454
- return resultSet;
455
- }
456
- var resultEnumerable = result as ConstrainedEnumerableConverter<T>;
457
- if (resultEnumerable != null) {
458
- resultEnumerable.InnerConverterFactory = (c, o) => new NumericJsonConverter<T>(MinValue, MaxValue, MinValueExclusive, MaxValueExclusive, o);
459
- return resultEnumerable;
460
- }
461
- var resultStandardArray = result as ConstrainedStandardArrayConverter<T>;
462
- if (resultStandardArray != null) {
463
- resultStandardArray.InnerConverterFactory = (c, o) => new NumericJsonConverter<T>(MinValue, MaxValue, MinValueExclusive, MaxValueExclusive, o);
464
- return resultStandardArray;
465
- }
466
- throw new InvalidOperationException($"Cannot create converter for {typeToConvert} with {this}");
467
- }
468
- }
469
- }`;
470
- }
471
- function getStringArrayConstraintConverter() {
472
- return `${GeneratedFileHeaderWithNullable}
473
-
474
- using System.Text.Json;
475
- using System.Text.Json.Serialization;
476
-
477
- namespace TypeSpec.Helpers.JsonConverters
478
- {
479
- /// <summary>
480
- /// Constrains an array of strings
481
- /// </summary>
482
- public class StringArrayConstraintAttribute : ArrayConstraintAttribute<string>
483
- {
484
- int? _minItemLength = null, _maxItemLength = null;
485
- public StringArrayConstraintAttribute() : base()
486
- {
487
- }
488
-
489
- public int MinItemLength { get { return _minItemLength.HasValue ? _minItemLength.Value : 0; } set { _minItemLength = value; } }
490
- public int MaxItemLength { get { return _maxItemLength.HasValue ? _maxItemLength.Value : 0; } set { _maxItemLength = value; } }
491
- public string? Pattern { get; set; }
492
-
493
- override public JsonConverter? CreateConverter(Type typeToConvert)
494
- {
495
- var result = base.CreateConverter(typeToConvert);
496
- var resultSet = result as ConstrainedSetConverter<string>;
497
- if (resultSet != null) {
498
- resultSet.InnerConverterFactory = (c, o) => new StringJsonConverter(MinItemLength, MaxItemLength, Pattern, o);
499
- return resultSet;
500
- }
501
-
502
- var resultEnumerable = result as ConstrainedEnumerableConverter<string>;
503
- if (resultEnumerable != null) {
504
- resultEnumerable.InnerConverterFactory = (c, o) => new StringJsonConverter(MinItemLength, MaxItemLength, Pattern, o);
505
- return resultEnumerable;
506
- }
507
-
508
- var resultStandardArray = result as ConstrainedStandardArrayConverter<string>;
509
- if (resultStandardArray != null) {
510
- resultStandardArray.InnerConverterFactory = (c, o) => new StringJsonConverter(MinItemLength, MaxItemLength, Pattern, o);
511
- return resultStandardArray;
512
- }
513
- throw new InvalidOperationException($"Cannot create converter for {typeToConvert} with {this}");
514
- }
515
- }
516
- }`;
517
- }
518
- function getTimeSpanDurationConverter() {
519
- return `${GeneratedFileHeaderWithNullable}
520
-
521
- using System.Text.Json;
522
- using System.Text.Json.Serialization;
523
- using System.Xml;
524
-
525
- namespace TypeSpec.Helpers.JsonConverters
526
- {
527
- /// <summary>
528
- /// Converts between Json duration and .Net TimeSpan
529
- /// </summary>
530
- public class TimeSpanDurationConverter : JsonConverter<TimeSpan>
531
- {
532
- public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
533
- {
534
- if (typeToConvert != typeof(TimeSpan))
535
- throw new ArgumentException($"Cannot apply converter {this.GetType().FullName} to type {typeToConvert.FullName}");
536
-
537
- var value = reader.GetString();
538
- if (string.IsNullOrWhiteSpace(value)) return TimeSpan.MinValue;
539
- return XmlConvert.ToTimeSpan(value);
540
- }
541
-
542
- public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options)
543
- {
544
- writer.WriteStringValue(XmlConvert.ToString(value));
545
- }
546
- }
547
- }
548
- `;
549
- }
550
- function getBase64UrlJsonConverter() {
551
- return `${GeneratedFileHeaderWithNullable}
552
-
553
- using System.Text.Json;
554
- using System.Text.Json.Serialization;
555
-
556
- namespace TypeSpec.Helpers.JsonConverters
557
- {
558
- /// <summary>
559
- /// System.Text.Json converter for the properties using Base64Url encoding
560
- /// </summary>
561
- public class Base64UrlJsonConverter : JsonConverter<byte[]>
562
- {
563
- /// <summary>
564
- /// Adds padding to the input
565
- /// </summary>
566
- /// <param name="input"> the input string </param>
567
- /// <returns> the padded string </returns>
568
- private static string Pad(string input)
569
- {
570
- var count = 3 - ((input.Length + 3) % 4);
571
- if (count == 0)
572
- {
573
- return input;
574
- }
575
- return $"{input}{new string('=', count)}";
576
- }
577
-
578
- public override byte[]? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
579
- {
580
- if (typeToConvert != typeof(byte[])) throw new ArgumentException($"Cannot apply converter {this.GetType().FullName} to type {typeToConvert.FullName}");
581
- var value = reader.GetString();
582
- if (string.IsNullOrWhiteSpace(value)) return null;
583
- return Convert.FromBase64String(Pad(value.Replace('-', '+').Replace('_', '/')));
584
- }
585
-
586
- public override void Write(Utf8JsonWriter writer, byte[] value, JsonSerializerOptions options)
587
- {
588
- writer.WriteStringValue(Convert.ToBase64String(value).TrimEnd('=').Replace('+', '-').Replace('/', '_'));
589
- }
590
- }
591
- }
592
- `;
593
- }
594
- function getUnixEpochDateTimeConverter() {
595
- return `${GeneratedFileHeaderWithNullable}
596
-
597
- using System.Text.Json;
598
- using System.Text.Json.Serialization;
599
-
600
- namespace TypeSpec.Helpers.JsonConverters
601
- {
602
- /// <summary>
603
- /// Converts between an integer timestamp and a .Net DateTime
604
- /// </summary>
605
- public sealed class UnixEpochDateTimeConverter : JsonConverter<DateTime>
606
- {
607
- static readonly DateTime s_epoch = new DateTime(1970, 1, 1, 0, 0, 0);
608
-
609
- public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
610
- {
611
- var formatted = reader.GetInt64()!;
612
- return s_epoch.AddMilliseconds(formatted);
613
- }
614
-
615
- public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
616
- {
617
- long unixTime = Convert.ToInt64((value - s_epoch).TotalMilliseconds);
618
- writer.WriteNumberValue(unixTime);
619
- }
620
- }
621
- }
622
- `;
623
- }
624
- function getUnixEpochDateTimeOffsetConverter() {
625
- return `${GeneratedFileHeaderWithNullable}
626
-
627
- using System.Text.Json;
628
- using System.Text.Json.Serialization;
629
-
630
- namespace TypeSpec.Helpers.JsonConverters
631
- {
632
- /// <summary>
633
- /// Converts between a Unix TimeStamp and a .Net DateTimeOffset
634
- /// </summary>
635
- public sealed class UnixEpochDateTimeOffsetConverter : JsonConverter<DateTimeOffset>
636
- {
637
- static readonly DateTimeOffset s_epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
638
-
639
-
640
- public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
641
- {
642
- var formatted = reader.GetInt64()!;
643
- return s_epoch.AddMilliseconds(formatted);
644
- }
645
-
646
- public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
647
- {
648
- long unixTime = Convert.ToInt64((value - s_epoch).TotalMilliseconds);
649
- writer.WriteNumberValue(unixTime);
650
- }
651
- }
652
- }
653
- `;
654
- }
655
- function getJsonProviderInterface() {
656
- return `${GeneratedFileHeaderWithNullable}
657
-
658
- using System.Text.Json;
659
- using System.Text.Json.Serialization;
660
-
661
- namespace TypeSpec.Helpers
662
- {
663
- /// <summary>
664
- /// Interface for Json serialization, suitable for providing a service in ASP.Net dependency injection
665
- /// </summary>
666
- public interface IJsonSerializationProvider
667
- {
668
- /// <summary>
669
- /// Serialize an object to a JSON string
670
- /// </summary>
671
- /// <typeparam name="T">The type of the object</typeparam>
672
- /// <param name="value">The object to serialize</param>
673
- /// <returns>A string representing the serialized object</returns>
674
- string Serialize<T>(T value);
675
-
676
- /// <summary>
677
- /// Create an object from a json string
678
- /// </summary>
679
- /// <typeparam name="T">The type of the object represented in the string</typeparam>
680
- /// <param name="value">The strign to deserialize</param>
681
- /// <returns>The deserialized object, or null</returns>
682
- T? Deserialize<T>(string value);
683
- }
684
- }
685
- `;
686
- }
687
- function getJsonProvider() {
688
- return `${GeneratedFileHeaderWithNullable}
689
-
690
- using System.Text.Json;
691
- using System.Text.Json.Serialization;
692
-
693
- namespace TypeSpec.Helpers
694
- {
695
- /// <summary>
696
- /// Standard implementation of IJsonSerializationProvider
697
- /// </summary>
698
- public class JsonSerializationProvider : IJsonSerializationProvider
699
- {
700
- /// <summary>
701
- /// The options to use for serialization
702
- /// </summary>
703
- public virtual JsonSerializerOptions Options { get; } = new JsonSerializerOptions
704
- {
705
- PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
706
- DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
707
- };
708
-
709
- /// <summary>
710
- /// Create an object from a json string
711
- /// </summary>
712
- /// <typeparam name="T">The type of the object represented in the string</typeparam>
713
- /// <param name="value">The strign to deserialize</param>
714
- /// <returns>The deserialized object, or null</returns>
715
- public virtual T? Deserialize<T>(string value)
716
- {
717
- return JsonSerializer.Deserialize<T>(value, Options);
718
- }
719
-
720
- /// <summary>
721
- /// Serialize an object to a JSON string
722
- /// </summary>
723
- /// <typeparam name="T">The type of the object</typeparam>
724
- /// <param name="value">The object to serialize</param>
725
- /// <returns>A string representing the serialized object</returns>
726
- public virtual string Serialize<T>(T value)
727
- {
728
- return JsonSerializer.Serialize(value, Options);
729
- }
730
- }
731
- }
732
- `;
733
- }
734
- function getHttpServiceException() {
735
- return `${GeneratedFileHeaderWithNullable}
736
-
737
- using Microsoft.AspNetCore.Mvc;
738
- using Microsoft.AspNetCore.Mvc.Filters;
739
-
740
- namespace TypeSpec.Helpers
741
- {
742
- /// <summary>
743
- /// Represents an HTTP response exception with a status code and optional value.
744
- /// </summary>
745
- public class HttpServiceException : Exception
746
- {
747
- /// <summary>
748
- /// Initializes a new instance of the HttpServiceException class.
749
- /// </summary>
750
- /// <param name="statusCode">The HTTP status code.</param>
751
- /// <param name="value">The optional value to include in the response.</param>
752
- public HttpServiceException(int statusCode, object? value = null, Dictionary<string, string>? headers = null) =>
753
- (StatusCode, Value, Headers) = (statusCode, value, headers ?? new Dictionary<string, string>());
754
-
755
- public int StatusCode { get; }
756
-
757
- public object? Value { get; }
758
-
759
- public Dictionary<string, string> Headers { get; }
760
- }
761
-
762
- /// <summary>
763
- /// An action filter that handles HttpServiceException and converts it to an HTTP response.
764
- /// </summary>
765
- public class HttpServiceExceptionFilter : IActionFilter, IOrderedFilter
766
- {
767
- public int Order => int.MaxValue - 10;
768
-
769
- public void OnActionExecuting(ActionExecutingContext context) { }
770
-
771
- public void OnActionExecuted(ActionExecutedContext context)
772
- {
773
- if (context.Exception is HttpServiceException httpServiceException)
774
- {
775
- foreach (var header in httpServiceException.Headers)
776
- {
777
- context.HttpContext.Response.Headers.Append(header.Key, header.Value.ToString());
778
- }
779
-
780
- context.Result = new ObjectResult(httpServiceException.Value)
781
- {
782
- StatusCode = httpServiceException.StatusCode
783
- };
784
-
785
- context.ExceptionHandled = true;
786
- }
787
- }
788
- }
789
- }
790
- `;
791
- }
792
- //# sourceMappingURL=boilerplate.js.map