Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports optional properties in C# < 8.0? #47

Open
twastvedt opened this issue Feb 17, 2022 · 0 comments
Open

Supports optional properties in C# < 8.0? #47

twastvedt opened this issue Feb 17, 2022 · 0 comments

Comments

@twastvedt
Copy link

twastvedt commented Feb 17, 2022

I have a project where I'm generating types for C# and Typescript. In C# I'm stuck on .NET Framework, thus on C# < 8.0. I'd like to be able to use optional properties (for Typescript), but they are turned into nullable reference types in C#, which are only supported in 8.0.

Here's an example. I have this json, which includes an optional reference property.

{
  "definitions": {
    "jobState": {
      "properties": {
        "jobId": {
          "type": "string"
        },
      }
    },
  },
  "properties": {
    "isReady": {
      "type": "boolean"
    }
  },
  "optionalProperties": {
    "jobState": {
      "metadata": { "description": "Current job." },
      "ref": "jobState"
    }
  }
}

It generates this Typescript, which is great (note the question mark after jobState):

export interface Status {
    isReady: boolean;

    /**
     * Current job.
     */
    jobState?: JobState;
}

export interface JobState {
    jobId: string;
}

and this C#, which is only compilable in 8.0. In 7.3 I get this error:

CS8370: Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.

using System.Text.Json.Serialization;

namespace MyProject
{
    public class Status
    {
        [JsonPropertyName("isReady")]
        public bool IsReady { get; set; }

        /// <summary>
        /// Current job.
        /// </summary>
        [JsonPropertyName("jobState")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public JobState? JobState { get; set; }
    }
}

Can there be a toggle to support C# < 8.0? Thanks!

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

No branches or pull requests

1 participant