christianhelle / refitter

A tool for generating Refit interfaces and contracts from OpenAPI specifications
https://refitter.github.io
MIT License
161 stars 36 forks source link

Add support for generating immutable records #409

Closed christianhelle closed 2 weeks ago

christianhelle commented 2 weeks ago

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
  }
}
sonarcloud[bot] commented 2 weeks ago

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud