Skip to content

Commit

Permalink
Add enclosingType to InputModelProperty (#4221)
Browse files Browse the repository at this point in the history
Contributes to #4091
  • Loading branch information
JoshLove-msft authored Aug 20, 2024
1 parent 1fba540 commit d9faa2a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> FlattenedNames { get; }
public IReadOnlyList<InputDecoratorInfo> Decorators { get; internal set; } = new List<InputDecoratorInfo>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<InputModelProperty> Properties { get; internal set; }

public IReadOnlyList<InputModelProperty> Properties
{
get => _properties;
internal set
{
foreach (var property in value)
{
property.EnclosingType = this;
}

_properties = value;
}
}

private IReadOnlyList<InputModelProperty> _properties = [];
public bool ModelAsStruct { get; internal set; }
public InputModelType? BaseModel { get; internal set; }
public IReadOnlyList<InputModelType> DerivedModels => DerivedModelsInternal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\..\Microsoft.Generator.CSharp\test\common\Microsoft.Generator.CSharp.Tests.Common.csproj" />
<ProjectReference Include="..\src\Microsoft.Generator.CSharp.Input.csproj" />
</ItemGroup>

Expand Down

0 comments on commit d9faa2a

Please sign in to comment.