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

Add support for generating immutable records #409

Merged
merged 2 commits into from
Jul 4, 2024
Merged

Conversation

christianhelle
Copy link
Owner

@christianhelle christianhelle commented Jul 4, 2024

This implements #407

This pull request updates the CSharpClientGeneratorFactory to use CSharpClassStyle.Record when the GenerateNativeRecords setting is true. This change adds proper support for generating contracts as immutable records

Here's an example of a generated immutable record using the changes in this pull request

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.8.0 (NJsonSchema v11.0.1.0 (Newtonsoft.Json v13.0.0.0))")]
public partial record Tag
{
    [JsonConstructor]
    public Tag(long @id, string @name)
    {
        this.Id = @id;
        this.Name = @name;
    }

    [JsonPropertyName("id")]
    public long Id { get; init; }

    [JsonPropertyName("name")]
    public string Name { get; init; }

    private IDictionary<string, object> _additionalProperties;

    [JsonExtensionData]
    public IDictionary<string, object> AdditionalProperties
    {
        get { return _additionalProperties ?? (_additionalProperties = new Dictionary<string, object>()); }
        set { _additionalProperties = value; }
    }
}

using the following .refitter settings file

{
  "openApiPath": "https://petstore3.swagger.io/api/v3/openapi.json",
  "codeGeneratorSettings": {
    "generateNativeRecords": true
  }
}

@christianhelle christianhelle added the enhancement New feature, bug fix, or request label Jul 4, 2024
@christianhelle christianhelle self-assigned this Jul 4, 2024
@christianhelle christianhelle added the .NET Pull requests that contain changes to .NET code label Jul 4, 2024
Copy link

sonarqubecloud bot commented Jul 4, 2024

@christianhelle christianhelle merged commit 2faef1f into main Jul 4, 2024
841 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature, bug fix, or request .NET Pull requests that contain changes to .NET code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant