Skip to content

Commit 7aefc0a

Browse files
committed
Fixed issues identified by sonarCube
1 parent bd3a51e commit 7aefc0a

File tree

3 files changed

+35
-47
lines changed

3 files changed

+35
-47
lines changed

src/Microsoft.OpenApi.Hidi/Formatters/PowerShellFormatter.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,35 +149,29 @@ private static string RemoveKeyTypeSegment(string operationId, IList<OpenApiPara
149149
foreach (var parameter in parameters)
150150
{
151151
var keyTypeExtension = parameter.Extensions.GetExtension("x-ms-docs-key-type");
152-
if (keyTypeExtension != null)
152+
if (keyTypeExtension != null && operationId.Contains(keyTypeExtension))
153153
{
154-
if (operationId.Contains(keyTypeExtension))
155-
{
156-
segments.Remove(keyTypeExtension);
157-
}
154+
segments.Remove(keyTypeExtension);
158155
}
159156
}
160157
return string.Join(".", segments);
161158
}
162159

163160
private static IList<OpenApiParameter> ResolveFunctionParameters(IList<OpenApiParameter> parameters)
164161
{
165-
foreach (var parameter in parameters)
162+
foreach (var parameter in parameters.Where(p => p.Content?.Any() ?? false))
166163
{
167-
if (parameter.Content?.Any() ?? false)
164+
// Replace content with a schema object of type array
165+
// for structured or collection-valued function parameters
166+
parameter.Content = null;
167+
parameter.Schema = new OpenApiSchema
168168
{
169-
// Replace content with a schema object of type array
170-
// for structured or collection-valued function parameters
171-
parameter.Content = null;
172-
parameter.Schema = new OpenApiSchema
169+
Type = "array",
170+
Items = new OpenApiSchema
173171
{
174-
Type = "array",
175-
Items = new OpenApiSchema
176-
{
177-
Type = "string"
178-
}
179-
};
180-
}
172+
Type = "string"
173+
}
174+
};
181175
}
182176
return parameters;
183177
}

