swallowkit 1.0.0-beta.21 → 1.0.0-beta.23

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 (36) hide show
  1. package/README.ja.md +4 -4
  2. package/README.md +4 -4
  3. package/dist/cli/commands/dev.d.ts +11 -0
  4. package/dist/cli/commands/dev.d.ts.map +1 -1
  5. package/dist/cli/commands/dev.js +80 -7
  6. package/dist/cli/commands/dev.js.map +1 -1
  7. package/dist/cli/commands/init.d.ts.map +1 -1
  8. package/dist/cli/commands/init.js +17 -18
  9. package/dist/cli/commands/init.js.map +1 -1
  10. package/dist/cli/commands/scaffold.d.ts +0 -3
  11. package/dist/cli/commands/scaffold.d.ts.map +1 -1
  12. package/dist/cli/commands/scaffold.js +3 -172
  13. package/dist/cli/commands/scaffold.js.map +1 -1
  14. package/dist/cli/index.d.ts.map +1 -1
  15. package/dist/cli/index.js +37 -1
  16. package/dist/cli/index.js.map +1 -1
  17. package/dist/core/project/validation.js +2 -2
  18. package/dist/core/project/validation.js.map +1 -1
  19. package/dist/core/scaffold/model-parser.d.ts.map +1 -1
  20. package/dist/core/scaffold/model-parser.js +5 -6
  21. package/dist/core/scaffold/model-parser.js.map +1 -1
  22. package/dist/core/scaffold/native-schema-generator.d.ts +13 -0
  23. package/dist/core/scaffold/native-schema-generator.d.ts.map +1 -0
  24. package/dist/core/scaffold/native-schema-generator.js +667 -0
  25. package/dist/core/scaffold/native-schema-generator.js.map +1 -0
  26. package/package.json +1 -1
  27. package/src/__tests__/dev.test.ts +53 -1
  28. package/src/__tests__/model-parser.test.ts +44 -64
  29. package/src/__tests__/scaffold.test.ts +54 -26
  30. package/src/cli/commands/dev.ts +101 -8
  31. package/src/cli/commands/init.ts +26 -19
  32. package/src/cli/commands/scaffold.ts +3 -213
  33. package/src/cli/index.ts +4 -1
  34. package/src/core/project/validation.ts +2 -2
  35. package/src/core/scaffold/model-parser.ts +7 -7
  36. package/src/core/scaffold/native-schema-generator.ts +769 -0
