Skip to content

[csharp] Support null types#23260

Open
devhl-labs wants to merge 2 commits intoOpenAPITools:masterfrom
devhl-labs:devhl/5180-null-type
Open

[csharp] Support null types#23260
devhl-labs wants to merge 2 commits intoOpenAPITools:masterfrom
devhl-labs:devhl/5180-null-type

Conversation

@devhl-labs
Copy link
Contributor

@devhl-labs devhl-labs commented Mar 15, 2026

Related to #19258 and #5180

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Adds C# generator support for OAS 3.1 type: null by emitting nullable object properties. Adds a NullTypes sample and enables it in CI.

  • New Features

    • Map properties with openApiType: 'null' to object and mark them nullable in AbstractCSharpCodegen.
    • Add NullTypes sample (bin/configs/csharp-generichost-latest-nullTypes.yaml, generated client, tests).
    • Update .github/workflows/samples-dotnet10.yaml to build and test the new sample.
  • Refactors

    • Remove unnecessary code in the C# generator’s model property post-processing.

Written for commit c92b00a. Summary will update on new commits.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

23 issues found across 64 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Model/ColorOrNull.cs">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Model/ColorOrNull.cs:111">
P2: ColorOrNullJsonConverter only accepts/writes object-shaped JSON, so valid string or null representations for this union will throw or serialize incorrectly.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Api/DefaultApi.cs">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Api/DefaultApi.cs:236">
P2: Path concatenation can produce a double-slash when BaseAddress.AbsolutePath ends with '/', resulting in a different URL path (e.g., /api//widgets/{id}) that may not match server routes.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/appveyor.yml">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/appveyor.yml:3">
P2: AppVeyor image is pinned to Visual Studio 2019, which won’t have the .NET 10 SDK required by this ‘latest’ sample (net10.0 targets). CI will fail to build/test unless a newer image or SDK install step is used.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Model/ShapeOrNull.cs">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Model/ShapeOrNull.cs:152">
P2: Nullable `Area` is treated as non-nullable in the converter: JSON nulls are read but then rejected, and serialization dereferences `AreaOption.Value!.Value` when `Area` is explicitly null. This makes `area: null` payloads fail despite `double?` being nullable.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Client/DateTimeJsonConverter.cs">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Client/DateTimeJsonConverter.cs:27">
P2: RFC3339 allows fractional seconds with 1 or more digits, but the parser only accepts up to 7. Valid RFC3339 timestamps with 8+ fractional digits will fail TryParseExact and throw NotSupportedException.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/README.md">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/README.md:95">
P2: README documents obsolete response types (`ApiResponse<T>`, `T`, `TOrDefault`) that do not match the sample’s generated interface-based API responses, causing misleading integration guidance.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Client/DateOnlyNullableJsonConverter.cs">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Client/DateOnlyNullableJsonConverter.cs:49">
P2: Invalid date payloads are reported with `NotSupportedException` instead of `JsonException`, misclassifying deserialization errors.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Client/ClientUtils.cs">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Client/ClientUtils.cs:56">
P2: `TryDeserialize` reports failure when deserializing a valid JSON `null` because it returns `result != null`. `JsonSerializer.Deserialize<T>` can succeed and return `null`, so `TryDeserialize` returns `false` for valid `null` payloads, breaking nullable/null-type handling.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/docs/scripts/git_push.sh">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/docs/scripts/git_push.sh:38">
P2: Embedding GIT_TOKEN in the remote URL persists the token in `.git/config`, leaving a long‑lived plaintext credential that can be exposed by git tooling. Prefer credential helpers or prompt-based auth instead of storing tokens in remote URLs.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Model/Color.cs">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Model/Color.cs:168">
P2: The Color JSON converter accepts array payloads and silently returns an empty Color instance because it only processes property names at object depth. This masks invalid input for an object schema instead of rejecting it.</violation>

<violation number="2" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Model/Color.cs:208">
P1: Nullable RGB fields are treated as non-nullable in custom JSON conversion, causing explicit JSON nulls to throw during read/write.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/docs/models/ShapeOrNull.md">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/docs/models/ShapeOrNull.md:7">
P3: ShapeOrNull is marked nullable in the OpenAPI schema, but the generated docs only describe the object fields and never mention that `null` is a valid value, which misleads consumers.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/docs/scripts/git_push.ps1">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/docs/scripts/git_push.ps1:22">
P2: Script only checks for any remote but later assumes a valid `origin`; if a different remote exists or `origin` is missing, `git pull/push origin` will fail or use the wrong repo.</violation>

<violation number="2" location="samples/client/petstore/csharp/generichost/latest/NullTypes/docs/scripts/git_push.ps1:29">
P2: `git pull --ff-only` failure is misclassified as missing repository, causing incorrect repo-creation/error paths for valid first-push states.</violation>

<violation number="3" location="samples/client/petstore/csharp/generichost/latest/NullTypes/docs/scripts/git_push.ps1:33">
P2: Auto-creating GitHub repo ignores `GitUserId`, which can create the repo under a different owner than the configured remote URL.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Model/Shape.cs">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Model/Shape.cs:152">
P2: ShapeJsonConverter still treats `area` as non-nullable: it throws on deserializing explicit null and will throw when serializing a set-but-null AreaOption, so null-type support is broken for this property.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Client/DateTimeNullableJsonConverter.cs">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Client/DateTimeNullableJsonConverter.cs:55">
P2: Malformed non-null date strings are silently deserialized as null; the nullable converter should throw on parse failure instead of returning null for invalid values.</violation>

<violation number="2" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Client/DateTimeNullableJsonConverter.cs:77">
P2: Serialization uses the `K` format specifier without normalizing `DateTimeKind`. For `DateTimeKind.Unspecified` this emits no offset, producing non‑RFC3339 strings (no timezone) even though the converter is documented for RFC3339 `date-time` values.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Model/NullTypeDirect.cs">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Model/NullTypeDirect.cs:141">
P2: `alwaysNull` represents a bare `null` type but the converter accepts any JSON value and re-serializes non-null values, so invalid payloads like `{ "alwaysNull": 123 }` are treated as valid.</violation>

<violation number="2" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Model/NullTypeDirect.cs:152">
P1: Nullable `Id` is handled as non-nullable in the custom JSON converter, causing runtime exceptions on null during read/write.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/docs/models/NullTypeDirect.md">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/docs/models/NullTypeDirect.md:8">
P3: The docs link `Null.md` for the `Object` type, but no `Null.md` file exists in the models docs directory, so this link is broken.</violation>
</file>

<file name="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Client/TokenBase.cs">

<violation number="1" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Client/TokenBase.cs:67">
P2: IsRateLimited reads _nextAvailable without synchronization while BeginRateLimit writes it under a lock. The timer thread can observe stale state and raise TokenBecameAvailable early. Guard the read with the same lock (or use a volatile/interlocked read).</violation>

<violation number="2" location="samples/client/petstore/csharp/generichost/latest/NullTypes/src/Org.OpenAPITools/Client/TokenBase.cs:81">
P2: Event is checked and invoked via separate reads, creating a race that can throw `NullReferenceException` when subscribers change concurrently.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant