Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use some target typed new #1399

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Microsoft.OpenApi.Hidi/Formatters/PowerShellFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ private static IList<OpenApiParameter> ResolveFunctionParameters(IList<OpenApiPa
// Replace content with a schema object of type array
// for structured or collection-valued function parameters
parameter.Content = null;
parameter.Schema = new OpenApiSchema
parameter.Schema = new()
{
Type = "array",
Items = new OpenApiSchema
Items = new()
{
Type = "string"
}
Expand All @@ -179,7 +179,7 @@ private void AddAdditionalPropertiesToSchema(OpenApiSchema schema)
{
if (schema != null && !_schemaLoop.Contains(schema) && "object".Equals(schema.Type, StringComparison.OrdinalIgnoreCase))
{
schema.AdditionalProperties = new OpenApiSchema { Type = "object" };
schema.AdditionalProperties = new() { Type = "object" };

/* Because 'additionalProperties' are now being walked,
* we need a way to keep track of visited schemas to avoid
Expand Down
16 changes: 8 additions & 8 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
if (options.Output == null)
{
var inputExtension = GetInputPathExtension(options.OpenApi, options.Csdl);
options.Output = new FileInfo($"./output{inputExtension}");
options.Output = new($"./output{inputExtension}");
};

if (options.CleanOutput && options.Output.Exists)
Expand Down Expand Up @@ -194,7 +194,7 @@ private static void WriteOpenApi(HidiOptions options, OpenApiFormat openApiForma

IOpenApiWriter writer = openApiFormat switch
{
OpenApiFormat.Json => options.TerseOutput ? new OpenApiJsonWriter(textWriter, settings, options.TerseOutput) : new OpenApiJsonWriter(textWriter, settings, false),
OpenApiFormat.Json => options.TerseOutput ? new(textWriter, settings, options.TerseOutput) : new OpenApiJsonWriter(textWriter, settings, false),
OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings),
_ => throw new ArgumentException("Unknown format"),
};
Expand Down Expand Up @@ -368,11 +368,11 @@ private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inli
{
stopwatch.Start();

result = await new OpenApiStreamReader(new OpenApiReaderSettings
{
result = await new OpenApiStreamReader(new()
{
LoadExternalRefs = inlineExternal,
BaseUrl = openApiFile.StartsWith("http", StringComparison.OrdinalIgnoreCase) ?
new Uri(openApiFile) :
new(openApiFile) :
new Uri("file://" + new FileInfo(openApiFile).DirectoryName + Path.DirectorySeparatorChar)
}
).ReadAsync(stream, cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -459,7 +459,7 @@ private static Dictionary<string, List<string>> EnumerateJsonDocument(JsonElemen
}
else
{
paths.Add(path, new List<string> {method});
paths.Add(path, new() {method});
}
}
else
Expand Down Expand Up @@ -741,7 +741,7 @@ internal static async Task PluginManifest(HidiOptions options, ILogger logger, C
outputFolder.Create();
}
// Write OpenAPI to Output folder
options.Output = new FileInfo(Path.Combine(options.OutputFolder, "openapi.json"));
options.Output = new(Path.Combine(options.OutputFolder, "openapi.json"));
options.TerseOutput = true;
WriteOpenApi(options, OpenApiFormat.Json, OpenApiSpecVersion.OpenApi3_0, document, logger);

Expand All @@ -762,7 +762,7 @@ internal static async Task PluginManifest(HidiOptions options, ILogger logger, C
// Write OpenAIPluginManifest to Output folder
var manifestFile = new FileInfo(Path.Combine(options.OutputFolder, "ai-plugin.json"));
using var file = new FileStream(manifestFile.FullName, FileMode.Create);
using var jsonWriter = new Utf8JsonWriter(file, new JsonWriterOptions { Indented = true });
using var jsonWriter = new Utf8JsonWriter(file, new() { Indented = true });
manifest.Write(jsonWriter);
await jsonWriter.FlushAsync(cancellationToken).ConfigureAwait(false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Hidi/Options/HidiOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private void ParseHidiOptions(ParseResult parseResult, CommandOptions options)
LogLevel = parseResult.GetValueForOption(options.LogLevelOption);
InlineLocal = parseResult.GetValueForOption(options.InlineLocalOption);
InlineExternal = parseResult.GetValueForOption(options.InlineExternalOption);
FilterOptions = new FilterOptions
FilterOptions = new()
{
FilterByOperationIds = parseResult.GetValueForOption(options.FilterByOperationIdsOption),
FilterByTags = parseResult.GetValueForOption(options.FilterByTagsOption),
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi.Readers/OpenApiDiagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public void AppendDiagnostic(OpenApiDiagnostic diagnosticToAdd, string fileNameT
foreach (var err in diagnosticToAdd.Errors)
{
var errMsgWithFileName = fileNameIsSupplied ? $"[File: {fileNameToAdd}] {err.Message}" : err.Message;
Errors.Add(new OpenApiError(err.Pointer, errMsgWithFileName));
Errors.Add(new(err.Pointer, errMsgWithFileName));
}
foreach (var warn in diagnosticToAdd.Warnings)
{
var warnMsgWithFileName = fileNameIsSupplied ? $"[File: {fileNameToAdd}] {warn.Message}" : warn.Message;
Warnings.Add(new OpenApiError(warn.Pointer, warnMsgWithFileName));
Warnings.Add(new(warn.Pointer, warnMsgWithFileName));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Readers/OpenApiReaderSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class OpenApiReaderSettings
/// <summary>
/// Dictionary of parsers for converting extensions into strongly typed classes
/// </summary>
public Dictionary<string, Func<IOpenApiAny, OpenApiSpecVersion, IOpenApiExtension>> ExtensionParsers { get; set; } = new Dictionary<string, Func<IOpenApiAny, OpenApiSpecVersion, IOpenApiExtension>>();
public Dictionary<string, Func<IOpenApiAny, OpenApiSpecVersion, IOpenApiExtension>> ExtensionParsers { get; set; } = new();

/// <summary>
/// Rules to use for validating OpenAPI specification. If none are provided a default set of rules are applied.
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task<ReadResult> ReadAsync(Stream input, CancellationToken cancella
{
// Buffer stream so that OpenApiTextReaderReader can process it synchronously
// YamlDocument doesn't support async reading.
bufferedStream = new MemoryStream();
bufferedStream = new();
await input.CopyToAsync(bufferedStream, 81920, cancellationToken);
bufferedStream.Position = 0;
}
Expand Down
14 changes: 7 additions & 7 deletions src/Microsoft.OpenApi.Readers/OpenApiTextReaderReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public OpenApiDocument Read(TextReader input, out OpenApiDiagnostic diagnostic)
}
catch (YamlException ex)
{
diagnostic = new OpenApiDiagnostic();
diagnostic.Errors.Add(new OpenApiError($"#line={ex.Start.Line}", ex.Message));
return new OpenApiDocument();
diagnostic = new();
diagnostic.Errors.Add(new($"#line={ex.Start.Line}", ex.Message));
return new();
}

return new OpenApiYamlDocumentReader(this._settings).Read(yamlDocument, out diagnostic);
Expand All @@ -72,8 +72,8 @@ public async Task<ReadResult> ReadAsync(TextReader input, CancellationToken canc
catch (YamlException ex)
{
var diagnostic = new OpenApiDiagnostic();
diagnostic.Errors.Add(new OpenApiError($"#line={ex.Start.Line}", ex.Message));
return new ReadResult
diagnostic.Errors.Add(new($"#line={ex.Start.Line}", ex.Message));
return new()
{
OpenApiDocument = null,
OpenApiDiagnostic = diagnostic
Expand Down Expand Up @@ -101,8 +101,8 @@ public T ReadFragment<T>(TextReader input, OpenApiSpecVersion version, out OpenA
}
catch (YamlException ex)
{
diagnostic = new OpenApiDiagnostic();
diagnostic.Errors.Add(new OpenApiError($"#line={ex.Start.Line}", ex.Message));
diagnostic = new();
diagnostic.Errors.Add(new($"#line={ex.Start.Line}", ex.Message));
return default;
}

Expand Down
14 changes: 7 additions & 7 deletions src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public OpenApiYamlDocumentReader(OpenApiReaderSettings settings = null)
/// <returns>Instance of newly created OpenApiDocument</returns>
public OpenApiDocument Read(YamlDocument input, out OpenApiDiagnostic diagnostic)
{
diagnostic = new OpenApiDiagnostic();
diagnostic = new();
var context = new ParsingContext(diagnostic)
{
ExtensionParsers = _settings.ExtensionParsers,
Expand All @@ -65,7 +65,7 @@ public OpenApiDocument Read(YamlDocument input, out OpenApiDiagnostic diagnostic
}
catch (OpenApiException ex)
{
diagnostic.Errors.Add(new OpenApiError(ex));
diagnostic.Errors.Add(new(ex));
}

// Validate the document
Expand Down Expand Up @@ -115,7 +115,7 @@ public async Task<ReadResult> ReadAsync(YamlDocument input, CancellationToken ca
}
catch (OpenApiException ex)
{
diagnostic.Errors.Add(new OpenApiError(ex));
diagnostic.Errors.Add(new(ex));
}

// Validate the document
Expand All @@ -132,7 +132,7 @@ public async Task<ReadResult> ReadAsync(YamlDocument input, CancellationToken ca
}
}

return new ReadResult
return new()
{
OpenApiDocument = document,
OpenApiDiagnostic = diagnostic
Expand All @@ -147,7 +147,7 @@ private Task<OpenApiDiagnostic> LoadExternalRefs(OpenApiDocument document, Cance
// Load this root document into the workspace
var streamLoader = new DefaultStreamLoader(_settings.BaseUrl);
var workspaceLoader = new OpenApiWorkspaceLoader(openApiWorkSpace, _settings.CustomExternalLoader ?? streamLoader, _settings);
return workspaceLoader.LoadAsync(new OpenApiReference { ExternalResource = "/" }, document, null, cancellationToken);
return workspaceLoader.LoadAsync(new() { ExternalResource = "/" }, document, null, cancellationToken);
}

private void ResolveReferences(OpenApiDiagnostic diagnostic, OpenApiDocument document)
Expand Down Expand Up @@ -181,7 +181,7 @@ private void ResolveReferences(OpenApiDiagnostic diagnostic, OpenApiDocument doc
/// <returns>Instance of newly created OpenApiDocument</returns>
public T ReadFragment<T>(YamlDocument input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic) where T : IOpenApiElement
{
diagnostic = new OpenApiDiagnostic();
diagnostic = new();
var context = new ParsingContext(diagnostic)
{
ExtensionParsers = _settings.ExtensionParsers
Expand All @@ -195,7 +195,7 @@ public T ReadFragment<T>(YamlDocument input, OpenApiSpecVersion version, out Ope
}
catch (OpenApiException ex)
{
diagnostic.Errors.Add(new OpenApiError(ex));
diagnostic.Errors.Add(new(ex));
}

// Validate the element
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi.Readers/ParseNodes/ListNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override List<T> CreateList<T>(Func<MapNode, T> map)
$"Expected list at line {_nodeList.Start.Line} while parsing {typeof(T).Name}", _nodeList);
}

return _nodeList.Select(n => map(new MapNode(Context, n as YamlMappingNode)))
return _nodeList.Select(n => map(new(Context, n as YamlMappingNode)))
.Where(i => i != null)
.ToList();
}
Expand All @@ -49,7 +49,7 @@ public override List<T> CreateSimpleList<T>(Func<ValueNode, T> map)
$"Expected list at line {_nodeList.Start.Line} while parsing {typeof(T).Name}", _nodeList);
}

return _nodeList.Select(n => map(new ValueNode(Context, n))).ToList();
return _nodeList.Select(n => map(new(Context, n))).ToList();
}

public IEnumerator<ParseNode> GetEnumerator()
Expand Down
14 changes: 7 additions & 7 deletions src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public PropertyNode this[string key]
{
if (this._node.Children.TryGetValue(new YamlScalarNode(key), out var node))
{
return new PropertyNode(Context, key, node);
return new(Context, key, node);
}

return null;
Expand All @@ -74,7 +74,7 @@ public override Dictionary<string, T> CreateMap<T>(Func<MapNode, T> map)
Context.StartObject(key);
value = n.Value as YamlMappingNode == null
? default
: map(new MapNode(Context, n.Value as YamlMappingNode));
: map(new(Context, n.Value as YamlMappingNode));
}
finally
{
Expand Down Expand Up @@ -110,7 +110,7 @@ public override Dictionary<string, T> CreateMapWithReference<T>(
Context.StartObject(key);
entry = (
key: key,
value: map(new MapNode(Context, (YamlMappingNode)n.Value))
value: map(new(Context, (YamlMappingNode)n.Value))
);
if (entry.value == null)
{
Expand All @@ -119,7 +119,7 @@ public override Dictionary<string, T> CreateMapWithReference<T>(
// If the component isn't a reference to another component, then point it to itself.
if (entry.value.Reference == null)
{
entry.value.Reference = new OpenApiReference
entry.value.Reference = new()
{
Type = referenceType,
Id = entry.key
Expand Down Expand Up @@ -155,7 +155,7 @@ public override Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
{
throw new OpenApiReaderException($"Expected scalar while parsing {typeof(T).Name}", Context);
}
return (key, value: map(new ValueNode(Context, (YamlScalarNode)n.Value)));
return (key, value: map(new(Context, (YamlScalarNode)n.Value)));
} finally {
Context.EndObject();
}
Expand All @@ -175,14 +175,14 @@ IEnumerator IEnumerable.GetEnumerator()

public override string GetRaw()
{
var x = new Serializer(new SerializerSettings(new JsonSchema()) { EmitJsonComptible = true });
var x = new Serializer(new(new JsonSchema()) { EmitJsonComptible = true });
return x.Serialize(_node);
}

public T GetReferencedObject<T>(ReferenceType referenceType, string referenceId)
where T : IOpenApiReferenceable, new()
{
return new T
return new()
{
UnresolvedReference = true,
Reference = Context.VersionService.ConvertToOpenApiReference(referenceId, referenceType)
Expand Down
11 changes: 5 additions & 6 deletions src/Microsoft.OpenApi.Readers/ParseNodes/PropertyNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Linq;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.Exceptions;
using SharpYaml.Serialization;

Expand Down Expand Up @@ -39,12 +38,12 @@ public void ParseField<T>(
}
catch (OpenApiReaderException ex)
{
Context.Diagnostic.Errors.Add(new OpenApiError(ex));
Context.Diagnostic.Errors.Add(new(ex));
}
catch (OpenApiException ex)
{
ex.Pointer = Context.GetLocation();
Context.Diagnostic.Errors.Add(new OpenApiError(ex));
Context.Diagnostic.Errors.Add(new(ex));
}
finally
{
Expand All @@ -63,12 +62,12 @@ public void ParseField<T>(
}
catch (OpenApiReaderException ex)
{
Context.Diagnostic.Errors.Add(new OpenApiError(ex));
Context.Diagnostic.Errors.Add(new(ex));
}
catch (OpenApiException ex)
{
ex.Pointer = Context.GetLocation();
Context.Diagnostic.Errors.Add(new OpenApiError(ex));
Context.Diagnostic.Errors.Add(new(ex));
}
finally
{
Expand All @@ -78,7 +77,7 @@ public void ParseField<T>(
else
{
Context.Diagnostic.Errors.Add(
new OpenApiError("", $"{Name} is not a valid property at {Context.GetLocation()}"));
new("", $"{Name} is not a valid property at {Context.GetLocation()}"));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Readers/ParseNodes/RootNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ParseNode Find(JsonPointer referencePointer)

public MapNode GetMap()
{
return new MapNode(Context, (YamlMappingNode)_yamlDocument.RootNode);
return new(Context, (YamlMappingNode)_yamlDocument.RootNode);
}
}
}
Loading
Loading