Azure / autorest.csharp

Extension for AutoRest (https://github.com/Azure/autorest) that generates C# code
MIT License
143 stars 166 forks source link

Swagger input generator is handling `SerializationFormat` on properties differently from the typespec generator #5158

Open ArcturusZhang opened 1 week ago

ArcturusZhang commented 1 week ago

This difference leads an ugly workaround on sample generator.

Fixing it here might not be worthy, creating this issue to track the issue.

Issue: In typespec input, models are created by ModelProvider class. The properties inside this class will have SerializationFormat property properly assigned. But in swagger input, models are created by SchemaObjectType class. The properties inside this class also have this SerializationFormat property, but its value is always Default.

Since now in the generator, only HLC and mgmt have some project using swagger as input, and HLC does not have a sample generator, this issue only impacts mgmt plane.

live1206 commented 1 day ago

For TypeSpec input, right now MPG convert CodeModel into InputNamespace, but still processing with SchemaObjectType instead of ModelTypeProvider, will it also cause SerializationFormat missing?

ArcturusZhang commented 1 day ago

For TypeSpec input, right now MPG convert CodeModel into InputNamespace, but still processing with SchemaObjectType instead of ModelTypeProvider, will it also cause SerializationFormat missing?

we lose this information in SchemaObjectType. This is how it is done: In ModelTypeProvider, we construct the properties using this ctor: https://github.com/Azure/autorest.csharp/blob/ac353cafa0e1fb5e9f81aecbd9a9930bee45c1ea/src/AutoRest.CSharp/Common/Output/Models/Types/ObjectTypeProperty.cs#L23 In SchemaObjectType, we construct the properties using this ctor: https://github.com/Azure/autorest.csharp/blob/ac353cafa0e1fb5e9f81aecbd9a9930bee45c1ea/src/AutoRest.CSharp/Common/Output/Models/Types/ObjectTypeProperty.cs#L38

In the second ctor, there is no input parameter of SerializationFormat, therefore it is always Default. We need the serialization format when writing the sample values, for example we need to know, if the property is date-time, and we have a value 2024-11-21, how we assign this value to the property. But since the SerializationFormat is missing in the property, it is tricky to restore it.