Skip to content

Commit

Permalink
feat(cli): strict mode option
Browse files Browse the repository at this point in the history
  • Loading branch information
NextFire committed Jul 10, 2024
1 parent 09439c9 commit 6a4301c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ An OpenAPI tool to compare OpenAPI Specifications.

## C# Library

The tool is available as a [nuget package](https://www.nuget.org/packages/Criteo.OpenApi.Comparator), directly usable into your C# application.
The tool is available as a [nuget package](https://www.nuget.org/packages/Criteo.OpenApi.Comparator), directly usable into your C# application.

To install it run the command:
```bash
Expand All @@ -21,7 +21,7 @@ var differences = OpenApiComparator.Compare(

## Command line tool

The comparator is also available as a [command line tool](https://www.nuget.org/packages/Criteo.OpenApi.Comparator.Cli/0.1.0).
The comparator is also available as a [command line tool](https://www.nuget.org/packages/Criteo.OpenApi.Comparator.Cli/0.1.0).

To install it, run the command:
```bash
Expand All @@ -39,6 +39,7 @@ Available options:
| --old | -o | true | Path to old OpenAPI Specification |
| --new | -n | true | Path to new OpenAPI Specification |
| --outputFormat | -f | false | Specifies in which format the differences should be displayed (default Json). Possible values: Json \| Text. |
| --strict | -s | false | Enable strict mode: breaking changes are errors instead of warnings |
| --help | -h | false | Log available options |

## Comparison rules
Expand Down
8 changes: 7 additions & 1 deletion src/Criteo.OpenApi.Comparator.Cli/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public class Options
/// </summary>
[Option('f', "outputFormat", Required = false, HelpText = "Specifies in which format the differences should be displayed (default Json). Possible values: Json | Text.")]
public OutputFormat OutputFormat { get; set; } = OutputFormat.Json;

/// <summary>
/// Enable strict mode
/// </summary>
[Option('s', "strict", Required = false, HelpText = "Enable strict mode: breaking changes are errors instead of warnings.")]
public bool StrictMode { get; set; }
}

/// <summary>
Expand All @@ -44,4 +50,4 @@ public enum OutputFormat
/// </summary>
Text = 1,
}
}
}
3 changes: 2 additions & 1 deletion src/Criteo.OpenApi.Comparator.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public static int Main(string[] args)
return 1;
}

var differences = OpenApiComparator.Compare(oldOpenApiSpecification, newOpenApiSpecification);
var differences = OpenApiComparator.Compare(
oldOpenApiSpecification, newOpenApiSpecification, options.StrictMode);

DisplayOutput(differences, options.OutputFormat);

Expand Down
5 changes: 3 additions & 2 deletions src/Criteo.OpenApi.Comparator/OpenApiComparator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ public static class OpenApiComparator
/// </summary>
/// <param name="oldOpenApiSpec">The content of the old OpenAPI Specification</param>
/// <param name="newOpenApiSpec">The content of the new OpenAPI Specification</param>
public static IEnumerable<ComparisonMessage> Compare(string oldOpenApiSpec, string newOpenApiSpec)
/// <param name="strict">If true, then breaking changes are errors instead of warnings.</param>
public static IEnumerable<ComparisonMessage> Compare(string oldOpenApiSpec, string newOpenApiSpec, bool strict = false)
{
var oldOpenApiDocument = OpenApiParser.Parse(oldOpenApiSpec);
var newOpenApiDocument = OpenApiParser.Parse(newOpenApiSpec);

var context = new ComparisonContext(oldOpenApiDocument, newOpenApiDocument);
var context = new ComparisonContext(oldOpenApiDocument, newOpenApiDocument) { Strict = strict };

var comparator = new OpenApiDocumentComparator();
var comparisonMessages = comparator.Compare(context, oldOpenApiDocument.Typed, newOpenApiDocument.Typed);
Expand Down

0 comments on commit 6a4301c

Please sign in to comment.