Skip to content

Commit

Permalink
feat(cli): strict mode option (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
NextFire authored Jul 10, 2024
1 parent 09439c9 commit 962f5b0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
17 changes: 9 additions & 8 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 @@ -34,12 +34,13 @@ openapi-compare -o new_oas.json -n old_oas.json -f Json
```

Available options:
| Option | Small | Required | Description |
|----------------|-------|----------|--------------------------------------------------------------------------------------------------------------|
| --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. |
| --help | -h | false | Log available options |
| Option | Small | Required | Description |
|----------------|-------|----------|---------------------------------------------------------------------------------------------------------------|
| --old | -o | true | Path or URL to old OpenAPI Specification. |
| --new | -n | true | Path or URL to new OpenAPI Specification. |
| --outputFormat | -f | false | (Default: Json) Specifies in which format the differences should be displayed. Possible values: Json \| Text. |
| --strict | -s | false | (Default: false) Enable strict mode: breaking changes are errors instead of warnings. |
| --help | -h | false | Log available options |

## Comparison rules

Expand Down
18 changes: 14 additions & 4 deletions src/Criteo.OpenApi.Comparator.Cli/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,30 @@ public class Options
/// <summary>
/// Path to old OpenAPI Specification
/// </summary>
[Option('o', "old", Required = true, HelpText = "Path or URL to old OpenAPI Specification.")]
[Option('o', "old", Required = true,
HelpText = "Path or URL to old OpenAPI Specification.")]
public string OldSpec { get; set; }

/// <summary>
/// Path to new OpenAPI Specification
/// </summary>
[Option('n', "new", Required = true, HelpText = "Path or URL to new OpenAPI Specification.")]
[Option('n', "new", Required = true,
HelpText = "Path or URL to new OpenAPI Specification.")]
public string NewSpec { get; set; }

/// <summary>
/// Specifies in which format the differences should be displayed (default Json)
/// </summary>
[Option('f', "outputFormat", Required = false, HelpText = "Specifies in which format the differences should be displayed (default Json). Possible values: Json | Text.")]
[Option('f', "outputFormat", Required = false, Default = OutputFormat.Json,
HelpText = "Specifies in which format the differences should be displayed. Possible values: Json | Text.")]
public OutputFormat OutputFormat { get; set; } = OutputFormat.Json;

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

/// <summary>
Expand All @@ -44,4 +54,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 962f5b0

Please sign in to comment.