Skip to content

Commit

Permalink
[http-client-csharp] fix: use inputmodel name as default name in seri…
Browse files Browse the repository at this point in the history
…alization provider (#4823)

This PR fixes an issue where the name of the serialization provider for
a model was being built incorrectly when the model is renamed and the
renamed model is also further customized. It addresses this issue by
ensuring the default name of the serialization provider is the name of
the input model it is constructed from.

fixes: #4793
  • Loading branch information
jorgerangel-msft authored Oct 22, 2024
1 parent dfade34 commit 30568a4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public MrwSerializationTypeDefinition(InputModelType inputModel, ModelProvider m

protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", "Models", $"{Name}.Serialization.cs");

protected override string BuildName() => _model.Name;
protected override string BuildName() => _inputModel.Name.ToCleanName();

protected override IReadOnlyList<AttributeStatement> BuildAttributes()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,27 @@ public async Task CanCustomizeEnumToFieldFrameworkType()
var file = writer.Write();
Assert.AreEqual(Helpers.GetExpectedFromFile(), file.Content);
}

[Test]
public async Task CanCustomizeModelName()
{
await MockHelpers.LoadMockPluginAsync(compilation: async () => await Helpers.GetCompilationFromDirectoryAsync());
var inputModel = InputFactory.Model("mockInputModel", properties: [], usage: InputModelTypeUsage.Json);

var plugin = await MockHelpers.LoadMockPluginAsync(
inputModels: () => [inputModel],
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync());

var modelProvider = plugin.Object.OutputLibrary.TypeProviders.Single(t => t is ModelProvider);
var serializationProvider = modelProvider.SerializationProviders.Single(t => t is MrwSerializationTypeDefinition);
Assert.IsNotNull(serializationProvider);

// both providers should have the custom name
Assert.AreEqual("CustomModel", modelProvider.Name);
Assert.AreEqual("CustomModel", serializationProvider.Name);

Assert.AreEqual("CustomModel", modelProvider.CustomCodeView?.Name);
Assert.AreEqual("CustomModel", serializationProvider.CustomCodeView?.Name);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#nullable disable

using Microsoft.Generator.CSharp.Customization;

namespace Sample.Models
{
[CodeGenModel("MockInputModel")]
internal partial class CustomModel { }
}

0 comments on commit 30568a4

Please sign in to comment.