From d9faa2ac04b571e5701888ac60e4fd10e6af33f9 Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Tue, 20 Aug 2024 16:32:52 -0700 Subject: [PATCH] Add enclosingType to InputModelProperty (#4221) Contributes to https://github.com/microsoft/typespec/issues/4091 --- .../src/InputTypes/InputModelProperty.cs | 1 + .../src/InputTypes/InputModelType.cs | 17 +++++++++++++- .../TypeSpecInputModelTypeConverter.cs | 2 +- .../test/InputModelTypeTests.cs | 22 +++++++++++++++++++ ...rosoft.Generator.CSharp.Input.Tests.csproj | 1 + 5 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/test/InputModelTypeTests.cs diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/InputModelProperty.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/InputModelProperty.cs index d60570bd71..481b61edc4 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/InputModelProperty.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/InputModelProperty.cs @@ -26,6 +26,7 @@ public InputModelProperty(string name, string serializedName, string description public bool IsRequired { get; } public bool IsReadOnly { get; } public bool IsDiscriminator { get; } + public InputModelType? EnclosingType { get; internal set; } public IReadOnlyList FlattenedNames { get; } public IReadOnlyList Decorators { get; internal set; } = new List(); } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/InputModelType.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/InputModelType.cs index ae0753a833..1d2c7400e2 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/InputModelType.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/InputModelType.cs @@ -36,7 +36,22 @@ public InputModelType(string name, string crossLanguageDefinitionId, string? acc public string? Deprecation { get; internal set; } public string? Description { get; internal set; } public InputModelTypeUsage Usage { get; internal set; } - public IReadOnlyList Properties { get; internal set; } + + public IReadOnlyList Properties + { + get => _properties; + internal set + { + foreach (var property in value) + { + property.EnclosingType = this; + } + + _properties = value; + } + } + + private IReadOnlyList _properties = []; public bool ModelAsStruct { get; internal set; } public InputModelType? BaseModel { get; internal set; } public IReadOnlyList DerivedModels => DerivedModelsInternal; diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/Serialization/TypeSpecInputModelTypeConverter.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/Serialization/TypeSpecInputModelTypeConverter.cs index 537b0a9e66..93a5e49810 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/Serialization/TypeSpecInputModelTypeConverter.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/Serialization/TypeSpecInputModelTypeConverter.cs @@ -40,7 +40,7 @@ public static InputModelType CreateModelType(ref Utf8JsonReader reader, string? deprecation: null, description: null, usage: InputModelTypeUsage.None, - properties: null!, + properties: [], baseModel: null, derivedModels: [], discriminatorValue: null, diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/test/InputModelTypeTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/test/InputModelTypeTests.cs new file mode 100644 index 0000000000..5fb90ab6c0 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/test/InputModelTypeTests.cs @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Microsoft.Generator.CSharp.Tests.Common; +using NUnit.Framework; + +namespace Microsoft.Generator.CSharp.Input.Tests +{ + internal class InputModelTypeTests + { + [Test] + public void EnclosingTypeIsSet() + { + var property = InputFactory.Property("prop1", InputPrimitiveType.Any, true, true); + var model1 = InputFactory.Model("foo", "internal", usage: InputModelTypeUsage.Input, properties: [property]); + Assert.AreEqual(model1, property.EnclosingType); + + var model2 = InputFactory.Model("bar", "internal", usage: InputModelTypeUsage.Input, properties: [property]); + Assert.AreEqual(model2, property.EnclosingType); + } + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/test/Microsoft.Generator.CSharp.Input.Tests.csproj b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/test/Microsoft.Generator.CSharp.Input.Tests.csproj index a12a87a916..3c67ba1260 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/test/Microsoft.Generator.CSharp.Input.Tests.csproj +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/test/Microsoft.Generator.CSharp.Input.Tests.csproj @@ -1,6 +1,7 @@ +