Skip to content

Commit a7a79fd

Browse files
authored
Merge pull request #5 from MobileTeleSystems/fix_ca_issue
Fix CA issues
2 parents 6454f40 + d1add16 commit a7a79fd

File tree

21 files changed

+68
-19
lines changed

21 files changed

+68
-19
lines changed

.editorconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ dotnet_diagnostic.SA1200.severity = none
276276
dotnet_diagnostic.SA1309.severity = none
277277
dotnet_diagnostic.SA1310.severity = none
278278
dotnet_diagnostic.SA1311.severity = none
279-
dotnet_diagnostic.SA1503.severity = none
280279
dotnet_diagnostic.SA1600.severity = none
281280
dotnet_diagnostic.SA1623.severity = none
282281
dotnet_diagnostic.SA1633.severity = silent

ApiCodeGenerator.ruleset

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
<Rule Id="SA1309" Action="None" />
8181
<Rule Id="SA1310" Action="None" />
8282
<Rule Id="SA1311" Action="None" />
83-
<Rule Id="SA1503" Action="None" />
8483
<Rule Id="SA1600" Action="None" />
8584
<Rule Id="SA1623" Action="None" />
8685
<Rule Id="SA1633" Action="Hidden" />

src/ApiCodeGenerator.Abstraction/AssemblyResolver.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,18 @@ public static void Unregister()
113113
{
114114
var thisAsm = Assembly.GetCallingAssembly();
115115
if (assemblyName == thisAsm.GetName())
116+
{
116117
return null;
118+
}
117119

118120
var path = _resolvers.Select(r => r.Invoke(assemblyName)).FirstOrDefault(p => p != null);
119121

120122
var key = assemblyName.Name;
121123

122124
if (path == null && _asmPaths.TryGetValue(key, out var item))
125+
{
123126
path = item.Path;
127+
}
124128

125129
return path == null
126130
? null

src/ApiCodeGenerator.Abstraction/GeneratorContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal GeneratorContext(
3131

3232
public ILogger? Logger { get; internal set; }
3333

34-
public T? GetSettings<T>(JsonSerializer? jsonSerializer = null, IReadOnlyDictionary<string, string>? additionalVariables = null)
34+
public T? GetSettings<T>(JsonSerializer? jsonSerializer, IReadOnlyDictionary<string, string>? additionalVariables)
3535
where T : class
3636
=> (T?)_settingsFactory(typeof(T), jsonSerializer, additionalVariables);
3737
}

src/ApiCodeGenerator.AsyncApi/CSharp/CSharpGeneratorBaseSettings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ protected CSharpGeneratorBaseSettings()
1818

1919
CSharpGeneratorSettings.TemplateFactory = new DefaultTemplateFactory(CSharpGeneratorSettings, new[]
2020
{
21-
typeof(CSharpGeneratorBaseSettings).GetType().Assembly,
22-
typeof(CSharpGeneratorSettings).GetType().Assembly,
21+
GetType().Assembly,
22+
typeof(CSharpGeneratorSettings).Assembly,
2323
});
2424

2525
ResponseArrayType = "System.Collections.Generic.ICollection";

src/ApiCodeGenerator.AsyncApi/CSharp/Models/CSharpOperationModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ public CSharpOperationModel(
5252

5353
public CSharpParameterModel[] Parameters { get; }
5454

55-
public string PayloadType => _payloadType ??= ResolvePayloadType(Operation.Message.ActualObject.Payload.ActualSchema);
55+
public string PayloadType => _payloadType ??= ResolvePayloadType(Operation.Message.ActualObject.Payload.ActualSchema, hint: null);
5656

5757
protected Channel Channel { get; }
5858

5959
protected Operation Operation { get; }
6060

61-
protected virtual string ResolvePayloadType(JsonSchema jsonSchema, string? hint = null)
61+
protected virtual string ResolvePayloadType(JsonSchema jsonSchema, string? hint)
6262
{
6363
if (!jsonSchema.HasTypeNameTitle && string.IsNullOrEmpty(hint))
6464
{

src/ApiCodeGenerator.AsyncApi/DOM/Serialization/InheritanceConverter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object? exis
5757
/// <inheritdoc/>
5858
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
5959
{
60+
throw new NotSupportedException();
6061
}
6162

6263
private static Dictionary<string, Func<T>> GetFactories()

src/ApiCodeGenerator.Core/ApiDocumentProvider.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,41 @@ public ApiDocumentProvider(IFileProvider fileProvider, HttpClient httpClient)
4343
else
4444
{
4545
if (Uri.TryCreate(documentSource.FromDocument.Url, UriKind.Absolute, out var url))
46+
{
4647
return await FromUrlAsync(url);
48+
}
4749
else
50+
{
4851
return Failed("Invalid url format", null);
52+
}
4953
}
5054
}
5155
else
5256
{
5357
var json = documentSource.FromDocument.Json!;
5458
if (IsPath(json))
59+
{
5560
return await FromFileAsync(json);
61+
}
5662
else
63+
{
5764
return FromContent(json, null);
65+
}
5866
}
5967
}
6068

6169
if (documentSource.JsonSchemaToOpenApi != null)
6270
{
6371
var data = documentSource.JsonSchemaToOpenApi;
6472
if (string.IsNullOrEmpty(data.Name))
73+
{
6574
return Failed("jsonSchemaToOpenApi.name must be not null or empty", null);
75+
}
6676

6777
if (string.IsNullOrEmpty(data.Schema))
78+
{
6879
return Failed("jsonSchemaToOpenApi.schema must be not null or empty", null);
80+
}
6981

7082
if (IsPath(data.Schema))
7183
{

src/ApiCodeGenerator.Core/ExtensionManager/ExtensionLoader.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,15 @@ private static void GetAndMergeDict<T>(Type type, string propName, Dictionary<st
6666
if (propInfo != null)
6767
{
6868
if (!typeof(IDictionary<string, T>).IsAssignableFrom(propInfo.PropertyType))
69+
{
6970
throw new InvalidOperationException($"Property {propName} in assembly {type.Assembly.FullName} must return type IDictionary<string, {typeof(T)}>");
71+
}
7072

7173
var value = (IDictionary<string, T>)propInfo.GetValue(type, null);
7274
foreach (var kv in value)
75+
{
7376
dict.Add(kv.Key, kv.Value);
77+
}
7478
}
7579
}
7680

@@ -80,7 +84,9 @@ private static void GetAndMergeDictOfList<T>(Type type, string propName, Diction
8084
if (propInfo != null)
8185
{
8286
if (!typeof(IDictionary<string, T>).IsAssignableFrom(propInfo.PropertyType))
87+
{
8388
throw new InvalidOperationException($"Property {propName} in assembly {type.Assembly.FullName} must return type IDictionary<string, {typeof(T)}>");
89+
}
8490

8591
var value = (IDictionary<string, T>)propInfo.GetValue(type, null);
8692
if (value.Any())

src/ApiCodeGenerator.Core/ExtensionManager/Extensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ApiCodeGenerator.Core.ExtensionManager
1010
/// <summary>
1111
/// Информация о расширениях.
1212
/// </summary>
13-
public class Extensions : IExtensions
13+
internal class Extensions : IExtensions
1414
{
1515
public Extensions(
1616
IReadOnlyDictionary<string, ContentGeneratorFactory>? codeGenerators = null,

src/ApiCodeGenerator.Core/NswagDocument/Converters/VariableConverter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public override bool CanConvert(Type objectType)
3434
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
3535
{
3636
if (reader.Value != null)
37+
{
3738
return _replaceExpression.Replace((string)reader.Value, Replace);
39+
}
3840

3941
return null;
4042
}

src/ApiCodeGenerator.Core/NswagDocument/PreprocessorHelper.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,29 @@ internal static class PreprocessorHelper
1313
public static Preprocessors GetPreprocessors(IExtensions? extensions, IDictionary<string, JObject?>? processors, ILogger? log)
1414
{
1515
if (processors == null)
16+
{
1617
return new Preprocessors(new Dictionary<Type, Delegate[]>());
18+
}
1719

1820
var result = new Dictionary<Type, List<Delegate>>();
1921
foreach (var name in processors.Keys)
2022
{
2123
var value = processors[name]?.ToString(Formatting.None);
2224

2325
if (extensions?.Preprocessors.TryGetValue(name, out var type) != true)
26+
{
2427
throw new InvalidOperationException($"Preprocessor with name '{name}' not registred.");
28+
}
2529

2630
foreach (var (dataType, dlgt) in CreatePreprocessors(name, type, value, log))
2731
if (result.TryGetValue(dataType, out var list))
32+
{
2833
list.Add(dlgt);
34+
}
2935
else
36+
{
3037
result[dataType] = new List<Delegate> { dlgt };
38+
}
3139
}
3240

3341
return new Preprocessors(result.ToDictionary(i => i.Key, i => i.Value.ToArray()));
@@ -41,7 +49,9 @@ public static Preprocessors GetPreprocessors(IExtensions? extensions, IDictionar
4149
var ctor = type.GetConstructors().First();
4250
var ctorParams = ctor.GetParameters();
4351
if (ctorParams.Length > 1 || (ctorParams.Length == 1 && ctorParams[0].ParameterType != typeof(string)))
52+
{
4453
throw new InvalidOperationException($"Constructor with one or zero parameters not found for preprocessor '{name}'");
54+
}
4555

4656
var processor = ctor.GetParameters().Length == 0
4757
? Activator.CreateInstance(type)

src/ApiCodeGenerator.Core/NswagDocumentFactory.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ public Document LoadNswagDocument(TextReader reader, IReadOnlyDictionary<string,
8585
};
8686

8787
if (variables?.Any() == true)
88+
{
8889
settings.Converters.Add(new VariableConverter(variables));
90+
}
8991

9092
var jsonSerializer = JsonSerializer.Create(settings);
9193
Document? document;
@@ -137,7 +139,9 @@ void MergeObject(IContractResolver contractResolver, JObject doc, JObject baseDo
137139
curSrcToken = curSrcToken[curProp.PropertyName] as JObject;
138140

139141
if (curSrcToken is null || curTargToken is null)
142+
{
140143
break;
144+
}
141145

142146
curContract = contractResolver.ResolveContract(curProp.PropertyType);
143147
}

src/ApiCodeGenerator.Core/PhysicalFileProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public async Task WriteAllTextAsync(string path, string contents)
2626
{
2727
var outDir = Path.GetDirectoryName(path);
2828
if (!string.IsNullOrEmpty(outDir) && !Directory.Exists(outDir))
29+
{
2930
Directory.CreateDirectory(outDir);
31+
}
3032

3133
using var stream = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None);
3234
using var writer = new StreamWriter(stream);

src/ApiCodeGenerator.OpenApi/ContentGeneratorBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ protected static T InvokePreprocessors<T>(T data,
9898
return data;
9999
}
100100

101-
protected static TSettings ParseSettings(GeneratorContext context, IReadOnlyDictionary<string, string>? variables = null)
101+
protected static TSettings ParseSettings(GeneratorContext context, IReadOnlyDictionary<string, string>? variables)
102102
{
103103
var unwrapProps = new[]
104104
{

src/ApiCodeGenerator.OpenApi/Converters/SettingsConverter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,19 @@ void FillProperties(object targetObject, params string[] unwrapProp)
104104
{
105105
var pos = Array.IndexOf(unwrapProp, p.Name);
106106
if (pos > -1)
107+
{
107108
forUnwrap[pos] = p;
109+
}
108110
else
111+
{
109112
props[p.Name] = (p, targetObject);
113+
}
110114
}
111115

112116
foreach (var item in forUnwrap.Where(pi => pi != null))
117+
{
113118
FillProperties(item.GetValue(targetObject), unwrapProp);
119+
}
114120
}
115121
}
116122
}

src/ApiCodeGenerator.OpenApi/DefaultTemplateFactory.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public interface ITemplateProvider
122122
{
123123
string? GetFullName(string name, string language);
124124

125-
string? GetTemplateText(string fullName, string language);
125+
string? GetTemplateText(string templateFullName, string language);
126126
}
127127

128128
[System.Diagnostics.CodeAnalysis.SuppressMessage("Ordering Rules", "SA1402", Justification = "none")]
@@ -145,9 +145,9 @@ public EmbededResourceTemplateProvider(Assembly assembly, string resourcePrefix)
145145
: null;
146146
}
147147

148-
public string? GetTemplateText(string resourceName, string language)
148+
public string? GetTemplateText(string templateFullName, string language)
149149
{
150-
var resource = _assembly.GetManifestResourceStream(resourceName);
150+
var resource = _assembly.GetManifestResourceStream(templateFullName);
151151
if (resource != null)
152152
{
153153
using var reader = new StreamReader(resource);
@@ -181,11 +181,11 @@ public DirectoryTemplateProvider(string path)
181181
return null;
182182
}
183183

184-
public string? GetTemplateText(string templateFilePath, string language)
184+
public string? GetTemplateText(string templateFullName, string language)
185185
{
186-
if (File.Exists(templateFilePath))
186+
if (File.Exists(templateFullName))
187187
{
188-
return File.ReadAllText(templateFilePath);
188+
return File.ReadAllText(templateFullName);
189189
}
190190

191191
return null;

src/ApiCodeGenerator.OpenApi/Helpers/SettingsHelpers.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@ public static void SetSpecialSettings(IExtensions? extensions, ClientGeneratorBa
2323
{
2424
var mode = ((JToken?)value)?.Value<string>();
2525
if (mode is not null)
26+
{
2627
SetOperationMode(extensions, settings, mode);
28+
}
2729
}
2830
else if (propertyName.Equals("replaceNameCollection", StringComparison.OrdinalIgnoreCase))
2931
{
3032
if (((JToken?)value)?.Type == JTokenType.Object)
3133
{
3234
var replacementData = ((JObject)value).ToObject<IDictionary<string, string>>();
3335
if (replacementData is not null)
36+
{
3437
SetReplaceCollection(settings, replacementData);
38+
}
3539
}
3640
else
3741
{

test/ApiCodeGenerator.Core.Tests/GeneratorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public async Task RunCodeGeneration()
172172
Assert.True(result);
173173
Assert.NotNull(generatorMock.Object.Context);
174174
Assert.NotNull(generatorMock.Object.Context.DocumentReader);
175-
Assert.NotNull(generatorMock.Object.Context.GetSettings<JObject>());
175+
Assert.NotNull(generatorMock.Object.Context.GetSettings<JObject>(null, null));
176176
generatorMock.Verify(g => g.Generate(), Times.Once);
177177
_fileProviderMock.Verify(fp => fp.WriteAllTextAsync(It.Is<string>(v => v == OutFilePath), It.IsAny<string>()), Times.Once);
178178
}

test/ApiCodeGenerator.Core.Tests/Infrastructure/FakeCodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal class FakeCodeGenerator : IContentGenerator
1212
public FakeCodeGenerator(GeneratorContext context, Dictionary<string, string>? additionalVars = null)
1313
{
1414
Context = context;
15-
Settings = context.GetSettings<FakeCodeGeneratorSettings>(additionalVariables: additionalVars);
15+
Settings = context.GetSettings<FakeCodeGeneratorSettings>(null, additionalVariables: additionalVars);
1616
}
1717

1818
public GeneratorContext Context { get; }

test/ApiCodeGenerator.OpenApi.Tests/DefaultTemplateFactoryTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ public TestTemplateProvider(string templateText)
4949
_templateText = templateText;
5050
}
5151

52-
public string? GetFullName(string name, string language)
52+
string? ITemplateProvider.GetFullName(string name, string language)
5353
{
5454
return name == "Class"
5555
? $"{_providerKey}.{name}"
5656
: null;
5757
}
5858

59-
public string? GetTemplateText(string name, string language)
59+
string? ITemplateProvider.GetTemplateText(string name, string language)
6060
{
6161
if (name.StartsWith(_providerKey))
6262
{

0 commit comments

Comments
 (0)