-
Notifications
You must be signed in to change notification settings - Fork 280
Open api schema diff #316
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
Merged
Merged
Open api schema diff #316
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2717e6e
- Updated model to store OpenApiDifference
Shwetap05 c76d277
Merge branch 'vnext' into DiffPreview
Shwetap05 a3f3537
Fix typo.
Shwetap05 0f2626b
Merge branch 'vnext' into DiffPreview
Shwetap05 3c3e74f
- Added comparer for Schema, RequestBody, Responses, MediaType
Shwetap05 f59c308
More diff work
Shwetap05 4a66112
Merge
Shwetap05 91a905d
Fix review comments
Shwetap05 73d1208
Fix per review comments
Shwetap05 497d8a1
Added more tests
Shwetap05 536ea5c
Fix test failure
Shwetap05 af90775
Fix pointer to include schema in path
Shwetap05 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/Microsoft.OpenApi/Services/OpenApiComponentsComparer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using Microsoft.OpenApi.Models; | ||
|
|
||
| namespace Microsoft.OpenApi.Services | ||
| { | ||
| /// <summary> | ||
| /// Defines behavior for comparing properties of <see cref="OpenApiComponents"/>. | ||
| /// </summary> | ||
| public class OpenApiComponentsComparer : OpenApiComparerBase<OpenApiComponents> | ||
| { | ||
| /// <summary> | ||
| /// Executes comparision against source and target <see cref="OpenApiComponents"/>. | ||
| /// </summary> | ||
| /// <param name="sourceComponents">The source.</param> | ||
| /// <param name="targetComponents">The target.</param> | ||
| /// <param name="comparisonContext">Context under which to compare the source and target.</param> | ||
| public override void Compare( | ||
| OpenApiComponents sourceComponents, | ||
| OpenApiComponents targetComponents, | ||
| ComparisonContext comparisonContext) | ||
| { | ||
| if (sourceComponents == null && targetComponents == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (sourceComponents == null || targetComponents == null) | ||
| { | ||
| comparisonContext.AddOpenApiDifference( | ||
| new OpenApiDifference | ||
| { | ||
| OpenApiDifferenceOperation = OpenApiDifferenceOperation.Update, | ||
| SourceValue = sourceComponents, | ||
| TargetValue = targetComponents, | ||
| OpenApiComparedElementType = typeof(OpenApiComponents), | ||
| Pointer = comparisonContext.PathString | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| WalkAndCompare( | ||
| comparisonContext, | ||
| OpenApiConstants.Parameters, | ||
| () => comparisonContext | ||
| .GetComparer<IDictionary<string, OpenApiParameter>>() | ||
| .Compare(sourceComponents.Parameters, targetComponents.Parameters, comparisonContext)); | ||
|
|
||
| WalkAndCompare( | ||
| comparisonContext, | ||
| OpenApiConstants.RequestBodies, | ||
| () => comparisonContext | ||
| .GetComparer<IDictionary<string, OpenApiRequestBody>>() | ||
| .Compare(sourceComponents.RequestBodies, targetComponents.RequestBodies, comparisonContext)); | ||
|
|
||
| WalkAndCompare( | ||
| comparisonContext, | ||
| OpenApiConstants.Responses, | ||
| () => comparisonContext | ||
| .GetComparer<IDictionary<string, OpenApiResponse>>() | ||
| .Compare(sourceComponents.Responses, targetComponents.Responses, comparisonContext)); | ||
|
|
||
| WalkAndCompare( | ||
| comparisonContext, | ||
| OpenApiConstants.Schemas, | ||
| () => comparisonContext | ||
| .GetComparer<IDictionary<string, OpenApiSchema>>() | ||
| .Compare(sourceComponents.Schemas, targetComponents.Schemas, comparisonContext)); | ||
|
|
||
| WalkAndCompare( | ||
| comparisonContext, | ||
| OpenApiConstants.Headers, | ||
| () => comparisonContext | ||
| .GetComparer<IDictionary<string, OpenApiHeader>>() | ||
| .Compare(sourceComponents.Headers, targetComponents.Headers, comparisonContext)); | ||
|
|
||
| // To Do compare Examples | ||
| // To Do compare SecuritySchemes | ||
| // To Do compare Links | ||
| // To Do compare Callbacks | ||
| // To Do compare Extensions | ||
| } | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
src/Microsoft.OpenApi/Services/OpenApiDictionaryComparer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Microsoft.OpenApi.Interfaces; | ||
|
|
||
| namespace Microsoft.OpenApi.Services | ||
| { | ||
| /// <summary> | ||
| /// Defines behavior for comparing <see cref="IDictionary{TKey,TValue}"/> where TKey is <see cref="string"/> | ||
| /// and TValue is <see cref="IOpenApiSerializable"/>. | ||
| /// </summary> | ||
| public class OpenApiDictionaryComparer<T> : OpenApiComparerBase<IDictionary<string, T>> | ||
| where T : IOpenApiSerializable | ||
| { | ||
| /// <summary> | ||
| /// Executes comparision against source and target <see cref="IDictionary{TKey, TValue}"/> | ||
| /// where TKey is <see cref="string"/> and TValue is <see cref="IOpenApiSerializable"/>. | ||
| /// </summary> | ||
| /// <param name="sourceFragment">The source.</param> | ||
| /// <param name="targetFragment">The target.</param> | ||
| /// <param name="comparisonContext">Context under which to compare the source and target.</param> | ||
| public override void Compare( | ||
| IDictionary<string, T> sourceFragment, | ||
| IDictionary<string, T> targetFragment, | ||
| ComparisonContext comparisonContext) | ||
| { | ||
| if (sourceFragment == null && targetFragment == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (sourceFragment == null || targetFragment == null) | ||
| { | ||
| comparisonContext.AddOpenApiDifference( | ||
| new OpenApiDifference | ||
| { | ||
| OpenApiDifferenceOperation = OpenApiDifferenceOperation.Update, | ||
| SourceValue = sourceFragment, | ||
| TargetValue = targetFragment, | ||
| OpenApiComparedElementType = typeof(IDictionary<string, T>), | ||
| Pointer = comparisonContext.PathString | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| var newKeysInTarget = targetFragment.Keys.Except(sourceFragment.Keys).ToList(); | ||
|
|
||
| foreach (var newKeyInTarget in newKeysInTarget) | ||
| { | ||
| WalkAndAddOpenApiDifference( | ||
| comparisonContext, | ||
| newKeyInTarget, | ||
| new OpenApiDifference | ||
| { | ||
| OpenApiDifferenceOperation = OpenApiDifferenceOperation.Add, | ||
| TargetValue = new KeyValuePair<string, T>( | ||
| newKeyInTarget, | ||
| targetFragment[newKeyInTarget]), | ||
| OpenApiComparedElementType = typeof(KeyValuePair<string, T>) | ||
| }); | ||
| } | ||
|
|
||
| foreach (var source in sourceFragment) | ||
| { | ||
| if (targetFragment.Keys.Contains(source.Key)) | ||
| { | ||
| WalkAndCompare(comparisonContext, source.Key, | ||
| () => comparisonContext | ||
| .GetComparer<T>() | ||
| .Compare(source.Value, targetFragment[source.Key], comparisonContext)); | ||
| } | ||
| else | ||
| { | ||
| WalkAndAddOpenApiDifference( | ||
| comparisonContext, | ||
| source.Key, | ||
| new OpenApiDifference | ||
| { | ||
| OpenApiDifferenceOperation = OpenApiDifferenceOperation.Remove, | ||
| SourceValue = source, | ||
| OpenApiComparedElementType = typeof(KeyValuePair<string, T>) | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still adding more tests for comparers #Closed