From 891eb401113a7c1c9dc8d54869ef8609ce1cdecb Mon Sep 17 00:00:00 2001 From: ArcturusZhang Date: Mon, 29 May 2023 13:01:52 +0800 Subject: [PATCH 1/7] refactor --- .../Common/Generation/Types/CSharpType.cs | 12 ------- .../Common/Input/Source/CodeGenAttributes.cs | 36 +++++++++++++++++++ .../Common/Input/Source/ModelTypeMapping.cs | 11 +++--- .../Common/Input/Source/SourceInputModel.cs | 20 +++-------- 4 files changed, 47 insertions(+), 32 deletions(-) create mode 100644 src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs diff --git a/src/AutoRest.CSharp/Common/Generation/Types/CSharpType.cs b/src/AutoRest.CSharp/Common/Generation/Types/CSharpType.cs index e74879bfa87..0dfbcd1e0f3 100644 --- a/src/AutoRest.CSharp/Common/Generation/Types/CSharpType.cs +++ b/src/AutoRest.CSharp/Common/Generation/Types/CSharpType.cs @@ -163,18 +163,6 @@ internal static CSharpType FromSystemType(Type type, string defaultNamespace, So internal static CSharpType FromSystemType(BuildContext context, Type type) => FromSystemType(type, context.DefaultNamespace, context.SourceInputModel); - public bool IsCollectionType() - { - if (!IsFrameworkType) - return false; - - return FrameworkType.Equals(typeof(IList<>)) || - FrameworkType.Equals(typeof(IEnumerable<>)) || - FrameworkType == typeof(IReadOnlyList<>) || - FrameworkType.Equals(typeof(IDictionary<,>)) || - FrameworkType == typeof(IReadOnlyDictionary<,>); - } - public CSharpType GetNonNullable() { if (!IsNullable) diff --git a/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs b/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs new file mode 100644 index 00000000000..f92b7ac5999 --- /dev/null +++ b/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs @@ -0,0 +1,36 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using Microsoft.CodeAnalysis; +using Azure.Core; + +namespace AutoRest.CSharp.Input.Source +{ + public class CodeGenAttributes + { + public CodeGenAttributes(Compilation compilation) + { + CodeGenMemberAttribute = GetSymbol(compilation, typeof(CodeGenMemberAttribute)); + CodeGenTypeAttribute = GetSymbol(compilation, typeof(CodeGenTypeAttribute)); + CodeGenModelAttribute = GetSymbol(compilation, typeof(CodeGenModelAttribute)); + CodeGenClientAttribute = GetSymbol(compilation, typeof(CodeGenClientAttribute)); + CodeGenMemberSerializationAttribute = GetSymbol(compilation, typeof(CodeGenMemberSerializationAttribute)); + CodeGenMemberSerializationHooksAttribute = GetSymbol(compilation, typeof(CodeGenMemberSerializationHooksAttribute)); + } + + public INamedTypeSymbol CodeGenMemberAttribute { get; } + + public INamedTypeSymbol CodeGenTypeAttribute { get; } + + public INamedTypeSymbol CodeGenModelAttribute { get; } + + public INamedTypeSymbol CodeGenClientAttribute { get; } + + public INamedTypeSymbol CodeGenMemberSerializationAttribute { get; } + + public INamedTypeSymbol CodeGenMemberSerializationHooksAttribute { get; } + + private static INamedTypeSymbol GetSymbol(Compilation compilation, Type type) => compilation.GetTypeByMetadataName(type.FullName!) ?? throw new InvalidOperationException($"cannot load symbol of attribute {type}"); + } +} diff --git a/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs b/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs index 29693fe10f0..59e0a87ed8f 100644 --- a/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs +++ b/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs @@ -20,7 +20,7 @@ public class ModelTypeMapping public string[]? Usage { get; } public string[]? Formats { get; } - public ModelTypeMapping(INamedTypeSymbol modelAttribute, INamedTypeSymbol memberAttribute, INamedTypeSymbol serializationAttribute, INamedTypeSymbol serializationHooksAttribute, INamedTypeSymbol? existingType) + public ModelTypeMapping(CodeGenAttributes codeGenAttributes, INamedTypeSymbol? existingType) { _existingType = existingType; _propertyMappings = new(); @@ -32,17 +32,17 @@ public ModelTypeMapping(INamedTypeSymbol modelAttribute, INamedTypeSymbol member { var attributeTypeSymbol = attributeData.AttributeClass; // handle CodeGenMember attribute - if (SymbolEqualityComparer.Default.Equals(attributeTypeSymbol, memberAttribute) && TryGetCodeGenMemberAttributeValue(member, attributeData, out var schemaMemberName)) + if (SymbolEqualityComparer.Default.Equals(attributeTypeSymbol, codeGenAttributes.CodeGenMemberAttribute) && TryGetCodeGenMemberAttributeValue(member, attributeData, out var schemaMemberName)) { _propertyMappings.Add(schemaMemberName, member); } string[]? serializationPath = null; (string? SerializationHook, string? DeserializationHook)? serializationHooks = null; - if (SymbolEqualityComparer.Default.Equals(attributeTypeSymbol, serializationAttribute) && TryGetSerializationAttributeValue(member, attributeData, out var pathResult)) + if (SymbolEqualityComparer.Default.Equals(attributeTypeSymbol, codeGenAttributes.CodeGenMemberSerializationAttribute) && TryGetSerializationAttributeValue(member, attributeData, out var pathResult)) { serializationPath = pathResult; } - if (SymbolEqualityComparer.Default.Equals(attributeTypeSymbol, serializationHooksAttribute) && TryGetSerializationHooks(member, attributeData, out var hooks)) + if (SymbolEqualityComparer.Default.Equals(attributeTypeSymbol, codeGenAttributes.CodeGenMemberSerializationHooksAttribute) && TryGetSerializationHooks(member, attributeData, out var hooks)) { serializationHooks = hooks; } @@ -57,7 +57,8 @@ public ModelTypeMapping(INamedTypeSymbol modelAttribute, INamedTypeSymbol member { foreach (var attributeData in existingType.GetAttributes()) { - if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, modelAttribute)) + var attributeClass = attributeData.AttributeClass; + if (SymbolEqualityComparer.Default.Equals(attributeClass, codeGenAttributes.CodeGenModelAttribute)) { foreach (var namedArgument in attributeData.NamedArguments) { diff --git a/src/AutoRest.CSharp/Common/Input/Source/SourceInputModel.cs b/src/AutoRest.CSharp/Common/Input/Source/SourceInputModel.cs index 109ff97b66b..44617271859 100644 --- a/src/AutoRest.CSharp/Common/Input/Source/SourceInputModel.cs +++ b/src/AutoRest.CSharp/Common/Input/Source/SourceInputModel.cs @@ -15,12 +15,7 @@ public class SourceInputModel { private readonly Compilation _compilation; private readonly CompilationInput? _existingCompilation; - private readonly INamedTypeSymbol _typeAttribute; - private readonly INamedTypeSymbol _modelAttribute; - private readonly INamedTypeSymbol _clientAttribute; - private readonly INamedTypeSymbol _schemaMemberNameAttribute; - private readonly INamedTypeSymbol _serializationAttribute; - private readonly INamedTypeSymbol _serializationHooksAttribute; + private readonly CodeGenAttributes _codeGenAttributes; private readonly Dictionary _nameMap = new Dictionary(StringComparer.OrdinalIgnoreCase); public SourceInputModel(Compilation compilation, CompilationInput? existingCompilation = null) @@ -28,12 +23,7 @@ public SourceInputModel(Compilation compilation, CompilationInput? existingCompi _compilation = compilation; _existingCompilation = existingCompilation; - _schemaMemberNameAttribute = compilation.GetTypeByMetadataName(typeof(CodeGenMemberAttribute).FullName!)!; - _serializationAttribute = compilation.GetTypeByMetadataName(typeof(CodeGenMemberSerializationAttribute).FullName!)!; - _serializationHooksAttribute = compilation.GetTypeByMetadataName(typeof(CodeGenMemberSerializationHooksAttribute).FullName!)!; - _typeAttribute = compilation.GetTypeByMetadataName(typeof(CodeGenTypeAttribute).FullName!)!; - _modelAttribute = compilation.GetTypeByMetadataName(typeof(CodeGenModelAttribute).FullName!)!; - _clientAttribute = compilation.GetTypeByMetadataName(typeof(CodeGenClientAttribute).FullName!)!; + _codeGenAttributes = new CodeGenAttributes(compilation); IAssemblySymbol assembly = _compilation.Assembly; @@ -60,7 +50,7 @@ public SourceInputModel(Compilation compilation, CompilationInput? existingCompi public ModelTypeMapping CreateForModel(INamedTypeSymbol? symbol) { - return new ModelTypeMapping(_modelAttribute, _schemaMemberNameAttribute, _serializationAttribute, _serializationHooksAttribute, symbol); + return new ModelTypeMapping(_codeGenAttributes, symbol); } internal IMethodSymbol? FindMethod(string namespaceName, string typeName, string methodName, IEnumerable parameters) @@ -87,7 +77,7 @@ internal bool TryGetClientSourceInput(INamedTypeSymbol type, [NotNullWhen(true)] var attributeType = attribute.AttributeClass; while (attributeType != null) { - if (SymbolEqualityComparer.Default.Equals(attributeType, _clientAttribute)) + if (SymbolEqualityComparer.Default.Equals(attributeType, _codeGenAttributes.CodeGenClientAttribute)) { INamedTypeSymbol? parentClientType = null; foreach ((var argumentName, TypedConstant constant) in attribute.NamedArguments) @@ -119,7 +109,7 @@ private bool TryGetName(ISymbol symbol, [NotNullWhen(true)] out string? name) var type = attribute.AttributeClass; while (type != null) { - if (SymbolEqualityComparer.Default.Equals(type, _typeAttribute)) + if (SymbolEqualityComparer.Default.Equals(type, _codeGenAttributes.CodeGenTypeAttribute)) { if (attribute?.ConstructorArguments.Length > 0) { From 3176fb8d989552d4f2524ac7c7e32f032ad1db5e Mon Sep 17 00:00:00 2001 From: ArcturusZhang Date: Mon, 29 May 2023 13:03:13 +0800 Subject: [PATCH 2/7] fix the issue when a member has both attributes --- .../Common/Input/Source/ModelTypeMapping.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs b/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs index 59e0a87ed8f..009b950e73f 100644 --- a/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs +++ b/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs @@ -28,6 +28,8 @@ public ModelTypeMapping(CodeGenAttributes codeGenAttributes, INamedTypeSymbol? e foreach (ISymbol member in GetMembers(existingType)) { + string[]? serializationPath = null; + (string? SerializationHook, string? DeserializationHook)? serializationHooks = null; foreach (var attributeData in member.GetAttributes()) { var attributeTypeSymbol = attributeData.AttributeClass; @@ -36,8 +38,6 @@ public ModelTypeMapping(CodeGenAttributes codeGenAttributes, INamedTypeSymbol? e { _propertyMappings.Add(schemaMemberName, member); } - string[]? serializationPath = null; - (string? SerializationHook, string? DeserializationHook)? serializationHooks = null; if (SymbolEqualityComparer.Default.Equals(attributeTypeSymbol, codeGenAttributes.CodeGenMemberSerializationAttribute) && TryGetSerializationAttributeValue(member, attributeData, out var pathResult)) { serializationPath = pathResult; @@ -46,10 +46,10 @@ public ModelTypeMapping(CodeGenAttributes codeGenAttributes, INamedTypeSymbol? e { serializationHooks = hooks; } - if (serializationPath != null || serializationHooks != null) - { - _serializationMappings.Add(member, new SourcePropertySerializationMapping(member, serializationPath, serializationHooks?.SerializationHook, serializationHooks?.DeserializationHook)); - } + } + if (serializationPath != null || serializationHooks != null) + { + _serializationMappings.Add(member, new SourcePropertySerializationMapping(member, serializationPath, serializationHooks?.SerializationHook, serializationHooks?.DeserializationHook)); } } From 5530f1020f1da0e956dfe9e50a2390699643212c Mon Sep 17 00:00:00 2001 From: ArcturusZhang Date: Mon, 29 May 2023 13:06:23 +0800 Subject: [PATCH 3/7] add back the codegen suppress attribute --- src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs b/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs index f92b7ac5999..505fee2ae69 100644 --- a/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs +++ b/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs @@ -11,6 +11,7 @@ public class CodeGenAttributes { public CodeGenAttributes(Compilation compilation) { + CodeGenSuppressAttribute = GetSymbol(compilation, typeof(CodeGenSuppressAttribute)); CodeGenMemberAttribute = GetSymbol(compilation, typeof(CodeGenMemberAttribute)); CodeGenTypeAttribute = GetSymbol(compilation, typeof(CodeGenTypeAttribute)); CodeGenModelAttribute = GetSymbol(compilation, typeof(CodeGenModelAttribute)); @@ -19,6 +20,8 @@ public CodeGenAttributes(Compilation compilation) CodeGenMemberSerializationHooksAttribute = GetSymbol(compilation, typeof(CodeGenMemberSerializationHooksAttribute)); } + public INamedTypeSymbol CodeGenSuppressAttribute { get; } + public INamedTypeSymbol CodeGenMemberAttribute { get; } public INamedTypeSymbol CodeGenTypeAttribute { get; } From 60ba859782128a379f6d00a1bd5df78e443f1320 Mon Sep 17 00:00:00 2001 From: ArcturusZhang Date: Mon, 29 May 2023 13:50:48 +0800 Subject: [PATCH 4/7] more refactor --- .../Common/Input/Source/CodeGenAttributes.cs | 84 ++++++++++++++++++- .../Common/Input/Source/ModelTypeMapping.cs | 70 ++-------------- 2 files changed, 92 insertions(+), 62 deletions(-) diff --git a/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs b/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs index 505fee2ae69..c6723cbb5e3 100644 --- a/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs +++ b/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs @@ -2,8 +2,11 @@ // Licensed under the MIT License. using System; -using Microsoft.CodeAnalysis; +using System.Collections.Immutable; +using System.Diagnostics.CodeAnalysis; +using System.Linq; using Azure.Core; +using Microsoft.CodeAnalysis; namespace AutoRest.CSharp.Input.Source { @@ -35,5 +38,84 @@ public CodeGenAttributes(Compilation compilation) public INamedTypeSymbol CodeGenMemberSerializationHooksAttribute { get; } private static INamedTypeSymbol GetSymbol(Compilation compilation, Type type) => compilation.GetTypeByMetadataName(type.FullName!) ?? throw new InvalidOperationException($"cannot load symbol of attribute {type}"); + + private static bool CheckAttribute(AttributeData attributeData, INamedTypeSymbol codeGenAttribute) + => SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, codeGenAttribute); + + public bool TryGetCodeGenMemberAttributeValue(AttributeData attributeData, [MaybeNullWhen(false)] out string name) + { + name = null; + if (!CheckAttribute(attributeData, CodeGenMemberAttribute)) + return false; + + name = attributeData.ConstructorArguments.FirstOrDefault().Value as string; + return name != null; + } + + public bool TryGetSerializationAttributeValue(AttributeData attributeData, [MaybeNullWhen(false)] out string[] propertyNames) + { + propertyNames = null; + if (!CheckAttribute(attributeData, CodeGenMemberSerializationAttribute)) + return false; + + if (attributeData.ConstructorArguments.Length > 0) + { + propertyNames = ToStringArray(attributeData.ConstructorArguments[0].Values); + } + + return propertyNames != null; + } + + public bool TryGetSerializationHooksAttributeValue(AttributeData attributeData, out (string? SerializationHook, string? DeserializationHook) hooks) + { + hooks = default; + if (!CheckAttribute(attributeData, CodeGenMemberSerializationHooksAttribute)) + return false; + + string? serializationHook = null; + string? deserializationHook = null; + + var arguments = attributeData.ConstructorArguments; + serializationHook = arguments[0].Value as string; + deserializationHook = arguments[1].Value as string; + + hooks = (serializationHook, deserializationHook); + return serializationHook != null || deserializationHook != null; + } + + public bool TryGetCodeGenModelAttributeValue(AttributeData attributeData, out string[]? usage, out string[]? formats) + { + usage = null; + formats = null; + if (!CheckAttribute(attributeData, CodeGenModelAttribute)) + return false; + foreach (var namedArgument in attributeData.NamedArguments) + { + switch (namedArgument.Key) + { + case nameof(Azure.Core.CodeGenModelAttribute.Usage): + usage = ToStringArray(namedArgument.Value.Values); + break; + case nameof(Azure.Core.CodeGenModelAttribute.Formats): + formats = ToStringArray(namedArgument.Value.Values); + break; + } + } + + return true; + } + + private static string[]? ToStringArray(ImmutableArray values) + { + if (values.IsDefaultOrEmpty) + { + return null; + } + + return values + .Select(v => (string?)v.Value) + .OfType() + .ToArray(); + } } } diff --git a/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs b/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs index 009b950e73f..631171bb0ac 100644 --- a/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs +++ b/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs @@ -32,17 +32,18 @@ public ModelTypeMapping(CodeGenAttributes codeGenAttributes, INamedTypeSymbol? e (string? SerializationHook, string? DeserializationHook)? serializationHooks = null; foreach (var attributeData in member.GetAttributes()) { - var attributeTypeSymbol = attributeData.AttributeClass; // handle CodeGenMember attribute - if (SymbolEqualityComparer.Default.Equals(attributeTypeSymbol, codeGenAttributes.CodeGenMemberAttribute) && TryGetCodeGenMemberAttributeValue(member, attributeData, out var schemaMemberName)) + if (codeGenAttributes.TryGetCodeGenMemberAttributeValue(attributeData, out var schemaMemberName)) { _propertyMappings.Add(schemaMemberName, member); } - if (SymbolEqualityComparer.Default.Equals(attributeTypeSymbol, codeGenAttributes.CodeGenMemberSerializationAttribute) && TryGetSerializationAttributeValue(member, attributeData, out var pathResult)) + // handle CodeGenMemberSerialization attribute + if (codeGenAttributes.TryGetSerializationAttributeValue(attributeData, out var pathResult)) { serializationPath = pathResult; } - if (SymbolEqualityComparer.Default.Equals(attributeTypeSymbol, codeGenAttributes.CodeGenMemberSerializationHooksAttribute) && TryGetSerializationHooks(member, attributeData, out var hooks)) + // handle CodeGenMemberSerializationHooks attribute + if (codeGenAttributes.TryGetSerializationHooksAttributeValue(attributeData, out var hooks)) { serializationHooks = hooks; } @@ -57,69 +58,16 @@ public ModelTypeMapping(CodeGenAttributes codeGenAttributes, INamedTypeSymbol? e { foreach (var attributeData in existingType.GetAttributes()) { - var attributeClass = attributeData.AttributeClass; - if (SymbolEqualityComparer.Default.Equals(attributeClass, codeGenAttributes.CodeGenModelAttribute)) + // handle CodeGenModel attribute + if (codeGenAttributes.TryGetCodeGenModelAttributeValue(attributeData, out var usage, out var formats)) { - foreach (var namedArgument in attributeData.NamedArguments) - { - switch (namedArgument.Key) - { - case nameof(CodeGenModelAttribute.Usage): - Usage = ToStringArray(namedArgument.Value.Values); - break; - case nameof(CodeGenModelAttribute.Formats): - Formats = ToStringArray(namedArgument.Value.Values); - break; - } - } + Usage = usage; + Formats = formats; } } } } - private static bool TryGetSerializationHooks(ISymbol symbol, AttributeData attributeData, out (string? SerializationHook, string? DeserializationHook) hooks) - { - string? serializationHook = null; - string? deserializationHook = null; - - var arguments = attributeData.ConstructorArguments; - serializationHook = arguments[0].Value as string; - deserializationHook = arguments[1].Value as string; - - hooks = (serializationHook, deserializationHook); - return serializationHook != null || deserializationHook != null; - } - - private static bool TryGetCodeGenMemberAttributeValue(ISymbol symbol, AttributeData attributeData, [MaybeNullWhen(false)] out string name) - { - name = attributeData.ConstructorArguments.FirstOrDefault().Value as string; - return name != null; - } - - private static bool TryGetSerializationAttributeValue(ISymbol symbol, AttributeData attributeData, [MaybeNullWhen(false)] out string[] propertyNames) - { - propertyNames = null; - if (attributeData.ConstructorArguments.Length > 0) - { - propertyNames = ToStringArray(attributeData.ConstructorArguments[0].Values); - } - - return propertyNames != null; - } - - private static string[]? ToStringArray(ImmutableArray values) - { - if (values.IsDefaultOrEmpty) - { - return null; - } - - return values - .Select(v => (string?)v.Value) - .OfType() - .ToArray(); - } - public SourceMemberMapping? GetForMember(string name) { if (!_propertyMappings.TryGetValue(name, out var memberSymbol)) From 6675e1e02fbb2c7a930e1b5a45a8e494f5c1e451 Mon Sep 17 00:00:00 2001 From: ArcturusZhang Date: Mon, 29 May 2023 14:00:02 +0800 Subject: [PATCH 5/7] remove the null check and make it non-nullable --- .../Common/Input/Source/ModelTypeMapping.cs | 15 ++++++--------- .../Common/Input/Source/SourceInputModel.cs | 5 ++++- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs b/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs index 631171bb0ac..d96d791b511 100644 --- a/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs +++ b/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs @@ -20,7 +20,7 @@ public class ModelTypeMapping public string[]? Usage { get; } public string[]? Formats { get; } - public ModelTypeMapping(CodeGenAttributes codeGenAttributes, INamedTypeSymbol? existingType) + public ModelTypeMapping(CodeGenAttributes codeGenAttributes, INamedTypeSymbol existingType) { _existingType = existingType; _propertyMappings = new(); @@ -54,16 +54,13 @@ public ModelTypeMapping(CodeGenAttributes codeGenAttributes, INamedTypeSymbol? e } } - if (existingType != null) + foreach (var attributeData in existingType.GetAttributes()) { - foreach (var attributeData in existingType.GetAttributes()) + // handle CodeGenModel attribute + if (codeGenAttributes.TryGetCodeGenModelAttributeValue(attributeData, out var usage, out var formats)) { - // handle CodeGenModel attribute - if (codeGenAttributes.TryGetCodeGenModelAttributeValue(attributeData, out var usage, out var formats)) - { - Usage = usage; - Formats = formats; - } + Usage = usage; + Formats = formats; } } } diff --git a/src/AutoRest.CSharp/Common/Input/Source/SourceInputModel.cs b/src/AutoRest.CSharp/Common/Input/Source/SourceInputModel.cs index 44617271859..8e8c84e472f 100644 --- a/src/AutoRest.CSharp/Common/Input/Source/SourceInputModel.cs +++ b/src/AutoRest.CSharp/Common/Input/Source/SourceInputModel.cs @@ -48,8 +48,11 @@ public SourceInputModel(Compilation compilation, CompilationInput? existingCompi return osvAttribute?.ConstructorArguments[0].Values.Select(v => v.Value).OfType().ToList(); } - public ModelTypeMapping CreateForModel(INamedTypeSymbol? symbol) + public ModelTypeMapping? CreateForModel(INamedTypeSymbol? symbol) { + if (symbol == null) + return null; + return new ModelTypeMapping(_codeGenAttributes, symbol); } From 2f491a73da9c9aaf7196f2bad8e607679f100750 Mon Sep 17 00:00:00 2001 From: Arcturus Zhang Date: Tue, 30 May 2023 16:12:55 +0800 Subject: [PATCH 6/7] rename two methods --- src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs | 4 ++-- src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs b/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs index c6723cbb5e3..007ddd334d6 100644 --- a/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs +++ b/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs @@ -52,7 +52,7 @@ public bool TryGetCodeGenMemberAttributeValue(AttributeData attributeData, [Mayb return name != null; } - public bool TryGetSerializationAttributeValue(AttributeData attributeData, [MaybeNullWhen(false)] out string[] propertyNames) + public bool TryGetCodeGenMemberSerializationAttributeValue(AttributeData attributeData, [MaybeNullWhen(false)] out string[] propertyNames) { propertyNames = null; if (!CheckAttribute(attributeData, CodeGenMemberSerializationAttribute)) @@ -66,7 +66,7 @@ public bool TryGetSerializationAttributeValue(AttributeData attributeData, [Mayb return propertyNames != null; } - public bool TryGetSerializationHooksAttributeValue(AttributeData attributeData, out (string? SerializationHook, string? DeserializationHook) hooks) + public bool TryGetCodeGenMemberSerializationHooksAttributeValue(AttributeData attributeData, out (string? SerializationHook, string? DeserializationHook) hooks) { hooks = default; if (!CheckAttribute(attributeData, CodeGenMemberSerializationHooksAttribute)) diff --git a/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs b/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs index d96d791b511..1ef950415b7 100644 --- a/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs +++ b/src/AutoRest.CSharp/Common/Input/Source/ModelTypeMapping.cs @@ -38,12 +38,12 @@ public ModelTypeMapping(CodeGenAttributes codeGenAttributes, INamedTypeSymbol ex _propertyMappings.Add(schemaMemberName, member); } // handle CodeGenMemberSerialization attribute - if (codeGenAttributes.TryGetSerializationAttributeValue(attributeData, out var pathResult)) + if (codeGenAttributes.TryGetCodeGenMemberSerializationAttributeValue(attributeData, out var pathResult)) { serializationPath = pathResult; } // handle CodeGenMemberSerializationHooks attribute - if (codeGenAttributes.TryGetSerializationHooksAttributeValue(attributeData, out var hooks)) + if (codeGenAttributes.TryGetCodeGenMemberSerializationHooksAttributeValue(attributeData, out var hooks)) { serializationHooks = hooks; } From 464d97d43cf2118c725abfe84119d20aa676b67e Mon Sep 17 00:00:00 2001 From: Arcturus Zhang Date: Tue, 30 May 2023 16:52:48 +0800 Subject: [PATCH 7/7] resolve comments --- src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs b/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs index 007ddd334d6..f7d68d4d541 100644 --- a/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs +++ b/src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs @@ -102,7 +102,7 @@ public bool TryGetCodeGenModelAttributeValue(AttributeData attributeData, out st } } - return true; + return usage != null || formats != null; } private static string[]? ToStringArray(ImmutableArray values)