Back to Azure Sdk For

Generated Client Design

doc/design/GeneratedClientDesign.md

2019-05-16T16-528.4 KB
Original Source

Generated Client Design

Introduction

This document tracks design rules and decisions for .NET clients generated by the DPG generator. It may later be extended to include other generator designs.

<!-- ## Clients TBD ## Client Protocol Operations TBD ## Client Convenience Operations -->

Model Generation

Models are defined as partial classes with one or more constructors, properties, and methods that read from/write to wire formats.

Discussion Notes

The .NET team discourages using C# Records in public APIs, so we will not use them for model types.

Model Namespace

Generated models will be defined in a .Models namespace.

Discussion Notes

Tracking finalization of namespace decision autorest.csharp #2514

Type mappings from Cadl to .NET

The following table shows a mapping from Cadl built-in types to the corresponding types used in .NET models.

Cadl Type.NET TypeOpenAPI TypeGitHub IssueNotes
stringstringstringautorest.csharp #2337PrimitivePropertyModel.cs PrimitivePropertyModel.Serialization.cs
bytesBinaryDatatype: string, format: byteautorest.csharp #2337
int32inttype: integer, format: int32autorest.csharp #2337
int64longtype: integer, format: int64autorest.csharp #2337
safeintlongn/aautorest.csharp #2337
float32floattype: number, format: floatautorest.csharp #2337
float64doubletype: number, format: doubleautorest.csharp #2337
zonedDateTimeDateTimeOffsettype: string, format: date-timeautorest.csharp #2337Serialized differently based on body/header
durationTimeSpantype: string, format: durationautorest.csharp #2337
booleanboolbooleanautorest.csharp #2337
<!-- TBD | TBD | type: string, format: binary | [autorest.csharp #2500](https://github.com/Azure/autorest.csharp/issues/2500)| -->

Model Shape

Model APIs will differ depending on whether the model is an input model, an output model, or a round-trip model (both input and output).

The following describes the model shapes for different cases of properties.

ItemInputOutputRound-TripGitHub Issue
Constructor Accessibilitypublicinternalpublic
Required Propertyget-onlyget-onlyget/setautorest.csharp #2463
Optional Propertyget/setget-onlyget/setautorest.csharp #2339
Collection PropertyIList<T> get-onlyIReadOnlyList<T> get-onlyIList<T> get-onlyautorest.csharp #2471

Model Constructors

Models have one or more constructors as follows.

Main Constructor

  • Accessibility is specified in model shape table above
  • Takes required parameters and does not take optional parameters
  • Validates required reference type parameters for null using Argument.AssertNotNull
  • Takes list properties as IEnumerable<T> parameters
  • Initializes lists from IEnumerable parameter using System.Linq .ToList()

Serialization Constructor

  • Internal accessibility
  • Generated for Output and Round-trip types
  • Takes required and optional parameters
  • Takes list properties as IReadOnlyList<T> parameters for Output models and IList<T> parameters for Round-trip models
  • List properties are initialized by assignment

See examples:

Collections

There are a number of outstanding open issues regarding how to represent collections being tracked:

Enums

Enums may be generated as a CLR enum or a C# "Strongly-typed string" as follows.

  • Enums are generated in the same namespace as models.
  • A Cadl enum not found in a corresponding @knownValues decorator is generated as a C# enum
  • Generated enum has corresponding internal extensions file providing conversions to/from string.
  • A Cadl string model with a @knownValues decorator is generated as a C# "Strongly-typed string" (sometimes called "extensible enum") and the corresponding Cadl enum is not generated as a C# enum

GitHub issue: autorest.csharp #2477

See examples:

Nested Models

Models that are properties of other models are called nested models. A model that appears only as a non-root of a model graph is defined as partial class according to the same rules as root models.

GitHub issue: autorest.csharp #2489

See examples:

Requirements for model graphs with circular dependencies will be defined in a later iteration. GitHub issue autorest.csharp #2506

Nested Model Shape

Models that appear as properties of other models have their shape determined as follows:

  • Models that only appear as properties of input models are input models
  • Models that only appear as properties of output models are output models
  • Models that only appear as properties of round-trip models are round-trip models
  • Models that appear as properties of more than one model shape are round-trip models

Model Reference Documentation

Miscellaneous

  • Models should be generated with the #nullable disable because Azure SDK libraries have not yet exposed nullable reference types outside of Azure.Core. The reason we have not done this is because the API compatibility tools we use cannot detect API breaking changes to nullable reference types.