@@ -0,0 +1,667 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.PYTHON_SCHEMA_CODEGEN_REQUIREMENT = exports.NSWAG_CONSOLECORE_VERSION = void 0;
37
+ exports.buildCSharpCodegenToolManifestSource = buildCSharpCodegenToolManifestSource;
38
+ exports.buildPythonCodegenRequirementsSource = buildPythonCodegenRequirementsSource;
39
+ exports.getCSharpSchemaModelPath = getCSharpSchemaModelPath;
40
+ exports.getCSharpSchemaOptionPath = getCSharpSchemaOptionPath;
41
+ exports.getPythonSchemaModelPath = getPythonSchemaModelPath;
42
+ exports.getCSharpNativeGeneratorArgs = getCSharpNativeGeneratorArgs;
43
+ exports.getPythonNativeGeneratorArgs = getPythonNativeGeneratorArgs;
44
+ exports.generateLanguageSchemaArtifacts = generateLanguageSchemaArtifacts;
45
+ const fs = __importStar(require("fs"));
46
+ const path = __importStar(require("path"));
47
+ const child_process_1 = require("child_process");
48
+ const model_parser_1 = require("./model-parser");
49
+ const openapi_generator_1 = require("./openapi-generator");
50
+ exports.NSWAG_CONSOLECORE_VERSION = "14.7.1";
51
+ exports.PYTHON_SCHEMA_CODEGEN_REQUIREMENT = "datamodel-code-generator>=0.44.0,<1.0.0";
52
+ const PYTHON_OUTPUT_MODEL_TYPE = "pydantic_v2.BaseModel";
53
+ function getMachineAwareStdio() {
54
+ return process.env.SWALLOWKIT_MACHINE_OUTPUT === "1" ? "pipe" : "inherit";
55
+ }
56
+ function canRun(command, args, cwd) {
57
+ const result = (0, child_process_1.spawnSync)(command, args, {
58
+ cwd,
59
+ stdio: "ignore",
60
+ });
61
+ return !result.error && result.status === 0;
62
+ }
63
+ async function runCommand(command, args, cwd, errorMessage) {
64
+ await new Promise((resolve, reject) => {
65
+ const child = (0, child_process_1.spawn)(command, args, {
66
+ cwd,
67
+ stdio: getMachineAwareStdio(),
68
+ });
69
+ child.on("close", (code) => {
70
+ if (code === 0) {
71
+ resolve();
72
+ return;
73
+ }
74
+ reject(new Error(`${errorMessage} (${command} ${args.join(" ")}) exited with code ${code}`));
75
+ });
76
+ child.on("error", (error) => reject(new Error(`${errorMessage}: ${error.message}`)));
77
+ });
78
+ }
79
+ function buildCSharpCodegenToolManifestSource() {
80
+ return `${JSON.stringify({
81
+ version: 1,
82
+ isRoot: true,
83
+ tools: {
84
+ "nswag.consolecore": {
85
+ version: exports.NSWAG_CONSOLECORE_VERSION,
86
+ commands: ["nswag"],
87
+ },
88
+ },
89
+ }, null, 2)}\n`;
90
+ }
91
+ function buildPythonCodegenRequirementsSource() {
92
+ return `${exports.PYTHON_SCHEMA_CODEGEN_REQUIREMENT}\n`;
93
+ }
94
+ function toSnakeCase(value) {
95
+ return value
96
+ .replace(/([a-z0-9])([A-Z])/g, "$1_$2")
97
+ .replace(/[-\s]+/g, "_")
98
+ .toLowerCase();
99
+ }
100
+ function toPascalIdentifier(value) {
101
+ if (value.includes("-") || value.includes("_")) {
102
+ return value
103
+ .split(/[-_]/)
104
+ .filter(Boolean)
105
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
106
+ .join("");
107
+ }
108
+ return value.charAt(0).toUpperCase() + value.slice(1);
109
+ }
110
+ function isDateLikeField(field) {
111
+ return field.type === "date" || (field.type === "string" && field.name.toLowerCase().endsWith("at"));
112
+ }
113
+ function getCSharpSchemaModelPath(outputDir, modelName) {
114
+ return path.join(outputDir, "src", "SwallowKitBackendModels", "Model", `${modelName}.cs`);
115
+ }
116
+ function getCSharpSchemaOptionPath(outputDir) {
117
+ return path.join(outputDir, "src", "SwallowKitBackendModels", "Client", "Option.cs");
118
+ }
119
+ function getPythonSchemaModelPath(outputDir, modelName) {
120
+ return path.join(outputDir, "backend_models", "models", `${toSnakeCase(modelName)}.py`);
121
+ }
122
+ function getCSharpNativeGeneratorArgs(specPath, outputPath) {
123
+ return [
124
+ "tool",
125
+ "run",
126
+ "nswag",
127
+ "openapi2csclient",
128
+ `/input:${specPath}`,
129
+ `/output:${outputPath}`,
130
+ "/namespace:SwallowKitBackendModels",
131
+ "/GenerateClientClasses:false",
132
+ "/GenerateClientInterfaces:false",
133
+ "/GenerateResponseClasses:false",
134
+ "/GenerateExceptionClasses:false",
135
+ "/GenerateDtoTypes:true",
136
+ "/GenerateNullableReferenceTypes:true",
137
+ "/GenerateOptionalPropertiesAsNullable:true",
138
+ "/JsonLibrary:SystemTextJson",
139
+ ];
140
+ }
141
+ function getPythonNativeGeneratorArgs(specPath, outputPath) {
142
+ return [
143
+ "-m",
144
+ "datamodel_code_generator",
145
+ "--input",
146
+ specPath,
147
+ "--input-file-type",
148
+ "openapi",
149
+ "--output",
150
+ outputPath,
151
+ "--output-model-type",
152
+ PYTHON_OUTPUT_MODEL_TYPE,
153
+ "--target-python-version",
154
+ "3.11",
155
+ "--disable-timestamp",
156
+ "--use-union-operator",
157
+ "--collapse-root-models",
158
+ ];
159
+ }
160
+ function getCSharpFieldBaseType(field) {
161
+ if (field.isNestedSchema && field.nestedModelName) {
162
+ return field.nestedModelName;
163
+ }
164
+ if (field.enumValues?.length) {
165
+ return `${toPascalIdentifier(field.name)}Enum`;
166
+ }
167
+ if (field.isArray) {
168
+ return `List<${getCSharpArrayElementType(field)}>`;
169
+ }
170
+ switch (field.type) {
171
+ case "string":
172
+ return isDateLikeField(field) ? "DateTime" : "string";
173
+ case "number":
174
+ return "decimal";
175
+ case "boolean":
176
+ return "bool";
177
+ case "date":
178
+ return "DateTime";
179
+ case "object":
180
+ return "Dictionary<string, object>";
181
+ default:
182
+ return "object";
183
+ }
184
+ }
185
+ function getCSharpArrayElementType(field) {
186
+ if (field.isNestedSchema && field.nestedModelName) {
187
+ return field.nestedModelName;
188
+ }
189
+ if (field.enumValues?.length) {
190
+ return `${toPascalIdentifier(field.name)}Enum`;
191
+ }
192
+ switch (field.type) {
193
+ case "string":
194
+ return isDateLikeField(field) ? "DateTime" : "string";
195
+ case "number":
196
+ return "decimal";
197
+ case "boolean":
198
+ return "bool";
199
+ case "date":
200
+ return "DateTime";
201
+ case "object":
202
+ return "Dictionary<string, object>";
203
+ default:
204
+ return "object";
205
+ }
206
+ }
207
+ function getCSharpPropertyType(field) {
208
+ const baseType = getCSharpFieldBaseType(field);
209
+ return field.isOptional ? `${baseType}?` : baseType;
210
+ }
211
+ function getCSharpOptionType(field) {
212
+ return `Option<${getCSharpPropertyType(field)}>`;
213
+ }
214
+ function generateLegacyCompatibleOptionSource() {
215
+ return `// <auto-generated>
216
+ // Minimal Option<T> for OpenAPI Generator model compatibility.
217
+ // Full client supporting files are excluded to avoid Polly version conflicts.
218
+ // </auto-generated>
219
+
220
+ #nullable enable
221
+
222
+ namespace SwallowKitBackendModels.Client
223
+ {
224
+ /// <summary>
225
+ /// A wrapper for nullable/optional properties generated by OpenAPI Generator.
226
+ /// Tracks whether a value has been explicitly set (distinguishing null from absent).
227
+ /// </summary>
228
+ public readonly struct Option<TValue>
229
+ {
230
+ /// <summary>Whether this option has been explicitly set.</summary>
231
+ public bool IsSet { get; }
232
+
233
+ /// <summary>The contained value (may be default if not set).</summary>
234
+ public TValue Value { get; }
235
+
236
+ /// <summary>Create an Option with an explicit value.</summary>
237
+ public Option(TValue value)
238
+ {
239
+ IsSet = true;
240
+ Value = value;
241
+ }
242
+
243
+ /// <summary>Implicit conversion from Option to its inner value.</summary>
244
+ public static implicit operator TValue(Option<TValue> option) => option.Value;
245
+ }
246
+ }
247
+ `;
248
+ }
249
+ function buildCSharpEnumMembers(values) {
250
+ return values
251
+ .map((value, index) => ` ${toPascalIdentifier(value)} = ${index + 1}`)
252
+ .join(",\n\n");
253
+ }
254
+ function buildCSharpEnumFromStringCases(field, nullable) {
255
+ const enumType = `${toPascalIdentifier(field.name)}Enum`;
256
+ return field.enumValues
257
+ .map((value) => ` if (value.Equals("${value}", StringComparison.Ordinal))\n return ${enumType}.${toPascalIdentifier(value)};`)
258
+ .join("\n\n") + (nullable ? `\n\n return null;` : `\n\n throw new NotImplementedException($"Could not convert value to type ${enumType}: '{value}'");`);
259
+ }
260
+ function buildCSharpEnumToJsonCases(field) {
261
+ const enumType = `${toPascalIdentifier(field.name)}Enum`;
262
+ return field.enumValues
263
+ .map((value) => ` if (value == ${enumType}.${toPascalIdentifier(value)})\n return "${value}";`)
264
+ .join("\n\n");
265
+ }
266
+ function generateLegacyCompatibleCSharpModelSource(model) {
267
+ const requiredFields = model.fields.filter((field) => !field.isOptional);
268
+ const optionalFields = model.fields.filter((field) => field.isOptional);
269
+ const constructorParams = [
270
+ ...requiredFields.map((field) => `${getCSharpFieldBaseType(field)} ${field.name}`),
271
+ ...optionalFields.map((field) => `${getCSharpOptionType(field)} ${field.name} = default`),
272
+ ].join(", ");
273
+ const constructorAssignments = [
274
+ ...requiredFields.map((field) => ` ${toPascalIdentifier(field.name)} = ${field.name};`),
275
+ ...optionalFields.map((field) => ` ${toPascalIdentifier(field.name)}Option = ${field.name};`),
276
+ " OnCreated();",
277
+ ].join("\n");
278
+ const enumBlocks = model.fields
279
+ .filter((field) => field.enumValues?.length)
280
+ .map((field) => {
281
+ const enumType = `${toPascalIdentifier(field.name)}Enum`;
282
+ return ` /// <summary>
283
+ /// Defines ${toPascalIdentifier(field.name)}
284
+ /// </summary>
285
+ [JsonConverter(typeof(JsonStringEnumConverter))]
286
+ public enum ${enumType}
287
+ {
288
+ ${buildCSharpEnumMembers(field.enumValues)}
289
+ }
290
+
291
+ public static ${enumType} ${enumType}FromString(string value)
292
+ {
293
+ ${buildCSharpEnumFromStringCases(field, false)}
294
+ }
295
+
296
+ public static ${enumType}? ${enumType}FromStringOrDefault(string value)
297
+ {
298
+ ${buildCSharpEnumFromStringCases(field, true)}
299
+ }
300
+
301
+ public static string ${enumType}ToJsonValue(${enumType}? value)
302
+ {
303
+ ${buildCSharpEnumToJsonCases(field)}
304
+
305
+ throw new NotImplementedException($"Value could not be handled: '{value}'");
306
+ }`;
307
+ })
308
+ .join("\n\n");
309
+ const propertyBlocks = model.fields
310
+ .map((field) => {
311
+ const propertyName = toPascalIdentifier(field.name);
312
+ const propertyType = getCSharpPropertyType(field);
313
+ if (!field.isOptional) {
314
+ return ` /// <summary>
315
+ /// Gets or Sets ${propertyName}
316
+ /// </summary>
317
+ [JsonPropertyName("${field.name}")]
318
+ public ${propertyType} ${propertyName} { get; set; }`;
319
+ }
320
+ return ` /// <summary>
321
+ /// Used to track the state of ${propertyName}
322
+ /// </summary>
323
+ [JsonIgnore]
324
+ [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
325
+ public ${getCSharpOptionType(field)} ${propertyName}Option { get; private set; }
326
+
327
+ /// <summary>
328
+ /// Gets or Sets ${propertyName}
329
+ /// </summary>
330
+ [JsonPropertyName("${field.name}")]
331
+ public ${propertyType} ${propertyName} { get { return this.${propertyName}Option; } set { this.${propertyName}Option = new(value); } }`;
332
+ })
333
+ .join("\n\n");
334
+ const toStringBody = model.fields
335
+ .map((field) => ` sb.Append(" ${toPascalIdentifier(field.name)}: ").Append(${toPascalIdentifier(field.name)}).Append("\\n");`)
336
+ .join("\n");
337
+ return `// <auto-generated>
338
+ /*
339
+ * ${model.name} API
340
+ *
341
+ * Generated from SwallowKit Zod model metadata.
342
+ *
343
+ * The version of the OpenAPI document: 1.0.0
344
+ * Generated by native SwallowKit schema compatibility layer
345
+ */
346
+
347
+ #nullable enable
348
+
349
+ using System;
350
+ using System.Collections.Generic;
351
+ using System.ComponentModel.DataAnnotations;
352
+ using System.Text;
353
+ using System.Text.Json.Serialization;
354
+ using SwallowKitBackendModels.Client;
355
+
356
+ namespace SwallowKitBackendModels.Model
357
+ {
358
+ /// <summary>
359
+ /// ${model.name}
360
+ /// </summary>
361
+ public partial class ${model.name} : IValidatableObject
362
+ {
363
+ [JsonConstructor]
364
+ public ${model.name}(${constructorParams})
365
+ {
366
+ ${constructorAssignments}
367
+ }
368
+
369
+ partial void OnCreated();
370
+ ${enumBlocks ? `\n\n${enumBlocks}` : ""}
371
+
372
+ ${propertyBlocks}
373
+
374
+ public override string ToString()
375
+ {
376
+ var sb = new StringBuilder();
377
+ sb.Append("class ${model.name} {\\n");
378
+ ${toStringBody}
379
+ sb.Append("}\\n");
380
+ return sb.ToString();
381
+ }
382
+
383
+ IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
384
+ {
385
+ yield break;
386
+ }
387
+ }
388
+ }
389
+ `;
390
+ }
391
+ function getPythonTypeName(field) {
392
+ if (field.isNestedSchema && field.nestedModelName) {
393
+ return field.nestedModelName;
394
+ }
395
+ if (field.isArray) {
396
+ return `List[${getPythonArrayElementType(field)}]`;
397
+ }
398
+ switch (field.type) {
399
+ case "string":
400
+ return isDateLikeField(field) ? "datetime" : "StrictStr";
401
+ case "number":
402
+ return "Union[StrictFloat, StrictInt]";
403
+ case "boolean":
404
+ return "StrictBool";
405
+ case "date":
406
+ return "datetime";
407
+ case "object":
408
+ return "Dict[str, Any]";
409
+ default:
410
+ return "Any";
411
+ }
412
+ }
413
+ function getPythonArrayElementType(field) {
414
+ if (field.isNestedSchema && field.nestedModelName) {
415
+ return field.nestedModelName;
416
+ }
417
+ switch (field.type) {
418
+ case "string":
419
+ return isDateLikeField(field) ? "datetime" : "StrictStr";
420
+ case "number":
421
+ return "Union[StrictFloat, StrictInt]";
422
+ case "boolean":
423
+ return "StrictBool";
424
+ case "date":
425
+ return "datetime";
426
+ case "object":
427
+ return "Dict[str, Any]";
428
+ default:
429
+ return "Any";
430
+ }
431
+ }
432
+ function buildPythonFieldDeclaration(field) {
433
+ const pythonName = toSnakeCase(field.name);
434
+ const typeName = field.isOptional ? `Optional[${getPythonTypeName(field)}]` : getPythonTypeName(field);
435
+ const aliasSuffix = pythonName !== field.name ? `, alias="${field.name}"` : "";
436
+ if (field.isOptional) {
437
+ return `${pythonName}: ${typeName} = Field(default=None${aliasSuffix})`;
438
+ }
439
+ if (aliasSuffix) {
440
+ return `${pythonName}: ${typeName} = Field(${aliasSuffix.slice(2)})`;
441
+ }
442
+ return `${pythonName}: ${typeName}`;
443
+ }
444
+ function buildPythonEnumValidators(model) {
445
+ return model.fields
446
+ .filter((field) => field.enumValues?.length)
447
+ .map((field) => {
448
+ const pythonName = toSnakeCase(field.name);
449
+ const enumSet = field.enumValues.map((value) => `'${value}'`).join(", ");
450
+ return ` @field_validator('${pythonName}')
451
+ def ${pythonName}_validate_enum(cls, value):
452
+ """Validates the enum"""
453
+ if value is None:
454
+ return value
455
+
456
+ if value not in set([${enumSet}]):
457
+ raise ValueError("must be one of enum values (${field.enumValues.map((value) => `'${value}'`).join(", ")})")
458
+ return value`;
459
+ })
460
+ .join("\n\n");
461
+ }
462
+ function buildPythonModelImports(model) {
463
+ const nestedImports = Array.from(new Set(model.fields
464
+ .filter((field) => field.isNestedSchema && field.nestedModelName)
465
+ .map((field) => field.nestedModelName)))
466
+ .map((modelName) => `from .${toSnakeCase(modelName)} import ${modelName}`)
467
+ .join("\n");
468
+ return nestedImports ? `${nestedImports}\n\n` : "";
469
+ }
470
+ function generateLegacyCompatiblePythonModelSource(model) {
471
+ const fieldDeclarations = model.fields.map((field) => ` ${buildPythonFieldDeclaration(field)}`).join("\n");
472
+ const propertyNames = model.fields.map((field) => `"${field.name}"`).join(", ");
473
+ const validators = buildPythonEnumValidators(model);
474
+ const dictAssignments = model.fields
475
+ .map((field) => ` "${field.name}": obj.get("${field.name}")`)
476
+ .join(",\n");
477
+ return `# coding: utf-8
478
+
479
+ """
480
+ ${model.name} API
481
+
482
+ Generated from SwallowKit Zod model metadata.
483
+
484
+ The version of the OpenAPI document: 1.0.0
485
+ Generated by native SwallowKit schema compatibility layer
486
+
487
+ Do not edit the class manually.
488
+ """ # noqa: E501
489
+
490
+
491
+ from __future__ import annotations
492
+ import pprint
493
+ import re # noqa: F401
494
+ import json
495
+
496
+ from datetime import datetime
497
+ from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictFloat, StrictInt, StrictStr, field_validator
498
+ from typing import Any, ClassVar, Dict, List, Optional, Set, Union
499
+ from typing_extensions import Self
500
+
501
+ ${buildPythonModelImports(model)}class ${model.name}(BaseModel):
502
+ """
503
+ ${model.name}
504
+ """ # noqa: E501
505
+ ${fieldDeclarations}
506
+ __properties: ClassVar[List[str]] = [${propertyNames}]
507
+ ${validators ? `\n\n${validators}` : ""}
508
+
509
+ model_config = ConfigDict(
510
+ populate_by_name=True,
511
+ validate_assignment=True,
512
+ protected_namespaces=(),
513
+ )
514
+
515
+
516
+ def to_str(self) -> str:
517
+ """Returns the string representation of the model using alias"""
518
+ return pprint.pformat(self.model_dump(by_alias=True))
519
+
520
+ def to_json(self) -> str:
521
+ """Returns the JSON representation of the model using alias"""
522
+ return json.dumps(self.to_dict())
523
+
524
+ @classmethod
525
+ def from_json(cls, json_str: str) -> Optional[Self]:
526
+ """Create an instance of ${model.name} from a JSON string"""
527
+ return cls.from_dict(json.loads(json_str))
528
+
529
+ def to_dict(self) -> Dict[str, Any]:
530
+ """Return the dictionary representation of the model using alias."""
531
+ excluded_fields: Set[str] = set([
532
+ ])
533
+
534
+ _dict = self.model_dump(
535
+ by_alias=True,
536
+ exclude=excluded_fields,
537
+ exclude_none=True,
538
+ )
539
+ return _dict
540
+
541
+ @classmethod
542
+ def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
543
+ """Create an instance of ${model.name} from a dict"""
544
+ if obj is None:
545
+ return None
546
+
547
+ if not isinstance(obj, dict):
548
+ return cls.model_validate(obj)
549
+
550
+ _obj = cls.model_validate({
551
+ ${dictAssignments}
552
+ })
553
+ return _obj
554
+
555
+
556
+ `;
557
+ }
558
+ function buildGeneratedPythonPackageInitSource(models) {
559
+ return models.map((model) => `from .models.${toSnakeCase(model.name)} import ${model.name}`).join("\n") + "\n";
560
+ }
561
+ function buildGeneratedPythonModelsInitSource(models) {
562
+ return models.map((model) => `from .${toSnakeCase(model.name)} import ${model.name}`).join("\n") + "\n";
563
+ }
564
+ function ensureCSharpCodegenProjectFiles(functionsRoot) {
565
+ const toolManifestPath = path.join(functionsRoot, ".config", "dotnet-tools.json");
566
+ fs.mkdirSync(path.dirname(toolManifestPath), { recursive: true });
567
+ if (!fs.existsSync(toolManifestPath)) {
568
+ fs.writeFileSync(toolManifestPath, buildCSharpCodegenToolManifestSource(), "utf-8");
569
+ }
570
+ }
571
+ function ensurePythonCodegenProjectFiles(functionsRoot) {
572
+ const requirementsPath = path.join(functionsRoot, "requirements.codegen.txt");
573
+ if (!fs.existsSync(requirementsPath)) {
574
+ fs.writeFileSync(requirementsPath, buildPythonCodegenRequirementsSource(), "utf-8");
575
+ }
576
+ return requirementsPath;
577
+ }
578
+ function getVirtualEnvPythonPath(venvDir) {
579
+ return process.platform === "win32"
580
+ ? path.join(venvDir, "Scripts", "python.exe")
581
+ : path.join(venvDir, "bin", "python");
582
+ }
583
+ function detectSystemPythonLauncher(functionsRoot) {
584
+ const candidates = [
585
+ { command: "python", argsPrefix: [] },
586
+ { command: "py", argsPrefix: ["-3"] },
587
+ { command: "python3", argsPrefix: [] },
588
+ ];
589
+ for (const candidate of candidates) {
590
+ if (canRun(candidate.command, [...candidate.argsPrefix, "--version"], functionsRoot)) {
591
+ return candidate;
592
+ }
593
+ }
594
+ throw new Error("Python 3.11+ is required to generate backend schema assets.\n" +
595
+ "Install Python and retry, or create functions/.codegen-venv manually.");
596
+ }
597
+ async function ensurePythonCodegenEnvironment(functionsRoot) {
598
+ const requirementsPath = ensurePythonCodegenProjectFiles(functionsRoot);
599
+ const venvDir = path.join(functionsRoot, ".codegen-venv");
600
+ const venvPython = getVirtualEnvPythonPath(venvDir);
601
+ if (!fs.existsSync(venvPython)) {
602
+ const launcher = detectSystemPythonLauncher(functionsRoot);
603
+ await runCommand(launcher.command, [...launcher.argsPrefix, "-m", "venv", venvDir], functionsRoot, "Failed to create the Python schema code generation virtual environment.");
604
+ }
605
+ if (!canRun(venvPython, ["-c", "import datamodel_code_generator"], functionsRoot)) {
606
+ await runCommand(venvPython, ["-m", "pip", "install", "--disable-pip-version-check", "-r", requirementsPath], functionsRoot, "Failed to install Python schema generation dependencies.");
607
+ }
608
+ return venvPython;
609
+ }
610
+ async function generateCSharpSchemaArtifacts(models, specPath, outputDir, functionsRoot) {
611
+ ensureCSharpCodegenProjectFiles(functionsRoot);
612
+ if (!canRun("dotnet", ["--version"], functionsRoot)) {
613
+ throw new Error("The .NET SDK is required to generate C# backend schema assets.\n" +
614
+ "Install the .NET 8 SDK and retry.");
615
+ }
616
+ const tempContractsPath = path.join(outputDir, ".native-temp", "Contracts.cs");
617
+ fs.mkdirSync(path.dirname(tempContractsPath), { recursive: true });
618
+ await runCommand("dotnet", ["tool", "restore"], functionsRoot, "Failed to restore the NSwag dotnet tool.");
619
+ await runCommand("dotnet", getCSharpNativeGeneratorArgs(specPath, tempContractsPath), functionsRoot, "NSwag failed to generate C# backend schema assets.");
620
+ fs.rmSync(path.dirname(tempContractsPath), { recursive: true, force: true });
621
+ const optionPath = getCSharpSchemaOptionPath(outputDir);
622
+ fs.mkdirSync(path.dirname(optionPath), { recursive: true });
623
+ fs.writeFileSync(optionPath, generateLegacyCompatibleOptionSource(), "utf-8");
624
+ for (const model of models) {
625
+ const modelPath = getCSharpSchemaModelPath(outputDir, model.name);
626
+ fs.mkdirSync(path.dirname(modelPath), { recursive: true });
627
+ fs.writeFileSync(modelPath, generateLegacyCompatibleCSharpModelSource(model), "utf-8");
628
+ }
629
+ }
630
+ async function generatePythonSchemaArtifacts(models, specPath, outputDir, functionsRoot) {
631
+ const pythonExecutable = await ensurePythonCodegenEnvironment(functionsRoot);
632
+ const tempModelsPath = path.join(outputDir, ".native-temp", "models.py");
633
+ fs.mkdirSync(path.dirname(tempModelsPath), { recursive: true });
634
+ await runCommand(pythonExecutable, getPythonNativeGeneratorArgs(specPath, tempModelsPath), functionsRoot, "datamodel-code-generator failed to generate Python backend schema assets.");
635
+ fs.rmSync(path.dirname(tempModelsPath), { recursive: true, force: true });
636
+ const packageRoot = path.join(outputDir, "backend_models");
637
+ const modelsRoot = path.join(packageRoot, "models");
638
+ fs.mkdirSync(modelsRoot, { recursive: true });
639
+ fs.writeFileSync(path.join(packageRoot, "__init__.py"), buildGeneratedPythonPackageInitSource(models), "utf-8");
640
+ fs.writeFileSync(path.join(modelsRoot, "__init__.py"), buildGeneratedPythonModelsInitSource(models), "utf-8");
641
+ for (const model of models) {
642
+ const modelPath = getPythonSchemaModelPath(outputDir, model.name);
643
+ fs.mkdirSync(path.dirname(modelPath), { recursive: true });
644
+ fs.writeFileSync(modelPath, generateLegacyCompatiblePythonModelSource(model), "utf-8");
645
+ }
646
+ }
647
+ async function generateLanguageSchemaArtifacts(models, rootModel, functionsDir, backendLanguage) {
648
+ console.log("\n🧬 Generating OpenAPI export and native schema assets...");
649
+ const projectRoot = process.cwd();
650
+ const functionsRoot = path.join(projectRoot, functionsDir);
651
+ const openApiDir = path.join(functionsRoot, "openapi");
652
+ fs.mkdirSync(openApiDir, { recursive: true });
653
+ const specPath = path.join(openApiDir, `${(0, model_parser_1.toKebabCase)(rootModel.name)}.openapi.json`);
654
+ fs.writeFileSync(specPath, (0, openapi_generator_1.generateOpenApiDocument)(models, rootModel), "utf-8");
655
+ console.log(`āœ… Created: ${specPath}`);
656
+ const outputDir = path.join(functionsRoot, "generated", backendLanguage === "csharp" ? "csharp-models" : "python-models");
657
+ fs.rmSync(outputDir, { recursive: true, force: true });
658
+ fs.mkdirSync(outputDir, { recursive: true });
659
+ if (backendLanguage === "csharp") {
660
+ await generateCSharpSchemaArtifacts(models, specPath, outputDir, functionsRoot);
661
+ }
662
+ else {
663
+ await generatePythonSchemaArtifacts(models, specPath, outputDir, functionsRoot);
664
+ }
665
+ console.log(`āœ… Generated ${backendLanguage} schema assets: ${outputDir}`);
666
+ }
667
+ //# sourceMappingURL=native-schema-generator.js.map