Skip to content

Commit

Permalink
Align the structure of model property (#4319)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcturusZhang authored Sep 5, 2024
1 parent 06c0bb5 commit ad635d9
Show file tree
Hide file tree
Showing 31 changed files with 1,400 additions and 832 deletions.
5 changes: 0 additions & 5 deletions packages/http-client-csharp/emitter/src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,6 @@ function transformJSONProperties(this: any, key: string, value: any): any {
}
}

// omit the `rawExample` property from the examples
if (key === "rawExample") {
return undefined;
}

return value;
}

Expand Down
16 changes: 8 additions & 8 deletions packages/http-client-csharp/emitter/src/lib/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import {
} from "@azure-tools/typespec-client-generator-core";
import { Model } from "@typespec/compiler";
import { InputEnumTypeValue } from "../type/input-enum-type-value.js";
import { InputModelProperty } from "../type/input-model-property.js";
import {
InputArrayType,
InputDateTimeType,
InputDictionaryType,
InputDurationType,
InputEnumType,
InputLiteralType,
InputModelProperty,
InputModelType,
InputPrimitiveType,
InputType,
Expand Down Expand Up @@ -181,26 +181,26 @@ export function fromSdkModelType(
const serializedName = property.serializedName;
literalTypeContext.PropertyName = serializedName;

const isRequired = !property.optional;
const isDiscriminator = property.discriminator;
const modelProperty: InputModelProperty = {
Kind: property.kind,
Name: property.name,
SerializedName: serializedName,
Description: property.description ?? (isDiscriminator ? "Discriminator" : ""),
Description: property.description,
Type: fromSdkType(
property.type,
context,
typeMap,
isDiscriminator ? undefined : literalTypeContext // this is a workaround because the type of discriminator property in derived models is always literal and we wrap literal into enums, which leads to a lot of extra enum types, adding this check to avoid them
property.discriminator ? undefined : literalTypeContext // this is a workaround because the type of discriminator property in derived models is always literal and we wrap literal into enums, which leads to a lot of extra enum types, adding this check to avoid them
),
IsRequired: isRequired,
IsReadOnly: isReadOnly(property),
IsDiscriminator: isDiscriminator === true ? true : undefined,
Optional: property.optional,
ReadOnly: isReadOnly(property), // TODO -- we might pass the visibility through and then check if there is only read to know if this is readonly
Discriminator: property.discriminator,
FlattenedNames:
flattenedNamePrefixes.length > 0
? flattenedNamePrefixes.concat(property.name)
: undefined,
Decorators: property.decorators,
CrossLanguageDefinitionId: property.crossLanguageDefinitionId,
};

return [modelProperty];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export function fromSdkHttpExamples(
name: example.name,
description: example.description,
filePath: example.filePath,
rawExample: example.rawExample,
parameters: example.parameters.map((p) => fromSdkParameterExample(p)),
responses: fromSdkOperationResponses(example.responses),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ interface InputExampleBase {
name: string;
description: string;
filePath: string;
rawExample: any;
}

export interface InputHttpOperationExample extends InputExampleBase {
Expand Down

This file was deleted.

13 changes: 12 additions & 1 deletion packages/http-client-csharp/emitter/src/type/input-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from "@azure-tools/typespec-client-generator-core";
import { DateTimeKnownEncoding, DurationKnownEncoding } from "@typespec/compiler";
import { InputEnumTypeValue } from "./input-enum-type-value.js";
import { InputModelProperty } from "./input-model-property.js";

interface InputTypeBase {
Kind: string;
Expand Down Expand Up @@ -99,6 +98,18 @@ export interface InputModelType extends InputTypeBase {
BaseModel?: InputModelType;
}

export interface InputModelProperty extends InputTypeBase {
Kind: "property";
Name: string;
SerializedName: string;
Type: InputType;
Optional: boolean;
ReadOnly: boolean;
Discriminator: boolean;
CrossLanguageDefinitionId: string;
FlattenedNames?: string[]; // TODO -- remove this when we are ready to move the flatten handling from emitter to the generator
}

export function isInputModelType(type: InputType): type is InputModelType {
return type.Kind === "model";
}
Expand Down
18 changes: 9 additions & 9 deletions packages/http-client-csharp/emitter/test/Unit/model-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ op test(@body input: Pet): Pet;
strictEqual(discriminatorProperty?.Name, "kind");
strictEqual(discriminatorProperty.SerializedName, "kind");
strictEqual(discriminatorProperty.Type.Kind, "string");
strictEqual(discriminatorProperty.IsRequired, true);
strictEqual(discriminatorProperty.IsReadOnly, false);
strictEqual(discriminatorProperty.IsDiscriminator, true);
strictEqual(discriminatorProperty.Optional, false);
strictEqual(discriminatorProperty.ReadOnly, false);
strictEqual(discriminatorProperty.Discriminator, true);
strictEqual(discriminatorProperty.FlattenedNames, undefined);
// assert we will NOT have a DiscriminatorProperty on the derived models
assert(
Expand Down Expand Up @@ -146,9 +146,9 @@ op test(@body input: Pet): Pet;
strictEqual(discriminatorProperty.Type.Kind, "enum");
strictEqual(discriminatorProperty.Type.Name, "PetKind");
strictEqual(discriminatorProperty.Type.ValueType.Kind, "string");
strictEqual(discriminatorProperty.IsRequired, true);
strictEqual(discriminatorProperty.IsReadOnly, false);
strictEqual(discriminatorProperty.IsDiscriminator, true);
strictEqual(discriminatorProperty.Optional, false);
strictEqual(discriminatorProperty.ReadOnly, false);
strictEqual(discriminatorProperty.Discriminator, true);
strictEqual(discriminatorProperty.FlattenedNames, undefined);

// verify derived model Cat
Expand Down Expand Up @@ -239,9 +239,9 @@ op test(@body input: Pet): Pet;
strictEqual(discriminatorProperty.Type.Kind, "enum");
strictEqual(discriminatorProperty.Type.Name, "PetKind");
strictEqual(discriminatorProperty.Type.ValueType.Kind, "string");
strictEqual(discriminatorProperty.IsRequired, true);
strictEqual(discriminatorProperty.IsReadOnly, false);
strictEqual(discriminatorProperty.IsDiscriminator, true);
strictEqual(discriminatorProperty.Optional, false);
strictEqual(discriminatorProperty.ReadOnly, false);
strictEqual(discriminatorProperty.Discriminator, true);
strictEqual(discriminatorProperty.FlattenedNames, undefined);

// verify derived model Cat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.Generator.CSharp.Input
{
public class InputModelProperty
{
public InputModelProperty(string name, string serializedName, string description, InputType type, bool isRequired, bool isReadOnly, bool isDiscriminator, IReadOnlyList<string>? flattenedNames = null)
public InputModelProperty(string name, string serializedName, string? description, InputType type, bool isRequired, bool isReadOnly, bool isDiscriminator, IReadOnlyList<string>? flattenedNames = null)
{
Name = name;
SerializedName = serializedName;
Expand All @@ -21,7 +21,7 @@ public InputModelProperty(string name, string serializedName, string description

public string Name { get; }
public string SerializedName { get; }
public string Description { get; }
public string? Description { get; }
public InputType Type { get; }
public bool IsRequired { get; }
public bool IsReadOnly { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ private static InputModelProperty ReadInputModelProperty(ref Utf8JsonReader read
string? description = null;
InputType? propertyType = null;
bool isReadOnly = false;
bool isRequired = false;
bool isOptional = false;
bool isDiscriminator = false;
IReadOnlyList<InputDecoratorInfo>? decorators = null;
IReadOnlyList<string>? flattenedNames = null;

while (reader.TokenType != JsonTokenType.EndObject)
{
var isKnownProperty = reader.TryReadReferenceId(ref isFirstProperty, ref id)
|| reader.TryReadString(nameof(InputModelProperty.Name), ref name)
|| reader.TryReadString(nameof(InputModelProperty.SerializedName), ref serializedName)
|| reader.TryReadString(nameof(InputModelProperty.Description), ref description)
|| reader.TryReadWithConverter(nameof(InputModelProperty.Type), options, ref propertyType)
|| reader.TryReadBoolean(nameof(InputModelProperty.IsReadOnly), ref isReadOnly)
|| reader.TryReadBoolean(nameof(InputModelProperty.IsRequired), ref isRequired)
|| reader.TryReadBoolean(nameof(InputModelProperty.IsDiscriminator), ref isDiscriminator)
|| reader.TryReadWithConverter(nameof(InputModelProperty.Decorators), options, ref decorators)
|| reader.TryReadWithConverter(nameof(InputModelProperty.FlattenedNames), options, ref flattenedNames);
|| reader.TryReadString("Name", ref name)
|| reader.TryReadString("SerializedName", ref serializedName)
|| reader.TryReadString("Description", ref description)
|| reader.TryReadWithConverter("Type", options, ref propertyType)
|| reader.TryReadBoolean("ReadOnly", ref isReadOnly)
|| reader.TryReadBoolean("Optional", ref isOptional)
|| reader.TryReadBoolean("Discriminator", ref isDiscriminator)
|| reader.TryReadWithConverter("Decorators", options, ref decorators)
|| reader.TryReadWithConverter("FlattenNames", options, ref flattenedNames);

if (!isKnownProperty)
{
Expand All @@ -55,12 +55,11 @@ private static InputModelProperty ReadInputModelProperty(ref Utf8JsonReader read
}

name = name ?? throw new JsonException($"{nameof(InputModelProperty)} must have a name.");
description = description ?? throw new JsonException($"{nameof(InputModelProperty)} must have a description.");
// TO-DO: Implement as part of autorest output classes migration https://github.com/Azure/autorest.csharp/issues/4198
// description = BuilderHelpers.EscapeXmlDocDescription(description);
propertyType = propertyType ?? throw new JsonException($"{nameof(InputModelProperty)} must have a property type.");

var property = new InputModelProperty(name, serializedName ?? name, description, propertyType, isRequired, isReadOnly, isDiscriminator, flattenedNames) { Decorators = decorators ?? [] };
var property = new InputModelProperty(name, serializedName ?? name, description, propertyType, !isOptional, isReadOnly, isDiscriminator, flattenedNames) { Decorators = decorators ?? [] };
if (id != null)
{
resolver.AddReference(id, property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private PropertyProvider(InputModelProperty inputProperty, CSharpType propertyTy
EnclosingType = enclosingType;
IsDiscriminator = inputProperty.IsDiscriminator;

InitializeParameter(Name, FormattableStringHelpers.FromString(inputProperty.Description), Type);
InitializeParameter(Name, FormattableStringHelpers.FromString(inputProperty.Description) ?? FormattableStringHelpers.Empty, Type);
}

public PropertyProvider(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,18 +249,16 @@ protected virtual ParameterProvider CreateParameterCore(InputParameter parameter
/// <returns>An instance of <see cref="PropertyProvider"/>.</returns>
private PropertyProvider? CreatePropertyProviderCore(InputModelProperty property, TypeProvider enclosingType)
{
PropertyProvider.TryCreate(property, enclosingType, out var propertyProvider);
if (Visitors.Count == 0)
{
PropertyProvider.TryCreate(property, enclosingType, out var propertyProvider);
if (Visitors.Count == 0)
{
return propertyProvider;
}
foreach (var visitor in Visitors)
{
propertyProvider = visitor.Visit(property, propertyProvider);
}
return propertyProvider;
}
foreach (var visitor in Visitors)
{
propertyProvider = visitor.Visit(property, propertyProvider);
}
return propertyProvider;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@
"Properties": [
{
"$id": "3",
"Kind": "property",
"Name": "error",
"SerializedName": "error",
"Description": "",
"Type": {
"$id": "4",
"Kind": "string",
"Name": "string",
"CrossLanguageDefinitionId": "TypeSpec.string",
"Decorators": []
},
"IsRequired": true,
"IsReadOnly": false,
"Decorators": []
"Optional": false,
"ReadOnly": false,
"Discriminator": false,
"Decorators": [],
"CrossLanguageDefinitionId": "Authentication.ApiKey.InvalidAuth.error"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@
"Properties": [
{
"$id": "3",
"Kind": "property",
"Name": "error",
"SerializedName": "error",
"Description": "",
"Type": {
"$id": "4",
"Kind": "string",
"Name": "string",
"CrossLanguageDefinitionId": "TypeSpec.string",
"Decorators": []
},
"IsRequired": true,
"IsReadOnly": false,
"Decorators": []
"Optional": false,
"ReadOnly": false,
"Discriminator": false,
"Decorators": [],
"CrossLanguageDefinitionId": "Authentication.Http.Custom.InvalidAuth.error"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@
"Properties": [
{
"$id": "3",
"Kind": "property",
"Name": "error",
"SerializedName": "error",
"Description": "",
"Type": {
"$id": "4",
"Kind": "string",
"Name": "string",
"CrossLanguageDefinitionId": "TypeSpec.string",
"Decorators": []
},
"IsRequired": true,
"IsReadOnly": false,
"Decorators": []
"Optional": false,
"ReadOnly": false,
"Discriminator": false,
"Decorators": [],
"CrossLanguageDefinitionId": "Authentication.OAuth2.InvalidAuth.error"
}
]
}
Expand Down
Loading

0 comments on commit ad635d9

Please sign in to comment.