diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ab38dd31b..f63cb09f6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added support for tracing in Python. [#1872](https://github.com/microsoft/kiota/issues/1872) - Added auto-generated comment for TypeScript generation. [#3244](https://github.com/microsoft/kiota/issues/3244) - Added a new switch to exclude all assets generated only for backward compatibility. [#2952](https://github.com/microsoft/kiota/issues/2952) +- Remove `--json-no-indent` option in favor of `RAW_JSON` output format which does the same job. (CLI) ### Changed diff --git a/src/Kiota.Builder/Writers/CLI/CliCodeMethodWriter.cs b/src/Kiota.Builder/Writers/CLI/CliCodeMethodWriter.cs index 449d7e1931..e32288c2f5 100644 --- a/src/Kiota.Builder/Writers/CLI/CliCodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/CLI/CliCodeMethodWriter.cs @@ -35,8 +35,6 @@ partial class CliCodeMethodWriter : CodeMethodWriter private const string RequestAdapterParamType = "IRequestAdapter"; private const string RequestAdapterParamName = "reqAdapter"; private const string BuilderInstanceName = "builder"; - private const string JsonNoIndentParamType = "bool"; - private const string JsonNoIndentParamName = "jsonNoIndent"; private const string InvocationContextParamName = "invocationContext"; private const string CommandVariableName = "command"; private const string ExecCommandsVariableName = "execCommands"; @@ -305,10 +303,7 @@ private void AddCustomCommandOptions(LanguageWriter writer, ref List ava { // Add output type param var outputOptionName = "outputOption"; - writer.WriteLine($"var {outputOptionName} = new Option<{OutputFormatParamType}>(\"--{OutputFormatParamName}\", () => FormatterType.JSON){{"); - writer.IncreaseIndent(); - writer.WriteLine("IsRequired = true"); - writer.CloseBlock("};"); + writer.WriteLine($"var {outputOptionName} = new Option<{OutputFormatParamType}>(\"--{OutputFormatParamName}\", () => FormatterType.PRETTY_JSON);"); writer.WriteLine($"{CommandVariableName}.AddOption({outputOptionName});"); parameters.Add((OutputFormatParamType, OutputFormatParamName, null)); availableOptions.Add($"{InvocationContextParamName}.ParseResult.GetValueForOption({outputOptionName})"); @@ -320,21 +315,6 @@ private void AddCustomCommandOptions(LanguageWriter writer, ref List ava parameters.Add((OutputFilterQueryParamType, OutputFilterQueryParamName, null)); availableOptions.Add($"{InvocationContextParamName}.ParseResult.GetValueForOption({outputFilterQueryOptionName})"); - // Add JSON no-indent option - var jsonNoIndentOptionName = $"{JsonNoIndentParamName}Option"; - writer.WriteLine($"var {jsonNoIndentOptionName} = new Option(\"--{NormalizeToOption(JsonNoIndentParamName)}\", r => {{"); - writer.IncreaseIndent(); - writer.WriteLine("if (bool.TryParse(r.Tokens.Select(t => t.Value).LastOrDefault(), out var value)) {"); - writer.IncreaseIndent(); - writer.WriteLine("return value;"); - writer.CloseBlock(); - writer.WriteLine("return true;"); - writer.DecreaseIndent(); - writer.WriteLine("}, description: \"Disable indentation for the JSON output formatter.\");"); - writer.WriteLine($"{CommandVariableName}.AddOption({jsonNoIndentOptionName});"); - parameters.Add((JsonNoIndentParamType, JsonNoIndentParamName, null)); - availableOptions.Add($"{InvocationContextParamName}.ParseResult.GetValueForOption({jsonNoIndentOptionName})"); - // Add --all option if (isPageable) { @@ -357,21 +337,15 @@ private void WriteCommandHandlerBodyOutput(LanguageWriter writer, in CodeMethod { var formatterVar = "formatter"; - var formatterOptionsVar = "formatterOptions"; if (originalMethod.PagingInformation != null) { - writer.WriteLine($"IOutputFormatterOptions? {formatterOptionsVar} = null;"); writer.WriteLine($"IOutputFormatter? {formatterVar} = null;"); } if (originalMethod.ReturnType is CodeType type && conventions.GetTypeString(type, originalMethod) is { } typeString && !typeString.Equals("Stream", StringComparison.Ordinal)) { var formatterTypeVal = "FormatterType.TEXT"; - if (conventions.IsPrimitiveType(typeString)) - { - formatterOptionsVar = "null"; - } - else + if (!conventions.IsPrimitiveType(typeString)) { if (originalMethod.PagingInformation != null) { @@ -383,12 +357,6 @@ private void WriteCommandHandlerBodyOutput(LanguageWriter writer, in CodeMethod formatterTypeVal = OutputFormatParamName; string canFilterExpr = $"(response != Stream.Null)"; writer.WriteLine($"response = {canFilterExpr} ? await {OutputFilterParamName}.FilterOutputAsync(response, {OutputFilterQueryParamName}, {CancellationTokenParamName}) : response;"); - if (originalMethod.PagingInformation == null) - { - writer.Write("var "); - } - writer.Write($"{formatterOptionsVar} = {OutputFormatParamName}.GetOutputFormatterOptions(new FormatterOptionsModel(!{JsonNoIndentParamName}));", originalMethod.PagingInformation != null); - writer.WriteLine(); if (originalMethod.PagingInformation != null) { @@ -404,7 +372,7 @@ private void WriteCommandHandlerBodyOutput(LanguageWriter writer, in CodeMethod writer.WriteLine($"var {formatterVar} = {OutputFormatterFactoryParamName}.GetFormatter({formatterTypeVal});"); } - writer.WriteLine($"await {formatterVar}.WriteOutputAsync(response, {formatterOptionsVar}, {CancellationTokenParamName});"); + writer.WriteLine($"await {formatterVar}.WriteOutputAsync(response, {CancellationTokenParamName});"); } else { @@ -664,7 +632,7 @@ protected virtual void WriteCommandHandlerBody(CodeMethod codeElement, CodeClass // Check for null model // Add logging with reason for skipped execution here - writer.StartBlock("if (model is null)"); + writer.StartBlock("if (model is null) {"); writer.WriteLine("Console.Error.WriteLine(\"No model data to send.\");"); writer.WriteLine("return;"); writer.CloseBlock(); @@ -678,7 +646,7 @@ protected virtual void WriteCommandHandlerBody(CodeMethod codeElement, CodeClass requestBodyParam.Name = "stream"; // Check for file existence // Add logging with reason for skipped execution here - writer.StartBlock($"if ({pName} is null || !{pName}.Exists)"); + writer.StartBlock($"if ({pName} is null || !{pName}.Exists) {{"); writer.WriteLine("Console.Error.WriteLine(\"No available file to send.\");"); writer.WriteLine("return;"); writer.CloseBlock(); diff --git a/tests/Kiota.Builder.Tests/Writers/CLI/CliCodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/CLI/CliCodeMethodWriterTests.cs index 02e6d4d7b2..b2ff04bc3f 100644 --- a/tests/Kiota.Builder.Tests/Writers/CLI/CliCodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/CLI/CliCodeMethodWriterTests.cs @@ -814,7 +814,7 @@ public void WritesExecutableCommandWithRelatedLinksInDescription() Assert.Contains("var response = await reqAdapter.SendPrimitiveAsync(requestInfo, errorMapping: default, cancellationToken: cancellationToken) ?? Stream.Null;", result); Assert.Contains("IOutputFormatterFactory outputFormatterFactory = invocationContext.BindingContext.GetService(typeof(IOutputFormatterFactory)) as IOutputFormatterFactory ?? throw new ArgumentNullException(\"outputFormatterFactory\");", result); Assert.Contains("var formatter = outputFormatterFactory.GetFormatter(FormatterType.TEXT);", result); - Assert.Contains("await formatter.WriteOutputAsync(response, null, cancellationToken);", result); + Assert.Contains("await formatter.WriteOutputAsync(response, cancellationToken);", result); Assert.Contains("});", result); Assert.Contains("return command;", result); } @@ -883,7 +883,7 @@ public void WritesExecutableCommandForGetRequestPrimitive() Assert.Contains("var response = await reqAdapter.SendPrimitiveAsync(requestInfo, errorMapping: default, cancellationToken: cancellationToken) ?? Stream.Null;", result); Assert.Contains("IOutputFormatterFactory outputFormatterFactory = invocationContext.BindingContext.GetService(typeof(IOutputFormatterFactory)) as IOutputFormatterFactory ?? throw new ArgumentNullException(\"outputFormatterFactory\");", result); Assert.Contains("var formatter = outputFormatterFactory.GetFormatter(FormatterType.TEXT);", result); - Assert.Contains("await formatter.WriteOutputAsync(response, null, cancellationToken);", result); + Assert.Contains("await formatter.WriteOutputAsync(response, cancellationToken);", result); Assert.Contains("});", result); Assert.Contains("return command;", result); } @@ -939,10 +939,8 @@ public void WritesExecutableCommandForPagedGetRequestModel() Assert.Contains("command.Description = \"Test description\";", result); Assert.Contains("var qOption = new Option(\"-q\", getDefaultValue: ()=> \"test\", description: \"The q option\")", result); Assert.Contains("qOption.IsRequired = false;", result); - Assert.Contains("var jsonNoIndentOption = new Option(\"--json-no-indent\", r => {", result); Assert.Contains("var allOption = new Option(\"--all\")", result); Assert.Contains("command.AddOption(qOption);", result); - Assert.Contains("command.AddOption(jsonNoIndentOption);", result); Assert.Contains("command.AddOption(outputOption);", result); Assert.Contains("command.AddOption(allOption);", result); Assert.Contains("command.SetHandler(async (invocationContext) => {", result); @@ -953,13 +951,12 @@ public void WritesExecutableCommandForPagedGetRequestModel() Assert.Contains("var pagingData = new PageLinkData(requestInfo, null, itemName: \"item\", nextLinkName: \"nextLink\");", result); Assert.Contains("var reqAdapter = invocationContext.GetRequestAdapter()", result); Assert.Contains("var pageResponse = await pagingService.GetPagedDataAsync((info, token) => reqAdapter.SendNoContentAsync(info, cancellationToken: token), pagingData, all, cancellationToken);", result); - Assert.Contains("formatterOptions = output.GetOutputFormatterOptions(new FormatterOptionsModel(!jsonNoIndent));", result); Assert.Contains("IOutputFormatter? formatter = null;", result); Assert.Contains("if (pageResponse?.StatusCode >= 200 && pageResponse?.StatusCode < 300) {", result); Assert.Contains("formatter = outputFormatterFactory.GetFormatter(output);", result); Assert.Contains("response = (response != Stream.Null) ? await outputFilter.FilterOutputAsync(response, query, cancellationToken) : response", result); Assert.Contains("formatter = outputFormatterFactory.GetFormatter(FormatterType.TEXT);", result); - Assert.Contains("await formatter.WriteOutputAsync(response, formatterOptions, cancellationToken);", result); + Assert.Contains("await formatter.WriteOutputAsync(response, cancellationToken);", result); Assert.Contains("});", result); Assert.Contains("return command;", result); } @@ -1010,9 +1007,7 @@ public void WritesExecutableCommandForGetRequestModel() Assert.Contains("command.Description = \"Test description\";", result); Assert.Contains("var qOption = new Option(\"-q\", getDefaultValue: ()=> \"test\", description: \"The q option\")", result); Assert.Contains("qOption.IsRequired = false;", result); - Assert.Contains("var jsonNoIndentOption = new Option(\"--json-no-indent\", r => {", result); Assert.Contains("command.AddOption(qOption);", result); - Assert.Contains("command.AddOption(jsonNoIndentOption);", result); Assert.Contains("command.AddOption(outputOption);", result); Assert.Contains("command.SetHandler(async (invocationContext) => {", result); Assert.Contains("var q = invocationContext.ParseResult.GetValueForOption(qOption);", result); @@ -1020,9 +1015,8 @@ public void WritesExecutableCommandForGetRequestModel() Assert.Contains("if (testPath is not null) requestInfo.PathParameters.Add(\"test%2Dpath\", testPath);", result); Assert.Contains("var reqAdapter = invocationContext.GetRequestAdapter()", result); Assert.Contains("var response = await reqAdapter.SendPrimitiveAsync(requestInfo, errorMapping: default, cancellationToken: cancellationToken) ?? Stream.Null;", result); - Assert.Contains("var formatterOptions = output.GetOutputFormatterOptions(new FormatterOptionsModel(!jsonNoIndent));", result); Assert.Contains("response = (response != Stream.Null) ? await outputFilter.FilterOutputAsync(response, query, cancellationToken) : response", result); - Assert.Contains("await formatter.WriteOutputAsync(response, formatterOptions, cancellationToken);", result); + Assert.Contains("await formatter.WriteOutputAsync(response, cancellationToken);", result); Assert.Contains("});", result); Assert.Contains("return command;", result); } @@ -1277,7 +1271,6 @@ public void WritesExecutableCommandForDeleteRequest() Assert.Contains("Console.WriteLine(\"Success\");", result); Assert.Contains("return command;", result); Assert.DoesNotContain("command.AddOption(outputOption);", result); - Assert.DoesNotContain("var jsonNoIndentOption = new Option(\"--json-no-indent\", r => {", result); } [Fact] @@ -1391,6 +1384,6 @@ public void WritesExecutableCommandForPostVoidRequest() Assert.Contains("Console.WriteLine(\"Success\");", result); Assert.Contains("return command;", result); Assert.DoesNotContain("response = (response != Stream.Null) ? await outputFilter.FilterOutputAsync(response, query, cancellationToken) : response", result); - Assert.DoesNotContain("await formatter.WriteOutputAsync(response, formatterOptions, cancellationToken);", result); + Assert.DoesNotContain("await formatter.WriteOutputAsync(response, cancellationToken);", result); } }