Skip to content

[Generator Bug] @@alternateType to armResourceType emits WriteObjectValue instead of WriteStringValue #57287

@live1206

Description

@live1206

Description

When @@alternateType maps a property to Azure.Core.armResourceType (C# ResourceType), the generator emits WriteObjectValue(ResourceType, options) for serialization. But WriteObjectValue<T> has no case for ResourceType (which is a readonly struct wrapping a string), so it falls through to the default case and throws:

System.NotSupportedException: Not supported type Azure.Core.ResourceType

This also affects deserialization — the generator emits ResourceType.DeserializeResourceType() which doesn't exist as a public method.

Affected Model (Storage MPG Migration)

  • StorageAccountNameAvailabilityContent — spec has type: "Microsoft.Storage/storageAccounts" (constant string), mapped via:
@@alternateType(StorageAccountCheckNameAvailabilityParameters.type, Azure.Core.armResourceType, "csharp");

Impact

This blocks 230 out of 287 tests in the Storage SDK because nearly every test calls CheckStorageAccountNameAvailability during setup.

Current Workaround

Custom [CodeGenSerialization] hooks for both directions:

[CodeGenSerialization(nameof(ResourceType),
    DeserializationValueHook = nameof(DeserializeResourceType),
    SerializationValueHook = nameof(SerializeResourceType))]
public partial class StorageAccountNameAvailabilityContent
{
    private static void SerializeResourceType(Utf8JsonWriter writer, ResourceType resourceType)
    {
        writer.WriteStringValue(resourceType.ToString());
    }

    private static void DeserializeResourceType(JsonProperty property, ref ResourceType resourceType)
    {
        resourceType = new ResourceType(property.Value.GetString());
    }
}

Note: The SerializationValueHook requires regeneration to take effect. The generated file still has WriteObjectValue until regenerated.

Comparison with Native Usage

When armResourceType is used as a native property type (not via @@alternateType), the generator correctly emits WriteStringValue(ResourceType). The bug is specific to the @@alternateType code path.

Works (native): testresourcetype.tsp

model ResourceTypeTestProperties {
  optionalResourceType?: Azure.Core.armResourceType;  // generates WriteStringValue ✓
}

Broken (alternateType): client.tsp

@@alternateType(SomeModel.type, Azure.Core.armResourceType, "csharp");  // generates WriteObjectValue ✗

Related PR

Metadata

Metadata

Assignees

Labels

CodeGenIssues that relate to code generationMgmtThis issue is related to a management package.bugThis issue requires a change to an existing behavior in the product in order to be resolved.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions