Skip to content

Commit 805ac66

Browse files
Copilotm-nash
andcommitted
Add tests validating @dynamicModel decorator behavior
Co-authored-by: m-nash <[email protected]>
1 parent 4f28a65 commit 805ac66

File tree

2 files changed

+76
-165
lines changed

2 files changed

+76
-165
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Collections.Generic;
5+
using System.Reflection;
6+
using Microsoft.TypeSpec.Generator.Input;
7+
using Microsoft.TypeSpec.Generator.Primitives;
8+
using Microsoft.TypeSpec.Generator.Providers;
9+
using Microsoft.TypeSpec.Generator.Tests.Common;
10+
using NUnit.Framework;
11+
12+
namespace Microsoft.TypeSpec.Generator.Tests.Providers
13+
{
14+
public class DynamicModelSerializationTests
15+
{
16+
[SetUp]
17+
public void Setup()
18+
{
19+
MockHelpers.LoadMockGenerator();
20+
}
21+
[Test]
22+
public void DynamicModelSerialization()
23+
{
24+
var properties = new[]
25+
{
26+
InputFactory.Property("id", InputPrimitiveType.String, isRequired: true),
27+
InputFactory.Property("name", InputPrimitiveType.String, isRequired: true),
28+
InputFactory.Property("email", InputPrimitiveType.String, isRequired: false)
29+
};
30+
31+
// Create a model with the dynamicModel decorator
32+
var inputModel = InputFactory.Model("User", properties: properties);
33+
34+
// Use reflection to set decorators since the property has an internal setter
35+
var decoratorsProperty = typeof(InputType).GetProperty("Decorators");
36+
var decorators = new List<InputDecoratorInfo>
37+
{
38+
new InputDecoratorInfo("dynamicModel", null)
39+
};
40+
decoratorsProperty?.SetValue(inputModel, decorators);
41+
42+
var modelProvider = new ModelProvider(inputModel);
43+
var writer = new TypeProviderWriter(modelProvider);
44+
var file = writer.Write();
45+
46+
// Verify the model doesn't have the raw data field
47+
Assert.That(file.Content, Does.Not.Contain("_additionalBinaryDataProperties"));
48+
49+
// Verify the model has the Patch property
50+
Assert.That(file.Content, Contains.Substring("public object Patch { get; set; }"));
51+
}
52+
53+
[Test]
54+
public void RegularModelStillHasRawDataField()
55+
{
56+
var properties = new[]
57+
{
58+
InputFactory.Property("id", InputPrimitiveType.String, isRequired: true),
59+
InputFactory.Property("name", InputPrimitiveType.String, isRequired: true)
60+
};
61+
62+
// Create a regular model without the dynamicModel decorator
63+
var inputModel = InputFactory.Model("RegularUser", properties: properties);
64+
65+
var modelProvider = new ModelProvider(inputModel);
66+
var writer = new TypeProviderWriter(modelProvider);
67+
var file = writer.Write();
68+
69+
// Verify the model has the raw data field
70+
Assert.That(file.Content, Contains.Substring("_additionalBinaryDataProperties"));
71+
72+
// Verify the model doesn't have the Patch property
73+
Assert.That(file.Content, Does.Not.Contain("public object Patch { get; set; }"));
74+
}
75+
}
76+
}

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/DynamicModelTests.cs

Lines changed: 0 additions & 165 deletions
This file was deleted.

0 commit comments

Comments
 (0)