11using Microsoft . TypeSpec . Generator . ClientModel ;
22using Microsoft . TypeSpec . Generator . Primitives ;
33using Microsoft . TypeSpec . Generator . Providers ;
4- using System . Reflection ;
54
65namespace OpenAILibraryPlugin . Visitors ;
76
@@ -17,16 +16,22 @@ protected override TypeProvider VisitType(TypeProvider type)
1716 && type . DeclarationModifiers . HasFlag ( TypeSignatureModifiers . Abstract )
1817 && type . CustomCodeView ? . DeclarationModifiers . HasFlag ( TypeSignatureModifiers . Abstract ) == false )
1918 {
19+ var serializationProviders = type . SerializationProviders ;
20+
21+ // ensure we build the serialization attributes before we change the modifiers
22+ foreach ( var serialization in serializationProviders )
23+ {
24+ _ = serialization . Attributes ;
25+ }
26+
2027 // Keep types defined in custom code without 'abstract' non-abstract
28+ type . Update ( modifiers : type . DeclarationModifiers & ~ TypeSignatureModifiers . Abstract ) ;
2129
22- // To do: replace with this line when dependencies updated to include modifier support
23- // type.Update(modifiers: type.DeclarationModifiers & ~TypeSignatureModifiers.Abstract);
24- // To do: remove this reflection-based workaround for the above:
25- FieldInfo privateModifiersInfo = typeof ( TypeProvider )
26- . GetField ( "_declarationModifiers" , BindingFlags . Instance | BindingFlags . NonPublic ) ! ;
27- TypeSignatureModifiers privateValue = ( TypeSignatureModifiers ) privateModifiersInfo . GetValue ( type ) ! ;
28- privateValue &= ~ TypeSignatureModifiers . Abstract ;
29- privateModifiersInfo . SetValue ( type , privateValue ) ;
30+ // reset the serialization to pick up the new modifiers while keeping any serialization attributes
31+ foreach ( var serialization in serializationProviders )
32+ {
33+ serialization . Update ( attributes : serialization . Attributes , reset : true ) ;
34+ }
3035 }
3136 return type ;
3237 }
0 commit comments