-
Notifications
You must be signed in to change notification settings - Fork 280
Added more comparers #328
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
Added more comparers #328
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
| // Licensed under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Microsoft.OpenApi.Models; | ||
|
|
||
| namespace Microsoft.OpenApi.Services | ||
|
|
@@ -46,6 +48,32 @@ internal void Compare(string source, string target, ComparisonContext comparison | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Compares two Uri object. | ||
| /// </summary> | ||
| /// <param name="source">The source.</param> | ||
| /// <param name="target">The target.</param> | ||
| /// <param name="comparisonContext">The context under which to compare the objects.</param> | ||
| internal void Compare(Uri source, Uri target, ComparisonContext comparisonContext) | ||
| { | ||
| if (source == null && target == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (source != target) | ||
| { | ||
| comparisonContext.AddOpenApiDifference(new OpenApiDifference | ||
| { | ||
| OpenApiDifferenceOperation = OpenApiDifferenceOperation.Update, | ||
| OpenApiComparedElementType = typeof(Uri), | ||
| SourceValue = source, | ||
| TargetValue = target, | ||
| Pointer = comparisonContext.PathString | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Compares two boolean object. | ||
| /// </summary> | ||
|
|
@@ -138,6 +166,67 @@ internal void Compare<TEnum>(Enum source, Enum target, ComparisonContext compari | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Compares <see cref="IDictionary{TKey,TValue}"/> where TKey is <see cref="string"/> and TValue is | ||
| /// <see cref="string"/>. | ||
| /// </summary> | ||
| /// <param name="source">The source.</param> | ||
| /// <param name="target">The target.</param> | ||
| /// <param name="comparisonContext">The context under which to compare the objects.</param> | ||
| internal void Compare(IDictionary<string, string> source, IDictionary<string, string> target, | ||
| ComparisonContext comparisonContext) | ||
| { | ||
| if (source == null && target == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (source == null || target == null) | ||
| { | ||
| comparisonContext.AddOpenApiDifference( | ||
| new OpenApiDifference | ||
| { | ||
| OpenApiDifferenceOperation = OpenApiDifferenceOperation.Update, | ||
| SourceValue = source, | ||
| TargetValue = target, | ||
| OpenApiComparedElementType = typeof(IDictionary<string, T>), | ||
| Pointer = comparisonContext.PathString | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| var newKeysInTarget = target.Keys.Except(source.Keys).ToList(); | ||
|
|
||
| foreach (var newKeyInTarget in newKeysInTarget) | ||
| { | ||
| WalkAndAddOpenApiDifference( | ||
| comparisonContext, | ||
| newKeyInTarget, | ||
| new OpenApiDifference | ||
| { | ||
| OpenApiDifferenceOperation = OpenApiDifferenceOperation.Add, | ||
| TargetValue = target[newKeyInTarget], | ||
| OpenApiComparedElementType = typeof(string) | ||
| }); | ||
| } | ||
|
|
||
| var removedKeysFromSource = source.Keys.Except(target.Keys).ToList(); | ||
|
|
||
| foreach (var removedKeyFromSource in removedKeysFromSource) | ||
| { | ||
| WalkAndAddOpenApiDifference( | ||
| comparisonContext, | ||
| removedKeyFromSource, | ||
| new OpenApiDifference | ||
| { | ||
| OpenApiDifferenceOperation = OpenApiDifferenceOperation.Remove, | ||
| SourceValue = source[removedKeyFromSource], | ||
| OpenApiComparedElementType = typeof(string) | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a segment to the context path to enable pointing to the current location in the document. | ||
| /// </summary> | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. | ||
|
|
||
| using Microsoft.OpenApi.Models; | ||
|
|
||
| namespace Microsoft.OpenApi.Services | ||
| { | ||
| /// <summary> | ||
| /// Defines behavior for comparing properties of <see cref="OpenApiContact"/>. | ||
| /// </summary> | ||
| public class OpenApiContactComparer : OpenApiComparerBase<OpenApiContact> | ||
| { | ||
| /// <summary> | ||
| /// Executes comparision against source and target <see cref="OpenApiContact"/>. | ||
| /// </summary> | ||
| /// <param name="sourceContact">The source.</param> | ||
| /// <param name="targetContact">The target.</param> | ||
| /// <param name="comparisonContext">Context under which to compare the source and target.</param> | ||
| public override void Compare( | ||
| OpenApiContact sourceContact, | ||
| OpenApiContact targetContact, | ||
| ComparisonContext comparisonContext) | ||
| { | ||
| if (sourceContact == null && targetContact == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (sourceContact == null || targetContact == null) | ||
| { | ||
| comparisonContext.AddOpenApiDifference( | ||
| new OpenApiDifference | ||
| { | ||
| OpenApiDifferenceOperation = OpenApiDifferenceOperation.Update, | ||
| SourceValue = sourceContact, | ||
| TargetValue = targetContact, | ||
| OpenApiComparedElementType = typeof(OpenApiContact), | ||
| Pointer = comparisonContext.PathString | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| WalkAndCompare(comparisonContext, OpenApiConstants.Name, | ||
| () => Compare(sourceContact.Name, targetContact.Name, comparisonContext)); | ||
|
|
||
| WalkAndCompare(comparisonContext, OpenApiConstants.Email, | ||
| () => Compare(sourceContact.Email, targetContact.Email, comparisonContext)); | ||
|
|
||
| WalkAndCompare(comparisonContext, OpenApiConstants.Url, | ||
| () => Compare(sourceContact.Url, targetContact.Url, comparisonContext)); | ||
| } | ||
| } | ||
| } |
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
48 changes: 48 additions & 0 deletions
48
src/Microsoft.OpenApi/Services/OpenApiExternalDocsComparer.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,48 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. | ||
|
|
||
| using Microsoft.OpenApi.Models; | ||
|
|
||
| namespace Microsoft.OpenApi.Services | ||
| { | ||
| /// <summary> | ||
| /// Defines behavior for comparing properties of <see cref="OpenApiExternalDocs"/>. | ||
| /// </summary> | ||
| public class OpenApiExternalDocsComparer : OpenApiComparerBase<OpenApiExternalDocs> | ||
| { | ||
| /// <summary> | ||
| /// Executes comparision against source and target <see cref="OpenApiExternalDocs"/>. | ||
| /// </summary> | ||
| /// <param name="sourceDocs">The source.</param> | ||
| /// <param name="targetDocs">The target.</param> | ||
| /// <param name="comparisonContext">Context under which to compare the source and target.</param> | ||
| public override void Compare(OpenApiExternalDocs sourceDocs, OpenApiExternalDocs targetDocs, | ||
| ComparisonContext comparisonContext) | ||
| { | ||
| if (sourceDocs == null && targetDocs == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (sourceDocs == null || targetDocs == null) | ||
| { | ||
| comparisonContext.AddOpenApiDifference( | ||
| new OpenApiDifference | ||
| { | ||
| OpenApiDifferenceOperation = OpenApiDifferenceOperation.Update, | ||
| SourceValue = sourceDocs, | ||
| TargetValue = targetDocs, | ||
| OpenApiComparedElementType = typeof(OpenApiExternalDocs), | ||
| Pointer = comparisonContext.PathString | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| WalkAndCompare(comparisonContext, OpenApiConstants.Description, | ||
| () => Compare(sourceDocs.Description, targetDocs.Description, comparisonContext)); | ||
|
|
||
| WalkAndCompare(comparisonContext, OpenApiConstants.Url, | ||
| () => Compare(sourceDocs.Url, targetDocs.Url, comparisonContext)); | ||
| } | ||
| } | ||
| } |
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.
Based on this http://jsonpatch.com/, this should be
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.
I do not see a dictionary example for json patch , so i am not sure if this is correct, can you point me to a dictionary example that you might have found?
In reply to: 223511856 [](ancestors = 223511856)
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.
C# Dictionary should be treated as a JSON object.
The first example on http://jsonpatch.com/
See the second line:
{ "op": "add", "path": "/hello", "value": ["world"] }
and the third line:
{ "op": "remove", "path": "/foo" }
In reply to: 223857178 [](ancestors = 223857178,223511856)