diff --git a/.editorconfig b/.editorconfig index 305d1bc..3a3fc52 100644 --- a/.editorconfig +++ b/.editorconfig @@ -276,7 +276,6 @@ dotnet_diagnostic.SA1200.severity = none dotnet_diagnostic.SA1309.severity = none dotnet_diagnostic.SA1310.severity = none dotnet_diagnostic.SA1311.severity = none -dotnet_diagnostic.SA1503.severity = none dotnet_diagnostic.SA1600.severity = none dotnet_diagnostic.SA1623.severity = none dotnet_diagnostic.SA1633.severity = silent diff --git a/ApiCodeGenerator.ruleset b/ApiCodeGenerator.ruleset index 28e7949..f48be9e 100644 --- a/ApiCodeGenerator.ruleset +++ b/ApiCodeGenerator.ruleset @@ -80,7 +80,6 @@ - diff --git a/src/ApiCodeGenerator.Abstraction/AssemblyResolver.cs b/src/ApiCodeGenerator.Abstraction/AssemblyResolver.cs index 38903fc..f5dde37 100644 --- a/src/ApiCodeGenerator.Abstraction/AssemblyResolver.cs +++ b/src/ApiCodeGenerator.Abstraction/AssemblyResolver.cs @@ -113,14 +113,18 @@ public static void Unregister() { var thisAsm = Assembly.GetCallingAssembly(); if (assemblyName == thisAsm.GetName()) + { return null; + } var path = _resolvers.Select(r => r.Invoke(assemblyName)).FirstOrDefault(p => p != null); var key = assemblyName.Name; if (path == null && _asmPaths.TryGetValue(key, out var item)) + { path = item.Path; + } return path == null ? null diff --git a/src/ApiCodeGenerator.Abstraction/GeneratorContext.cs b/src/ApiCodeGenerator.Abstraction/GeneratorContext.cs index ef7a716..bb140fc 100644 --- a/src/ApiCodeGenerator.Abstraction/GeneratorContext.cs +++ b/src/ApiCodeGenerator.Abstraction/GeneratorContext.cs @@ -31,7 +31,7 @@ internal GeneratorContext( public ILogger? Logger { get; internal set; } - public T? GetSettings(JsonSerializer? jsonSerializer = null, IReadOnlyDictionary? additionalVariables = null) + public T? GetSettings(JsonSerializer? jsonSerializer, IReadOnlyDictionary? additionalVariables) where T : class => (T?)_settingsFactory(typeof(T), jsonSerializer, additionalVariables); } diff --git a/src/ApiCodeGenerator.AsyncApi/CSharp/CSharpGeneratorBaseSettings.cs b/src/ApiCodeGenerator.AsyncApi/CSharp/CSharpGeneratorBaseSettings.cs index 5f7dd1b..76a4f9e 100644 --- a/src/ApiCodeGenerator.AsyncApi/CSharp/CSharpGeneratorBaseSettings.cs +++ b/src/ApiCodeGenerator.AsyncApi/CSharp/CSharpGeneratorBaseSettings.cs @@ -18,8 +18,8 @@ protected CSharpGeneratorBaseSettings() CSharpGeneratorSettings.TemplateFactory = new DefaultTemplateFactory(CSharpGeneratorSettings, new[] { - typeof(CSharpGeneratorBaseSettings).GetType().Assembly, - typeof(CSharpGeneratorSettings).GetType().Assembly, + GetType().Assembly, + typeof(CSharpGeneratorSettings).Assembly, }); ResponseArrayType = "System.Collections.Generic.ICollection"; diff --git a/src/ApiCodeGenerator.AsyncApi/CSharp/Models/CSharpOperationModel.cs b/src/ApiCodeGenerator.AsyncApi/CSharp/Models/CSharpOperationModel.cs index da3ee8b..9a7ce72 100644 --- a/src/ApiCodeGenerator.AsyncApi/CSharp/Models/CSharpOperationModel.cs +++ b/src/ApiCodeGenerator.AsyncApi/CSharp/Models/CSharpOperationModel.cs @@ -52,13 +52,13 @@ public CSharpOperationModel( public CSharpParameterModel[] Parameters { get; } - public string PayloadType => _payloadType ??= ResolvePayloadType(Operation.Message.ActualObject.Payload.ActualSchema); + public string PayloadType => _payloadType ??= ResolvePayloadType(Operation.Message.ActualObject.Payload.ActualSchema, hint: null); protected Channel Channel { get; } protected Operation Operation { get; } - protected virtual string ResolvePayloadType(JsonSchema jsonSchema, string? hint = null) + protected virtual string ResolvePayloadType(JsonSchema jsonSchema, string? hint) { if (!jsonSchema.HasTypeNameTitle && string.IsNullOrEmpty(hint)) { diff --git a/src/ApiCodeGenerator.AsyncApi/DOM/Serialization/InheritanceConverter.cs b/src/ApiCodeGenerator.AsyncApi/DOM/Serialization/InheritanceConverter.cs index e37706e..51d17e8 100644 --- a/src/ApiCodeGenerator.AsyncApi/DOM/Serialization/InheritanceConverter.cs +++ b/src/ApiCodeGenerator.AsyncApi/DOM/Serialization/InheritanceConverter.cs @@ -57,6 +57,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object? exis /// public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) { + throw new NotSupportedException(); } private static Dictionary> GetFactories() diff --git a/src/ApiCodeGenerator.Core/ApiDocumentProvider.cs b/src/ApiCodeGenerator.Core/ApiDocumentProvider.cs index 224f098..8e83581 100644 --- a/src/ApiCodeGenerator.Core/ApiDocumentProvider.cs +++ b/src/ApiCodeGenerator.Core/ApiDocumentProvider.cs @@ -43,18 +43,26 @@ public ApiDocumentProvider(IFileProvider fileProvider, HttpClient httpClient) else { if (Uri.TryCreate(documentSource.FromDocument.Url, UriKind.Absolute, out var url)) + { return await FromUrlAsync(url); + } else + { return Failed("Invalid url format", null); + } } } else { var json = documentSource.FromDocument.Json!; if (IsPath(json)) + { return await FromFileAsync(json); + } else + { return FromContent(json, null); + } } } @@ -62,10 +70,14 @@ public ApiDocumentProvider(IFileProvider fileProvider, HttpClient httpClient) { var data = documentSource.JsonSchemaToOpenApi; if (string.IsNullOrEmpty(data.Name)) + { return Failed("jsonSchemaToOpenApi.name must be not null or empty", null); + } if (string.IsNullOrEmpty(data.Schema)) + { return Failed("jsonSchemaToOpenApi.schema must be not null or empty", null); + } if (IsPath(data.Schema)) { diff --git a/src/ApiCodeGenerator.Core/ExtensionManager/ExtensionLoader.cs b/src/ApiCodeGenerator.Core/ExtensionManager/ExtensionLoader.cs index cb0d329..1ab2f6d 100644 --- a/src/ApiCodeGenerator.Core/ExtensionManager/ExtensionLoader.cs +++ b/src/ApiCodeGenerator.Core/ExtensionManager/ExtensionLoader.cs @@ -66,11 +66,15 @@ private static void GetAndMergeDict(Type type, string propName, Dictionary).IsAssignableFrom(propInfo.PropertyType)) + { throw new InvalidOperationException($"Property {propName} in assembly {type.Assembly.FullName} must return type IDictionary"); + } var value = (IDictionary)propInfo.GetValue(type, null); foreach (var kv in value) + { dict.Add(kv.Key, kv.Value); + } } } @@ -80,7 +84,9 @@ private static void GetAndMergeDictOfList(Type type, string propName, Diction if (propInfo != null) { if (!typeof(IDictionary).IsAssignableFrom(propInfo.PropertyType)) + { throw new InvalidOperationException($"Property {propName} in assembly {type.Assembly.FullName} must return type IDictionary"); + } var value = (IDictionary)propInfo.GetValue(type, null); if (value.Any()) diff --git a/src/ApiCodeGenerator.Core/ExtensionManager/Extensions.cs b/src/ApiCodeGenerator.Core/ExtensionManager/Extensions.cs index 9001bb7..c5194d9 100644 --- a/src/ApiCodeGenerator.Core/ExtensionManager/Extensions.cs +++ b/src/ApiCodeGenerator.Core/ExtensionManager/Extensions.cs @@ -10,7 +10,7 @@ namespace ApiCodeGenerator.Core.ExtensionManager /// /// Информация о расширениях. /// - public class Extensions : IExtensions + internal class Extensions : IExtensions { public Extensions( IReadOnlyDictionary? codeGenerators = null, diff --git a/src/ApiCodeGenerator.Core/NswagDocument/Converters/VariableConverter.cs b/src/ApiCodeGenerator.Core/NswagDocument/Converters/VariableConverter.cs index 79dbe64..4e403ec 100644 --- a/src/ApiCodeGenerator.Core/NswagDocument/Converters/VariableConverter.cs +++ b/src/ApiCodeGenerator.Core/NswagDocument/Converters/VariableConverter.cs @@ -34,7 +34,9 @@ public override bool CanConvert(Type objectType) public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) { if (reader.Value != null) + { return _replaceExpression.Replace((string)reader.Value, Replace); + } return null; } diff --git a/src/ApiCodeGenerator.Core/NswagDocument/PreprocessorHelper.cs b/src/ApiCodeGenerator.Core/NswagDocument/PreprocessorHelper.cs index ad4766a..fec1792 100644 --- a/src/ApiCodeGenerator.Core/NswagDocument/PreprocessorHelper.cs +++ b/src/ApiCodeGenerator.Core/NswagDocument/PreprocessorHelper.cs @@ -13,7 +13,9 @@ internal static class PreprocessorHelper public static Preprocessors GetPreprocessors(IExtensions? extensions, IDictionary? processors, ILogger? log) { if (processors == null) + { return new Preprocessors(new Dictionary()); + } var result = new Dictionary>(); foreach (var name in processors.Keys) @@ -21,13 +23,19 @@ public static Preprocessors GetPreprocessors(IExtensions? extensions, IDictionar var value = processors[name]?.ToString(Formatting.None); if (extensions?.Preprocessors.TryGetValue(name, out var type) != true) + { throw new InvalidOperationException($"Preprocessor with name '{name}' not registred."); + } foreach (var (dataType, dlgt) in CreatePreprocessors(name, type, value, log)) if (result.TryGetValue(dataType, out var list)) + { list.Add(dlgt); + } else + { result[dataType] = new List { dlgt }; + } } return new Preprocessors(result.ToDictionary(i => i.Key, i => i.Value.ToArray())); @@ -41,7 +49,9 @@ public static Preprocessors GetPreprocessors(IExtensions? extensions, IDictionar var ctor = type.GetConstructors().First(); var ctorParams = ctor.GetParameters(); if (ctorParams.Length > 1 || (ctorParams.Length == 1 && ctorParams[0].ParameterType != typeof(string))) + { throw new InvalidOperationException($"Constructor with one or zero parameters not found for preprocessor '{name}'"); + } var processor = ctor.GetParameters().Length == 0 ? Activator.CreateInstance(type) diff --git a/src/ApiCodeGenerator.Core/NswagDocumentFactory.cs b/src/ApiCodeGenerator.Core/NswagDocumentFactory.cs index e98147d..ea90871 100644 --- a/src/ApiCodeGenerator.Core/NswagDocumentFactory.cs +++ b/src/ApiCodeGenerator.Core/NswagDocumentFactory.cs @@ -85,7 +85,9 @@ public Document LoadNswagDocument(TextReader reader, IReadOnlyDictionary(T data, return data; } - protected static TSettings ParseSettings(GeneratorContext context, IReadOnlyDictionary? variables = null) + protected static TSettings ParseSettings(GeneratorContext context, IReadOnlyDictionary? variables) { var unwrapProps = new[] { diff --git a/src/ApiCodeGenerator.OpenApi/Converters/SettingsConverter.cs b/src/ApiCodeGenerator.OpenApi/Converters/SettingsConverter.cs index 0ee7271..4a6178a 100644 --- a/src/ApiCodeGenerator.OpenApi/Converters/SettingsConverter.cs +++ b/src/ApiCodeGenerator.OpenApi/Converters/SettingsConverter.cs @@ -104,13 +104,19 @@ void FillProperties(object targetObject, params string[] unwrapProp) { var pos = Array.IndexOf(unwrapProp, p.Name); if (pos > -1) + { forUnwrap[pos] = p; + } else + { props[p.Name] = (p, targetObject); + } } foreach (var item in forUnwrap.Where(pi => pi != null)) + { FillProperties(item.GetValue(targetObject), unwrapProp); + } } } } diff --git a/src/ApiCodeGenerator.OpenApi/DefaultTemplateFactory.cs b/src/ApiCodeGenerator.OpenApi/DefaultTemplateFactory.cs index c0e5456..095203c 100644 --- a/src/ApiCodeGenerator.OpenApi/DefaultTemplateFactory.cs +++ b/src/ApiCodeGenerator.OpenApi/DefaultTemplateFactory.cs @@ -122,7 +122,7 @@ public interface ITemplateProvider { string? GetFullName(string name, string language); - string? GetTemplateText(string fullName, string language); + string? GetTemplateText(string templateFullName, string language); } [System.Diagnostics.CodeAnalysis.SuppressMessage("Ordering Rules", "SA1402", Justification = "none")] @@ -145,9 +145,9 @@ public EmbededResourceTemplateProvider(Assembly assembly, string resourcePrefix) : null; } - public string? GetTemplateText(string resourceName, string language) + public string? GetTemplateText(string templateFullName, string language) { - var resource = _assembly.GetManifestResourceStream(resourceName); + var resource = _assembly.GetManifestResourceStream(templateFullName); if (resource != null) { using var reader = new StreamReader(resource); @@ -181,11 +181,11 @@ public DirectoryTemplateProvider(string path) return null; } - public string? GetTemplateText(string templateFilePath, string language) + public string? GetTemplateText(string templateFullName, string language) { - if (File.Exists(templateFilePath)) + if (File.Exists(templateFullName)) { - return File.ReadAllText(templateFilePath); + return File.ReadAllText(templateFullName); } return null; diff --git a/src/ApiCodeGenerator.OpenApi/Helpers/SettingsHelpers.cs b/src/ApiCodeGenerator.OpenApi/Helpers/SettingsHelpers.cs index 5dc4426..b68e7ec 100644 --- a/src/ApiCodeGenerator.OpenApi/Helpers/SettingsHelpers.cs +++ b/src/ApiCodeGenerator.OpenApi/Helpers/SettingsHelpers.cs @@ -23,7 +23,9 @@ public static void SetSpecialSettings(IExtensions? extensions, ClientGeneratorBa { var mode = ((JToken?)value)?.Value(); if (mode is not null) + { SetOperationMode(extensions, settings, mode); + } } else if (propertyName.Equals("replaceNameCollection", StringComparison.OrdinalIgnoreCase)) { @@ -31,7 +33,9 @@ public static void SetSpecialSettings(IExtensions? extensions, ClientGeneratorBa { var replacementData = ((JObject)value).ToObject>(); if (replacementData is not null) + { SetReplaceCollection(settings, replacementData); + } } else { diff --git a/test/ApiCodeGenerator.Core.Tests/GeneratorTests.cs b/test/ApiCodeGenerator.Core.Tests/GeneratorTests.cs index 8c387ec..bd38ce7 100644 --- a/test/ApiCodeGenerator.Core.Tests/GeneratorTests.cs +++ b/test/ApiCodeGenerator.Core.Tests/GeneratorTests.cs @@ -172,7 +172,7 @@ public async Task RunCodeGeneration() Assert.True(result); Assert.NotNull(generatorMock.Object.Context); Assert.NotNull(generatorMock.Object.Context.DocumentReader); - Assert.NotNull(generatorMock.Object.Context.GetSettings()); + Assert.NotNull(generatorMock.Object.Context.GetSettings(null, null)); generatorMock.Verify(g => g.Generate(), Times.Once); _fileProviderMock.Verify(fp => fp.WriteAllTextAsync(It.Is(v => v == OutFilePath), It.IsAny()), Times.Once); } diff --git a/test/ApiCodeGenerator.Core.Tests/Infrastructure/FakeCodeGenerator.cs b/test/ApiCodeGenerator.Core.Tests/Infrastructure/FakeCodeGenerator.cs index b31f41a..12801c2 100644 --- a/test/ApiCodeGenerator.Core.Tests/Infrastructure/FakeCodeGenerator.cs +++ b/test/ApiCodeGenerator.Core.Tests/Infrastructure/FakeCodeGenerator.cs @@ -12,7 +12,7 @@ internal class FakeCodeGenerator : IContentGenerator public FakeCodeGenerator(GeneratorContext context, Dictionary? additionalVars = null) { Context = context; - Settings = context.GetSettings(additionalVariables: additionalVars); + Settings = context.GetSettings(null, additionalVariables: additionalVars); } public GeneratorContext Context { get; } diff --git a/test/ApiCodeGenerator.OpenApi.Tests/DefaultTemplateFactoryTests.cs b/test/ApiCodeGenerator.OpenApi.Tests/DefaultTemplateFactoryTests.cs index d57d4f3..9acf6e6 100644 --- a/test/ApiCodeGenerator.OpenApi.Tests/DefaultTemplateFactoryTests.cs +++ b/test/ApiCodeGenerator.OpenApi.Tests/DefaultTemplateFactoryTests.cs @@ -49,14 +49,14 @@ public TestTemplateProvider(string templateText) _templateText = templateText; } - public string? GetFullName(string name, string language) + string? ITemplateProvider.GetFullName(string name, string language) { return name == "Class" ? $"{_providerKey}.{name}" : null; } - public string? GetTemplateText(string name, string language) + string? ITemplateProvider.GetTemplateText(string name, string language) { if (name.StartsWith(_providerKey)) {