src/Microsoft.OpenApi.Hidi/Options/CommandOptions.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@ namespace Microsoft.OpenApi.Hidi.Options
88
internal class CommandOptions
99
{
1010
// command option parameters and aliases
11-
public Option<string> OpenApiDescriptionOption = new("--openapi", "Input OpenAPI description file path or URL");
12-
public Option<string> CsdlOption = new("--csdl", "Input CSDL file path or URL");
13-
public Option<string> CsdlFilterOption = new("--csdl-filter", "Comma delimited list of EntitySets or Singletons to filter CSDL on. e.g. tasks,accounts");
14-
public Option<FileInfo> OutputOption = new("--output", "The output file path for the generated file.") { Arity = ArgumentArity.ZeroOrOne };
15-
public Option<string> OutputFolderOption = new("--output-folder", "The output directory path for the generated files.") { Arity = ArgumentArity.ZeroOrOne };
16-
public Option<bool> CleanOutputOption = new("--clean-output", "Overwrite an existing file");
17-
public Option<string> VersionOption = new("--version", "OpenAPI specification version");
18-
public Option<string> MetadataVersionOption = new("--metadata-version", "Graph metadata version to use.");
19-
public Option<OpenApiFormat?> FormatOption = new("--format", "File format");
20-
public Option<bool> TerseOutputOption = new("--terse-output", "Produce terse json output");
21-
public Option<string> SettingsFileOption = new("--settings-path", "The configuration file with CSDL conversion settings.");
22-
public Option<LogLevel> LogLevelOption = new("--log-level", () => LogLevel.Information, "The log level to use when logging messages to the main output.");
23-
public Option<string> FilterByOperationIdsOption = new("--filter-by-operationids", "Filters OpenApiDocument by comma delimited list of OperationId(s) provided");
24-
public Option<string> FilterByTagsOption = new("--filter-by-tags", "Filters OpenApiDocument by comma delimited list of Tag(s) provided. Also accepts a single regex.");
25-
public Option<string> FilterByCollectionOption = new("--filter-by-collection", "Filters OpenApiDocument by Postman collection provided. Provide path to collection file.");
26-
public Option<string> ManifestOption = new("--manifest", "Path to API manifest file to locate and filter an OpenApiDocument");
27-
public Option<bool> InlineLocalOption = new("--inline-local", "Inline local $ref instances");
28-
public Option<bool> InlineExternalOption = new("--inline-external", "Inline external $ref instances");
11+
public readonly Option<string> OpenApiDescriptionOption = new("--openapi", "Input OpenAPI description file path or URL");
12+
public readonly Option<string> CsdlOption = new("--csdl", "Input CSDL file path or URL");
13+
public readonly Option<string> CsdlFilterOption = new("--csdl-filter", "Comma delimited list of EntitySets or Singletons to filter CSDL on. e.g. tasks,accounts");
14+
public readonly Option<FileInfo> OutputOption = new("--output", "The output file path for the generated file.") { Arity = ArgumentArity.ZeroOrOne };
15+
public readonly Option<string> OutputFolderOption = new("--output-folder", "The output directory path for the generated files.") { Arity = ArgumentArity.ZeroOrOne };
16+
public readonly Option<bool> CleanOutputOption = new("--clean-output", "Overwrite an existing file");
17+
public readonly Option<string> VersionOption = new("--version", "OpenAPI specification version");
18+
public readonly Option<string> MetadataVersionOption = new("--metadata-version", "Graph metadata version to use.");
19+
public readonly Option<OpenApiFormat?> FormatOption = new("--format", "File format");
20+
public readonly Option<bool> TerseOutputOption = new("--terse-output", "Produce terse json output");
21+
public readonly Option<string> SettingsFileOption = new("--settings-path", "The configuration file with CSDL conversion settings.");
22+
public readonly Option<LogLevel> LogLevelOption = new("--log-level", () => LogLevel.Information, "The log level to use when logging messages to the main output.");
23+
public readonly Option<string> FilterByOperationIdsOption = new("--filter-by-operationids", "Filters OpenApiDocument by comma delimited list of OperationId(s) provided");
24+
public readonly Option<string> FilterByTagsOption = new("--filter-by-tags", "Filters OpenApiDocument by comma delimited list of Tag(s) provided. Also accepts a single regex.");
25+
public readonly Option<string> FilterByCollectionOption = new("--filter-by-collection", "Filters OpenApiDocument by Postman collection provided. Provide path to collection file.");
26+
public readonly Option<string> ManifestOption = new("--manifest", "Path to API manifest file to locate and filter an OpenApiDocument");
27+
public readonly Option<bool> InlineLocalOption = new("--inline-local", "Inline local $ref instances");
28+
public readonly Option<bool> InlineExternalOption = new("--inline-external", "Inline external $ref instances");
2929

3030
public CommandOptions()
3131
{

src/Microsoft.OpenApi/Services/OpenApiFilterService.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,11 @@ private static bool AddReferences(OpenApiComponents newComponents, OpenApiCompon
334334
}
335335
}
336336

337-
foreach (var item in newComponents.RequestBodies)
337+
foreach (var item in newComponents.RequestBodies
338+
.Where(item => !target.RequestBodies.ContainsKey(item.Key)))
338339
{
339-
if (!target.RequestBodies.ContainsKey(item.Key))
340-
{
341340
moreStuff = true;
342341
target.RequestBodies.Add(item);
343-
}
344342
}
345343
return moreStuff;
346344
}
@@ -350,13 +348,9 @@ private static string ExtractPath(string url, IList<OpenApiServer> serverList)
350348
// if OpenAPI has servers, then see if the url matches one of them
351349
var baseUrl = serverList.Select(s => s.Url.TrimEnd('/')).Where(c => url.Contains(c)).FirstOrDefault();
352350

353-
if (baseUrl == null) {
354-
// if no match, then extract path from either the absolute or relative url
355-
return new Uri(new Uri("http://localhost/"), url).GetComponents(UriComponents.Path | UriComponents.KeepDelimiter, UriFormat.Unescaped);
356-
} else {
357-
// if match, then extract path from the url relative to the matched server
358-
return url.Split(new[] { baseUrl }, StringSplitOptions.None)[1];
359-
}
351+
return baseUrl == null ?
352+
new Uri(new Uri("http://localhost/"), url).GetComponents(UriComponents.Path | UriComponents.KeepDelimiter, UriFormat.Unescaped)
353+
: url.Split(new[] { baseUrl }, StringSplitOptions.None)[1];
360354
}
361355
}
362356
}

0 commit comments

Comments
 (0)