Skip to content

Output-only model properties should use init-only setters ({ get; init; }) #161

@daviburg

Description

@daviburg

Context

All generated model properties use { get; set; } regardless of whether the type is an input model (caller constructs it) or an output model (service returns it). Azure SDK guidelines recommend { get; init; } for output-only model properties to prevent accidental mutation after deserialization.

Problem

// Current — all properties settable after construction
public class VisualizeResults
{
    public string Body { get; set; }
    public string BodyHtml { get; set; }
    public string AttachmentContent { get; set; }
    // ...
}

VisualizeResults is only ever returned by the service — callers never construct it. Allowing set; means callers can silently overwrite a deserialized value, which leads to subtle bugs and reduces the ability to reason about data provenance.

Proposed change

Use { get; init; } for properties on output-only model types:

public class VisualizeResults
{
    [JsonPropertyName("body")]
    public string Body { get; init; }

    [JsonPropertyName("bodyHtml")]
    public string BodyHtml { get; init; }
    // ...
}

Input models (used as request bodies, e.g., QueryAndListSchema) keep { get; set; } for convenient object initializer syntax.

Scope

Generator template change — the CodefulSdkGenerator must distinguish input vs output types (based on whether the type appears as a method parameter or only as a return type) and emit init accordingly. The KustoModelFactory pattern already exists to support constructing output models in tests, so init setters are compatible with the existing test infrastructure.

Azure SDK guideline

Output model properties should be read-only after construction

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions