Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private IReadOnlyList<ModelProvider> BuildDerivedModels()
}
}

return [.. derivedModels];
return [.. derivedModels.OrderBy(m => m.Name, StringComparer.Ordinal)];
}
internal override TypeProvider? BaseTypeProvider => _baseTypeProvider ??= BuildBaseTypeProvider();
private TypeProvider? _baseTypeProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static IEnumerable<TestCaseData> GetBaseModels()
yield return new TestCaseData(_baseModel, """
/// <summary>
/// pet description
/// Please note this is the abstract base class. The derived classes available for instantiation are: <see cref="Sample.Models.Cat"/>, <see cref="Sample.Models.Dog"/>, and <see cref="Sample.Models.AnotherAnimal"/>.
/// Please note this is the abstract base class. The derived classes available for instantiation are: <see cref="Sample.Models.AnotherAnimal"/>, <see cref="Sample.Models.Cat"/>, and <see cref="Sample.Models.Dog"/>.
/// </summary>

""");
Expand Down Expand Up @@ -120,8 +120,8 @@ public void BaseShouldBeAbstract(InputModelType inputModel, string expectedSumma
Assert.AreEqual(expectedSummary, baseModel.XmlDocs.Summary!.ToDisplayString());
}

[TestCase(true, "Please note this is the abstract base class. The derived classes available for instantiation are: <see cref=\"Sample.Models.Cat\"/>, <see cref=\"Sample.Models.Dog\"/>, and <see cref=\"Sample.Models.AnotherAnimal\"/>.")]
[TestCase(false, "Please note this is the base class. The derived classes available for instantiation are: <see cref=\"Sample.Models.Cat\"/>, <see cref=\"Sample.Models.Dog\"/>, and <see cref=\"Sample.Models.AnotherAnimal\"/>.")]
[TestCase(true, "Please note this is the abstract base class. The derived classes available for instantiation are: <see cref=\"Sample.Models.AnotherAnimal\"/>, <see cref=\"Sample.Models.Cat\"/>, and <see cref=\"Sample.Models.Dog\"/>.")]
[TestCase(false, "Please note this is the base class. The derived classes available for instantiation are: <see cref=\"Sample.Models.AnotherAnimal\"/>, <see cref=\"Sample.Models.Cat\"/>, and <see cref=\"Sample.Models.Dog\"/>.")]
public void DiscriminatedBaseDescriptionReflectsAbstractness(bool isAbstract, string expectedDescription)
{
MockHelpers.LoadMockGenerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,32 @@ public void TestBuildDescription_SkipsDerivedClassesTextWhenNoPublicDerivedModel
Assert.AreEqual("BaseModel description", provider!.Description.ToString());
}

[TestCase(false)]
[TestCase(true)]
[SetCulture("en-US")]
public void DerivedModelsAreSortedAndDeduplicated(bool reverseInputOrder)
{
var cat = InputFactory.Model("Cat");
var zebra = InputFactory.Model("Zebra");
var antelope = InputFactory.Model("antelope", isExactName: true);
InputModelType[] subtypes = [zebra, cat];
InputModelType[] derivedModels = [antelope, cat];
if (reverseInputOrder)
{
Array.Reverse(subtypes);
Array.Reverse(derivedModels);
}

var inputModel = InputFactory.Model(
"Animal",
discriminatedModels: subtypes.ToDictionary(m => m.Name),
derivedModels: derivedModels);
var model = CodeModelGenerator.Instance.TypeFactory.CreateModel(inputModel);

Assert.IsNotNull(model);
Assert.AreEqual(new[] { "Cat", "Zebra", "antelope" }, model!.DerivedModels.Select(m => m.Name));
}

[Test]
public void TestBuildProperties_ValidateInheritHierarchyWithOverride()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace SampleTypeSpec
{
/// <summary>
/// Base animal with discriminator
/// Please note this is the abstract base class. The derived classes available for instantiation are: <see cref="Pet"/> and <see cref="Dog"/>.
/// Please note this is the abstract base class. The derived classes available for instantiation are: <see cref="Dog"/> and <see cref="Pet"/>.
/// </summary>
[PersistableModelProxy(typeof(UnknownAnimal))]
public abstract partial class Animal : IJsonModel<Animal>
Expand Down Expand Up @@ -150,10 +150,10 @@ internal static Animal DeserializeAnimal(JsonElement element, ModelReaderWriterO
{
switch (discriminator.GetString())
{
case "pet":
return Pet.DeserializePet(element, options);
case "dog":
return Dog.DeserializeDog(element, options);
case "pet":
return Pet.DeserializePet(element, options);
}
}
return UnknownAnimal.DeserializeUnknownAnimal(element, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace SampleTypeSpec
{
/// <summary>
/// Base animal with discriminator
/// Please note this is the abstract base class. The derived classes available for instantiation are: <see cref="Pet"/> and <see cref="Dog"/>.
/// Please note this is the abstract base class. The derived classes available for instantiation are: <see cref="Dog"/> and <see cref="Pet"/>.
/// </summary>
public abstract partial class Animal
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ public static StreamingItem StreamingItem(string message = default)

/// <summary>
/// Base animal with discriminator
/// Please note this is the abstract base class. The derived classes available for instantiation are: <see cref="SampleTypeSpec.Pet"/> and <see cref="SampleTypeSpec.Dog"/>.
/// Please note this is the abstract base class. The derived classes available for instantiation are: <see cref="SampleTypeSpec.Dog"/> and <see cref="SampleTypeSpec.Pet"/>.
/// </summary>
/// <param name="kind"> The kind of animal. </param>
/// <param name="name"> Name of the animal. </param>
Expand Down