Add ATS nullable scalar support#17153
Conversation
Capture nullable scalar type-reference metadata during ATS scanning and project it into generated TypeScript and Python SDK types. Add focused scanner, snapshot, and runtime tests for nullable scalar DTO and context property scenarios. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add a concrete XML documentation example for nullable ATS type-reference metadata. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 17153Or
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 17153" |
There was a problem hiding this comment.
Pull request overview
Adds ATS-level nullability for scalar type references captured from C# member metadata, and projects that metadata into generated TypeScript and Python SDK output so string?/int?-style members are emitted as nullable types in those languages.
Changes:
- Extend
AtsTypeRefwith optional use-site nullability metadata (IsNullable) and omit it from JSON unless set. - Update ATS scanning to capture property nullability via
NullabilityInfoContextfor DTOs and exported context properties (getter/setter). - Update TypeScript/Python generators to emit
| null/| Nonefor nullable primitive/enum type references, and update snapshots/tests accordingly.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/Aspire.Hosting.RemoteHost.Tests/AtsCapabilityScannerTests.cs | Adds coverage for DTO property nullability flowing into AtsTypeRef.IsNullable. |
| tests/Aspire.Hosting.CodeGeneration.TypeScript.Tests/Snapshots/TwoPassScanningGeneratedAspire.verified.ts | Updates TS snapshot to reflect ` |
| tests/Aspire.Hosting.CodeGeneration.TypeScript.Tests/Snapshots/AtsGeneratedAspire.verified.ts | Updates TS snapshot for ATS-only scan scenario to reflect nullable scalar emission. |
| tests/Aspire.Hosting.CodeGeneration.TypeScript.JsTests/tests/nullableProperties.test.ts | Adds runtime JS test coverage for nullable scalar getter/setter behavior (null round-tripping). |
| tests/Aspire.Hosting.CodeGeneration.Python.Tests/Snapshots/TwoPassScanningGeneratedAspire.verified.py | Updates Python snapshot to reflect ` |
| tests/Aspire.Hosting.CodeGeneration.Python.Tests/Snapshots/AtsGeneratedAspire.verified.py | Updates Python ATS-only snapshot to reflect ` |
| tests/Aspire.Hosting.CodeGeneration.Python.Tests/AtsPythonCodeGeneratorTests.cs | Adds targeted assertions for nullable scalar emission in generated Python. |
| src/Aspire.TypeSystem/AtsCapabilityInfo.cs | Introduces AtsTypeRef.IsNullable (nullable scalar metadata) and adjusts JSON serialization for nullability fields. |
| src/Aspire.Hosting.RemoteHost/AtsCapabilityScanner.cs | Uses NullabilityInfoContext to stamp use-site nullability onto DTO/context property type refs (incl. getter/setter). |
| src/Aspire.Hosting.CodeGeneration.TypeScript/AtsTypeScriptCodeGenerator.cs | Applies ` |
| src/Aspire.Hosting.CodeGeneration.Python/AtsPythonCodeGenerator.cs | Applies ` |
| /// <summary> | ||
| /// Gets or sets whether this type reference accepts a JSON null value at its current use site. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Nullability is attached to the type reference as it appears in a capability or DTO property. | ||
| /// Nested element, key, and value type nullability is only represented when those nested | ||
| /// references were scanned from member metadata that exposes nullability information. | ||
| /// For example, a DTO property declared as <code>string?</code> produces a nullable string | ||
| /// type reference, while the same CLR <see cref="string" /> type on a non-nullable property does not. | ||
| /// </remarks> | ||
| [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] | ||
| public bool? IsNullable { get; init; } | ||
|
|
| Assert.Contains("OptionalField: str | None", aspirePy); | ||
| Assert.Contains("def description(self) -> str | None:", aspirePy); | ||
| Assert.Contains("def description(self, value: str | None) -> None:", aspirePy); | ||
| Assert.Contains("Name: str", aspirePy); | ||
| Assert.Contains("def name(self) -> str:", aspirePy); | ||
| Assert.Contains("def name(self, value: str) -> None:", aspirePy); |
|
🎬 CLI E2E Test Recordings — 85 recordings uploaded (commit View all recordings
📹 Recordings uploaded automatically from CI run #25947285922 |
JamesNK
left a comment
There was a problem hiding this comment.
LGTM — clean implementation of nullable scalar metadata propagation through the ATS pipeline. Two minor comments: (1) unused NullableScalarContext test type that could have a companion scanner test, (2) serialization behavior change on existing AtsParameterInfo.IsNullable. Neither is blocking.
| } | ||
|
|
||
| [AspireExport(ExposeProperties = true)] | ||
| private sealed class NullableScalarContext |
There was a problem hiding this comment.
NullableScalarContext is defined here with [AspireExport(ExposeProperties = true)] but no test validates its scanned capabilities. ScanAssembly_DtoNullableScalarProperties_SetTypeRefNullability only covers NullableScalarDto. The context property scanning path (CreateContextTypeCapabilities) has its own WithNullability calls for getter/setter type refs — consider adding a companion test that validates the getter returns a nullable AtsTypeRef and the setter's value parameter has IsNullable == true.
The feature is covered by snapshot tests via TestCallbackContext/TestEnvironmentContext, so this is a test robustness gap rather than a correctness issue.
| /// <summary> | ||
| /// Gets or sets whether this parameter is nullable. | ||
| /// </summary> | ||
| [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] |
There was a problem hiding this comment.
Adding [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] to this pre-existing bool property changes the wire format: "isNullable": false was previously emitted and will now be omitted. Consumers that treat a missing field as its default (false) are unaffected, but any consumer doing explicit presence-checking on the JSON key could break. Worth confirming no downstream consumer relies on the explicit presence of this field.
Description
Generated polyglot SDKs currently lose C# nullable scalar property metadata, so nullable properties such as
string?are emitted as non-nullable SDK types. This adds ATS type-reference nullability at member use sites and projects that metadata into generated TypeScript and Python scalar output.The scanner now captures DTO and context property nullability with
NullabilityInfoContext. TypeScript emits| nulland Python emits| Nonefor nullable primitive/enum type references, while Go, Java, and Rust behavior is intentionally unchanged. The metadata only serializes when nullable to avoid broad snapshot churn.Related issue: N/A
Checklist
<remarks />and<code />elements on your triple slash comments?