Skip to content

Commit

Permalink
♻️Fix CD pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-mathon committed Jan 6, 2023
1 parent cb566e8 commit 7d180df
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 121 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
with:
dotnet-version: 3.1.x
- name: Publish Criteo.OpenApi.Comparator to NuGet
id: publish_nuget
id: publish_package_nuget
uses: brandedoutcast/publish-nuget@v2
with:
PROJECT_FILE_PATH: src/Criteo.OpenApi.Comparator/Criteo.OpenApi.Comparator.csproj
Expand All @@ -23,7 +23,7 @@ jobs:
PACKAGE_NAME: Criteo.OpenApi.Comparator
INCLUDE_SYMBOLS: true
- name: Publish Criteo.OpenApi.Comparator.Cli to NuGet
id: publish_nuget
id: publish_cli_nuget
uses: brandedoutcast/publish-nuget@v2
with:
PROJECT_FILE_PATH: src/Criteo.OpenApi.Comparator.Cli/Criteo.OpenApi.Comparator.Cli.csproj
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
},
"404": {
"$ref": "#/components/schemas/Pet"
"$ref": "#/components/responses/ErrorResponse"
}
}
}
Expand Down
62 changes: 8 additions & 54 deletions src/Criteo.OpenApi.Comparator/Comparators/ComponentComparator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,22 @@
// Licensed under the Apache 2.0 License. See LICENSE in the project root for license information.

using System;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;

