swallowkit 1.0.0-beta.22 → 1.0.0-beta.24

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