From 56ea1e5c1a3ec3433ec1a47060276a85f4db3f43 Mon Sep 17 00:00:00 2001 From: Alex McKinney Date: Tue, 30 Jul 2024 12:42:06 -0400 Subject: [PATCH] (fix, csharp): map values are nullable --- .../csharp/codegen/src/context/CsharpTypeMapper.ts | 14 +++++++++----- generators/csharp/sdk/CHANGELOG.md | 6 ++++++ .../src/SeedExamples/Types/ExtendedMovie.cs | 2 +- .../examples/src/SeedExamples/Types/Movie.cs | 2 +- .../src/SeedTrace/Submission/GradedResponseV2.cs | 2 +- .../trace/src/SeedTrace/Submission/Scope.cs | 2 +- .../trace/src/SeedTrace/V2/Problem/TestCaseV2.cs | 2 +- .../src/SeedTrace/V2/V3/Problem/TestCaseV2.cs | 2 +- .../src/SeedExamples/Types/Types/ExtendedMovie.cs | 2 +- .../examples/src/SeedExamples/Types/Types/Movie.cs | 2 +- .../Optional/OptionalClient.cs | 2 +- .../SeedTrace/Submission/Types/GradedResponseV2.cs | 2 +- .../trace/src/SeedTrace/Submission/Types/Scope.cs | 2 +- .../src/SeedTrace/V2/Problem/Types/TestCaseV2.cs | 2 +- .../SeedTrace/V2/V3/Problem/Types/TestCaseV2.cs | 2 +- 15 files changed, 28 insertions(+), 18 deletions(-) diff --git a/generators/csharp/codegen/src/context/CsharpTypeMapper.ts b/generators/csharp/codegen/src/context/CsharpTypeMapper.ts index 536eb99cb26..6a5d539bc7c 100644 --- a/generators/csharp/codegen/src/context/CsharpTypeMapper.ts +++ b/generators/csharp/codegen/src/context/CsharpTypeMapper.ts @@ -63,11 +63,15 @@ export class CsharpTypeMapper { switch (container.type) { case "list": return Type.list(this.convert({ reference: container.list, unboxOptionals: true })); - case "map": - return Type.map( - this.convert({ reference: container.keyType }), - this.convert({ reference: container.valueType }) - ); + case "map": { + const key = this.convert({ reference: container.keyType }); + const value = this.convert({ reference: container.valueType }); + if (value.internalType.type === "object") { + // object map values should be nullable. + return Type.map(key, csharp.Type.optional(value)); + } + return Type.map(key, value); + } case "set": return Type.set(this.convert({ reference: container.set, unboxOptionals: true })); case "optional": diff --git a/generators/csharp/sdk/CHANGELOG.md b/generators/csharp/sdk/CHANGELOG.md index c96d297fef2..ad007eaa490 100644 --- a/generators/csharp/sdk/CHANGELOG.md +++ b/generators/csharp/sdk/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +- Improvement: `map` types are now generated as `Dictionary` types so they + can support explicit `null` values. Note that this does _not_ affect every `unknown` type to be an `object?` + since it would otherwise alter its required/optional characteristics. + ## [0.3.4 - 2024-07-30] - Improvement: Make datetime deserialization more lenient, and include milliseconds in datetime serialization. diff --git a/seed/csharp-model/examples/src/SeedExamples/Types/ExtendedMovie.cs b/seed/csharp-model/examples/src/SeedExamples/Types/ExtendedMovie.cs index f258fcc02dd..7c71948a159 100644 --- a/seed/csharp-model/examples/src/SeedExamples/Types/ExtendedMovie.cs +++ b/seed/csharp-model/examples/src/SeedExamples/Types/ExtendedMovie.cs @@ -37,5 +37,5 @@ public record ExtendedMovie public string? Book { get; set; } [JsonPropertyName("metadata")] - public Dictionary Metadata { get; set; } = new Dictionary(); + public Dictionary Metadata { get; set; } = new Dictionary(); } diff --git a/seed/csharp-model/examples/src/SeedExamples/Types/Movie.cs b/seed/csharp-model/examples/src/SeedExamples/Types/Movie.cs index 3a7751af15b..45307253698 100644 --- a/seed/csharp-model/examples/src/SeedExamples/Types/Movie.cs +++ b/seed/csharp-model/examples/src/SeedExamples/Types/Movie.cs @@ -34,5 +34,5 @@ public record Movie public string? Book { get; set; } [JsonPropertyName("metadata")] - public Dictionary Metadata { get; set; } = new Dictionary(); + public Dictionary Metadata { get; set; } = new Dictionary(); } diff --git a/seed/csharp-model/trace/src/SeedTrace/Submission/GradedResponseV2.cs b/seed/csharp-model/trace/src/SeedTrace/Submission/GradedResponseV2.cs index 8e77fd863df..03d19fe02e3 100644 --- a/seed/csharp-model/trace/src/SeedTrace/Submission/GradedResponseV2.cs +++ b/seed/csharp-model/trace/src/SeedTrace/Submission/GradedResponseV2.cs @@ -10,5 +10,5 @@ public record GradedResponseV2 public required string SubmissionId { get; set; } [JsonPropertyName("testCases")] - public Dictionary TestCases { get; set; } = new Dictionary(); + public Dictionary TestCases { get; set; } = new Dictionary(); } diff --git a/seed/csharp-model/trace/src/SeedTrace/Submission/Scope.cs b/seed/csharp-model/trace/src/SeedTrace/Submission/Scope.cs index febb8132c4c..72da4fd177d 100644 --- a/seed/csharp-model/trace/src/SeedTrace/Submission/Scope.cs +++ b/seed/csharp-model/trace/src/SeedTrace/Submission/Scope.cs @@ -7,5 +7,5 @@ namespace SeedTrace; public record Scope { [JsonPropertyName("variables")] - public Dictionary Variables { get; set; } = new Dictionary(); + public Dictionary Variables { get; set; } = new Dictionary(); } diff --git a/seed/csharp-model/trace/src/SeedTrace/V2/Problem/TestCaseV2.cs b/seed/csharp-model/trace/src/SeedTrace/V2/Problem/TestCaseV2.cs index 1ebf862e87c..78067b3eae7 100644 --- a/seed/csharp-model/trace/src/SeedTrace/V2/Problem/TestCaseV2.cs +++ b/seed/csharp-model/trace/src/SeedTrace/V2/Problem/TestCaseV2.cs @@ -14,7 +14,7 @@ public record TestCaseV2 public required object Implementation { get; set; } [JsonPropertyName("arguments")] - public Dictionary Arguments { get; set; } = new Dictionary(); + public Dictionary Arguments { get; set; } = new Dictionary(); [JsonPropertyName("expects")] public TestCaseExpects? Expects { get; set; } diff --git a/seed/csharp-model/trace/src/SeedTrace/V2/V3/Problem/TestCaseV2.cs b/seed/csharp-model/trace/src/SeedTrace/V2/V3/Problem/TestCaseV2.cs index 7a8aae6d6ff..09512994a65 100644 --- a/seed/csharp-model/trace/src/SeedTrace/V2/V3/Problem/TestCaseV2.cs +++ b/seed/csharp-model/trace/src/SeedTrace/V2/V3/Problem/TestCaseV2.cs @@ -14,7 +14,7 @@ public record TestCaseV2 public required object Implementation { get; set; } [JsonPropertyName("arguments")] - public Dictionary Arguments { get; set; } = new Dictionary(); + public Dictionary Arguments { get; set; } = new Dictionary(); [JsonPropertyName("expects")] public TestCaseExpects? Expects { get; set; } diff --git a/seed/csharp-sdk/examples/src/SeedExamples/Types/Types/ExtendedMovie.cs b/seed/csharp-sdk/examples/src/SeedExamples/Types/Types/ExtendedMovie.cs index f258fcc02dd..7c71948a159 100644 --- a/seed/csharp-sdk/examples/src/SeedExamples/Types/Types/ExtendedMovie.cs +++ b/seed/csharp-sdk/examples/src/SeedExamples/Types/Types/ExtendedMovie.cs @@ -37,5 +37,5 @@ public record ExtendedMovie public string? Book { get; set; } [JsonPropertyName("metadata")] - public Dictionary Metadata { get; set; } = new Dictionary(); + public Dictionary Metadata { get; set; } = new Dictionary(); } diff --git a/seed/csharp-sdk/examples/src/SeedExamples/Types/Types/Movie.cs b/seed/csharp-sdk/examples/src/SeedExamples/Types/Types/Movie.cs index 3a7751af15b..45307253698 100644 --- a/seed/csharp-sdk/examples/src/SeedExamples/Types/Types/Movie.cs +++ b/seed/csharp-sdk/examples/src/SeedExamples/Types/Types/Movie.cs @@ -34,5 +34,5 @@ public record Movie public string? Book { get; set; } [JsonPropertyName("metadata")] - public Dictionary Metadata { get; set; } = new Dictionary(); + public Dictionary Metadata { get; set; } = new Dictionary(); } diff --git a/seed/csharp-sdk/optional/src/SeedObjectsWithImports/Optional/OptionalClient.cs b/seed/csharp-sdk/optional/src/SeedObjectsWithImports/Optional/OptionalClient.cs index 3d1f522aa25..8a38a4e3578 100644 --- a/seed/csharp-sdk/optional/src/SeedObjectsWithImports/Optional/OptionalClient.cs +++ b/seed/csharp-sdk/optional/src/SeedObjectsWithImports/Optional/OptionalClient.cs @@ -14,7 +14,7 @@ public OptionalClient(RawClient client) _client = client; } - public async Task SendOptionalBodyAsync(Dictionary? request) + public async Task SendOptionalBodyAsync(Dictionary? request) { var response = await _client.MakeRequestAsync( new RawClient.JsonApiRequest diff --git a/seed/csharp-sdk/trace/src/SeedTrace/Submission/Types/GradedResponseV2.cs b/seed/csharp-sdk/trace/src/SeedTrace/Submission/Types/GradedResponseV2.cs index 8e77fd863df..03d19fe02e3 100644 --- a/seed/csharp-sdk/trace/src/SeedTrace/Submission/Types/GradedResponseV2.cs +++ b/seed/csharp-sdk/trace/src/SeedTrace/Submission/Types/GradedResponseV2.cs @@ -10,5 +10,5 @@ public record GradedResponseV2 public required string SubmissionId { get; set; } [JsonPropertyName("testCases")] - public Dictionary TestCases { get; set; } = new Dictionary(); + public Dictionary TestCases { get; set; } = new Dictionary(); } diff --git a/seed/csharp-sdk/trace/src/SeedTrace/Submission/Types/Scope.cs b/seed/csharp-sdk/trace/src/SeedTrace/Submission/Types/Scope.cs index febb8132c4c..72da4fd177d 100644 --- a/seed/csharp-sdk/trace/src/SeedTrace/Submission/Types/Scope.cs +++ b/seed/csharp-sdk/trace/src/SeedTrace/Submission/Types/Scope.cs @@ -7,5 +7,5 @@ namespace SeedTrace; public record Scope { [JsonPropertyName("variables")] - public Dictionary Variables { get; set; } = new Dictionary(); + public Dictionary Variables { get; set; } = new Dictionary(); } diff --git a/seed/csharp-sdk/trace/src/SeedTrace/V2/Problem/Types/TestCaseV2.cs b/seed/csharp-sdk/trace/src/SeedTrace/V2/Problem/Types/TestCaseV2.cs index 1ebf862e87c..78067b3eae7 100644 --- a/seed/csharp-sdk/trace/src/SeedTrace/V2/Problem/Types/TestCaseV2.cs +++ b/seed/csharp-sdk/trace/src/SeedTrace/V2/Problem/Types/TestCaseV2.cs @@ -14,7 +14,7 @@ public record TestCaseV2 public required object Implementation { get; set; } [JsonPropertyName("arguments")] - public Dictionary Arguments { get; set; } = new Dictionary(); + public Dictionary Arguments { get; set; } = new Dictionary(); [JsonPropertyName("expects")] public TestCaseExpects? Expects { get; set; } diff --git a/seed/csharp-sdk/trace/src/SeedTrace/V2/V3/Problem/Types/TestCaseV2.cs b/seed/csharp-sdk/trace/src/SeedTrace/V2/V3/Problem/Types/TestCaseV2.cs index 7a8aae6d6ff..09512994a65 100644 --- a/seed/csharp-sdk/trace/src/SeedTrace/V2/V3/Problem/Types/TestCaseV2.cs +++ b/seed/csharp-sdk/trace/src/SeedTrace/V2/V3/Problem/Types/TestCaseV2.cs @@ -14,7 +14,7 @@ public record TestCaseV2 public required object Implementation { get; set; } [JsonPropertyName("arguments")] - public Dictionary Arguments { get; set; } = new Dictionary(); + public Dictionary Arguments { get; set; } = new Dictionary(); [JsonPropertyName("expects")] public TestCaseExpects? Expects { get; set; }