namespace Criteo.OpenApi.Comparator.Comparators
{
/// <summary>
/// Compare the fields that components have in common (e.g. the $ref field)
/// https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#componentsObject
/// </summary>
internal abstract class ComponentComparator
internal static class ComponentComparator<T> where T : IOpenApiReferenceable
{
/// <summary>
/// Compare a modified parameter to an old one and look for breaking as well as non-breaking changes.
/// </summary>
/// <param name="context">The modified document context.</param>
/// <param name="oldParameter">The original parameter to compare.</param>
/// <param name="newParameter">The new parameter to compare.</param>
/// <returns>A list of messages from the comparison.</returns>
protected void Compare(ComparisonContext context, OpenApiParameter oldParameter, OpenApiParameter newParameter)
internal static void Compare(ComparisonContext context, T oldComponent, T newComponent)
{
if (oldParameter == null)
throw new ArgumentNullException(nameof(oldParameter));
if (oldComponent == null)
throw new ArgumentNullException(nameof(oldComponent));

if (newParameter == null)
throw new ArgumentNullException(nameof(newParameter));
if (newComponent == null)
throw new ArgumentNullException(nameof(newComponent));

CompareReference(context, oldParameter.Reference, newParameter.Reference);
}

/// <summary>
/// Compare a modified response to an old one and look for breaking as well as non-breaking changes.
/// </summary>
/// <param name="context">The modified document context.</param>
/// <param name="oldResponse">The original response to compare.</param>
/// <param name="newResponse">The new response to compare.</param>
/// <returns>A list of messages from the comparison.</returns>
protected void Compare(ComparisonContext context, OpenApiResponse oldResponse, OpenApiResponse newResponse)
{
if (oldResponse == null)
throw new ArgumentNullException(nameof(oldResponse));

if (newResponse == null)
throw new ArgumentNullException(nameof(newResponse));

CompareReference(context, oldResponse.Reference, newResponse.Reference);
}

/// <summary>
/// Compare a modified header to an old one and look for breaking as well as non-breaking changes.
/// </summary>
/// <param name="context">The modified document context.</param>
/// <param name="oldHeader">The original header to compare.</param>
/// <param name="newHeader">The new header to compare.</param>
/// <returns>A list of messages from the comparison.</returns>
protected void Compare(ComparisonContext context, OpenApiHeader oldHeader, OpenApiHeader newHeader)
{
if (oldHeader == null)
throw new ArgumentNullException(nameof(oldHeader));

if (newHeader == null)
throw new ArgumentNullException(nameof(newHeader));

CompareReference(context, oldHeader.Reference, newHeader.Reference);
CompareReference(context, oldComponent.Reference, newComponent.Reference);
}

private static void CompareReference(ComparisonContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ internal ContentComparator(SchemaComparator schemaComparator)
}

internal IEnumerable<ComparisonMessage> Compare(ComparisonContext context,
IDictionary<string, OpenApiMediaType> oldContent, IDictionary<string, OpenApiMediaType> newContent)
IDictionary<string, OpenApiMediaType> oldContent,
IDictionary<string, OpenApiMediaType> newContent)
{
oldContent = oldContent ?? new Dictionary<string, OpenApiMediaType>();
newContent = newContent ?? new Dictionary<string, OpenApiMediaType>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@ internal class OpenApiDocumentComparator
{
private readonly OperationComparator _operationComparator;
private readonly SchemaComparator _schemaComparator;
private readonly ContentComparator _contentComparator;
private readonly ParameterComparator _parameterComparator;
private readonly RequestBodyComparator _requestBodyComparator;
private readonly ResponseComparator _responseComparator;

private readonly IDictionary<OpenApiSchema, bool> _isSchemaReferenced = new Dictionary<OpenApiSchema, bool>();

internal OpenApiDocumentComparator()
{
_schemaComparator = new SchemaComparator();
_contentComparator = new ContentComparator(_schemaComparator);
_parameterComparator = new ParameterComparator(_schemaComparator, _contentComparator);
_requestBodyComparator = new RequestBodyComparator(_contentComparator);
_responseComparator = new ResponseComparator(_contentComparator);
_operationComparator = new OperationComparator(_parameterComparator, _requestBodyComparator, _responseComparator);
var contentComparator = new ContentComparator(_schemaComparator);
_parameterComparator = new ParameterComparator(_schemaComparator, contentComparator);
var requestBodyComparator = new RequestBodyComparator(contentComparator);
_responseComparator = new ResponseComparator(contentComparator);
_operationComparator = new OperationComparator(_parameterComparator, requestBodyComparator, _responseComparator);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal OperationComparator(ParameterComparator parameter,
/// <param name="oldOperation">The original operation.</param>
/// <param name="newOperation">The new operation.</param>
/// <returns>A list of messages from the comparison.</returns>
internal IEnumerable<ComparisonMessage> Compare(ComparisonContext context,
internal void Compare(ComparisonContext context,
OpenApiOperation oldOperation,
OpenApiOperation newOperation)
{
Expand All @@ -58,8 +58,6 @@ internal IEnumerable<ComparisonMessage> Compare(ComparisonContext context,
CompareRequestBody(context, oldOperation.RequestBody, newOperation.RequestBody);

CompareExtensions(context, oldOperation.Extensions, newOperation.Extensions);

return context.Messages;
}

/// <summary>
Expand Down
21 changes: 6 additions & 15 deletions src/Criteo.OpenApi.Comparator/Comparators/ParameterComparator.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Copyright (c) Criteo Technology. All rights reserved.
// Licensed under the Apache 2.0 License. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using Criteo.OpenApi.Comparator.Comparators.Extensions;
using Microsoft.OpenApi.Models;

namespace Criteo.OpenApi.Comparator.Comparators
{
internal class ParameterComparator : ComponentComparator
internal class ParameterComparator
{
private readonly SchemaComparator _schemaComparator;
private readonly ContentComparator _contentComparator;
Expand All @@ -22,42 +21,36 @@ internal ParameterComparator(SchemaComparator schemaComparator, ContentComparato
_visitedParameters = new LinkedList<OpenApiParameter>();
}

internal IEnumerable<ComparisonMessage> Compare(
internal void Compare(
ComparisonContext context,
OpenApiParameter oldParameter,
OpenApiParameter newParameter)
{
if (oldParameter == null)
throw new ArgumentNullException(nameof(oldParameter));

if (newParameter == null)
throw new ArgumentNullException(nameof(newParameter));
ComponentComparator<OpenApiParameter>.Compare(context, oldParameter, newParameter);

context.Direction = DataDirection.Request;

base.Compare(context, oldParameter, newParameter);

var areParametersReferenced = false;

if (!string.IsNullOrWhiteSpace(oldParameter.Reference?.ReferenceV3))
{
oldParameter = FindReferencedParameter(oldParameter.Reference, context.OldOpenApiDocument.Components.Parameters);
areParametersReferenced = true;
if (oldParameter == null)
return context.Messages;
return;
}
if (!string.IsNullOrWhiteSpace(newParameter.Reference?.ReferenceV3))
{
newParameter = FindReferencedParameter(newParameter.Reference, context.NewOpenApiDocument.Components.Parameters);
areParametersReferenced = true;
if (newParameter == null)
return context.Messages;
return;
}

if (areParametersReferenced)
{
if (_visitedParameters.Contains(oldParameter))
return context.Messages;
return;

_visitedParameters.AddFirst(oldParameter);
}
Expand All @@ -75,8 +68,6 @@ internal IEnumerable<ComparisonMessage> Compare(
_contentComparator.Compare(context, oldParameter.Content, newParameter.Content);

context.Direction = DataDirection.None;

return context.Messages;
}

private static void CompareIn(ComparisonContext context,
Expand Down
18 changes: 7 additions & 11 deletions src/Criteo.OpenApi.Comparator/Comparators/RequestBodyComparator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,46 @@ internal RequestBodyComparator(ContentComparator contentComparator)
_contentComparator = contentComparator;
}

internal IEnumerable<ComparisonMessage> Compare(ComparisonContext context,
internal void Compare(ComparisonContext context,
OpenApiRequestBody oldRequestBody, OpenApiRequestBody newRequestBody)
{
context.Direction = DataDirection.Request;

if (oldRequestBody == null && newRequestBody == null)
return context.Messages;
return;

if (oldRequestBody == null)
{
context.LogBreakingChange(ComparisonRules.AddedRequestBody);
return context.Messages;
return;
}

if (newRequestBody == null)
{
context.LogBreakingChange(ComparisonRules.RemovedRequestBody);
return context.Messages;
return;
}

if (!string.IsNullOrWhiteSpace(oldRequestBody.Reference?.ReferenceV3))
{
oldRequestBody = oldRequestBody.Reference.Resolve(context.OldOpenApiDocument.Components.RequestBodies);
if (oldRequestBody == null)
return context.Messages;
return;
}

if (!string.IsNullOrWhiteSpace(newRequestBody.Reference?.ReferenceV3))
{
newRequestBody = newRequestBody.Reference.Resolve(context.NewOpenApiDocument.Components.RequestBodies);
if (newRequestBody == null)
return context.Messages;
return;
}

CompareRequired(context, oldRequestBody.Required, newRequestBody.Required);

_contentComparator.Compare(context, oldRequestBody.Content, newRequestBody.Content);

return context.Messages;
}

private static IEnumerable<ComparisonMessage> CompareRequired(ComparisonContext context,
private static void CompareRequired(ComparisonContext context,
bool oldRequired, bool newRequired)
{
if (oldRequired != newRequired)
Expand All @@ -73,8 +71,6 @@ private static IEnumerable<ComparisonMessage> CompareRequired(ComparisonContext
}
context.Pop();
}

return context.Messages;
}
}
}
23 changes: 7 additions & 16 deletions src/Criteo.OpenApi.Comparator/Comparators/ResponseComparator.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Copyright (c) Criteo Technology. All rights reserved.
// Licensed under the Apache 2.0 License. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using Criteo.OpenApi.Comparator.Comparators.Extensions;
using Microsoft.OpenApi.Models;

namespace Criteo.OpenApi.Comparator.Comparators
{
internal class ResponseComparator : ComponentComparator
internal class ResponseComparator
{
private readonly ContentComparator _contentComparator;

Expand All @@ -17,43 +16,35 @@ internal ResponseComparator(ContentComparator contentComparator)
_contentComparator = contentComparator;
}

internal IEnumerable<ComparisonMessage> Compare(ComparisonContext context,
internal void Compare(ComparisonContext context,
OpenApiResponse oldResponse, OpenApiResponse newResponse)
{
if (oldResponse == null)
throw new ArgumentNullException(nameof(oldResponse));

if (newResponse == null)
throw new ArgumentNullException(nameof(newResponse));
ComponentComparator<OpenApiResponse>.Compare(context, oldResponse, newResponse);

context.Direction = DataDirection.Response;

if (!string.IsNullOrWhiteSpace(oldResponse.Reference?.ReferenceV3))
{
oldResponse = oldResponse.Reference.Resolve(context.OldOpenApiDocument.Components.Responses);
if (oldResponse == null)
return context.Messages;
return;
}

if (!string.IsNullOrWhiteSpace(newResponse.Reference?.ReferenceV3))
{
newResponse = newResponse.Reference.Resolve(context.NewOpenApiDocument.Components.Responses);
if (newResponse == null)
return context.Messages;
return;
}

CompareHeaders(context, oldResponse.Headers, newResponse.Headers);

base.Compare(context, oldResponse, newResponse);

_contentComparator.Compare(context, oldResponse.Content, newResponse.Content);

context.Direction = DataDirection.None;

return context.Messages;
}

private void CompareHeaders(ComparisonContext context,
private static void CompareHeaders(ComparisonContext context,
IDictionary<string, OpenApiHeader> oldHeaders,
IDictionary<string, OpenApiHeader> newHeaders)
{
Expand All @@ -70,7 +61,7 @@ private void CompareHeaders(ComparisonContext context,
}
else
{
base.Compare(context, oldHeader, header.Value);
ComponentComparator<OpenApiHeader>.Compare(context, oldHeader, header.Value);
}
context.Pop();
}
Expand Down
Loading

0 comments on commit 7d180df

Please sign in to comment.