-
-
Notifications
You must be signed in to change notification settings - Fork 64
Add option to remove [JsonConverter(typeof(JsonStringEnumConverter))] from generated contracts #786
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
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8fa6f2a
Introduce --no-inline-json-converters option
christianhelle c2a1f00
Implement --no-inline-json-converters to remove [JsonConverter(typeof…
christianhelle 203bcf4
Add tests to verify InlineJsonConverters setting
christianhelle 0d2b093
Update README to include --no-inline-json-converters option
christianhelle cb2abe1
Update CLI-tool.md
christianhelle a18d10e
Update refitter-file-format.md
christianhelle ac7beb5
Update NuGet package README
christianhelle 2478123
Update source generator NuGet package README
christianhelle 2dec7f4
Refactor: Extract JsonConverter attribute removal logic into `Sanitiz…
christianhelle 528db4d
Simplify JsonConverter attribute removal using Regex
christianhelle 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
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
155 changes: 155 additions & 0 deletions
155
src/Refitter.Tests/Examples/InlineJsonConvertersTests.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,155 @@ | ||
| using FluentAssertions; | ||
| using FluentAssertions.Execution; | ||
| using Refitter.Core; | ||
| using Refitter.Tests.Build; | ||
| using Refitter.Tests.TestUtilities; | ||
| using Xunit; | ||
|
|
||
| namespace Refitter.Tests.Examples; | ||
|
|
||
| public class InlineJsonConvertersTests | ||
| { | ||
| private const string OpenApiSpec = @" | ||
| { | ||
| ""swagger"": ""2.0"", | ||
| ""info"": { | ||
| ""title"": ""Enum Test API"", | ||
| ""version"": ""1.0.0"" | ||
| }, | ||
| ""host"": ""example.com"", | ||
| ""basePath"": ""/"", | ||
| ""schemes"": [ | ||
| ""https"" | ||
| ], | ||
| ""paths"": { | ||
| ""/api/pets/{petId}"": { | ||
| ""get"": { | ||
| ""summary"": ""Get pet by ID"", | ||
| ""operationId"": ""getPetById"", | ||
| ""parameters"": [ | ||
| { | ||
| ""name"": ""petId"", | ||
| ""in"": ""path"", | ||
| ""required"": true, | ||
| ""type"": ""integer"" | ||
| } | ||
| ], | ||
| ""responses"": { | ||
| ""200"": { | ||
| ""description"": ""Success"", | ||
| ""schema"": { | ||
| ""$ref"": ""#/definitions/Pet"" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| ""definitions"": { | ||
| ""Pet"": { | ||
| ""type"": ""object"", | ||
| ""properties"": { | ||
| ""id"": { | ||
| ""type"": ""integer"", | ||
| ""format"": ""int64"" | ||
| }, | ||
| ""name"": { | ||
| ""type"": ""string"" | ||
| }, | ||
| ""status"": { | ||
| ""type"": ""string"", | ||
| ""enum"": [ | ||
| ""available"", | ||
| ""pending"", | ||
| ""sold"" | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| "; | ||
|
|
||
| [Fact] | ||
| public async Task Can_Generate_Code() | ||
| { | ||
| string generatedCode = await GenerateCode(); | ||
| generatedCode.Should().NotBeNullOrWhiteSpace(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Can_Build_Generated_Code() | ||
| { | ||
| string generatedCode = await GenerateCode(); | ||
| BuildHelper | ||
| .BuildCSharp(generatedCode) | ||
| .Should() | ||
| .BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Generated_Code_Contains_JsonConverter_By_Default() | ||
| { | ||
| string generatedCode = await GenerateCode(inlineJsonConverters: true); | ||
|
|
||
| using (new AssertionScope()) | ||
| { | ||
| generatedCode.Should().Contain("[JsonConverter(typeof(JsonStringEnumConverter))]"); | ||
| generatedCode.Should().Contain("Status { get; set; }"); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Generated_Code_Does_Not_Contain_JsonConverter_When_Disabled() | ||
| { | ||
| string generatedCode = await GenerateCode(inlineJsonConverters: false); | ||
|
|
||
| using (new AssertionScope()) | ||
| { | ||
| generatedCode.Should().NotContain("[JsonConverter(typeof(JsonStringEnumConverter))]"); | ||
| generatedCode.Should().Contain("Status { get; set; }"); | ||
| generatedCode.Should().Contain("public enum PetStatus"); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Generated_Code_Without_JsonConverter_Can_Build() | ||
| { | ||
| string generatedCode = await GenerateCode(inlineJsonConverters: false); | ||
| BuildHelper | ||
| .BuildCSharp(generatedCode) | ||
| .Should() | ||
| .BeTrue(); | ||
| } | ||
|
|
||
| private static async Task<string> GenerateCode(bool inlineJsonConverters = true) | ||
| { | ||
| string swaggerFile = await SwaggerFileHelper.CreateSwaggerFile(OpenApiSpec); | ||
| try | ||
| { | ||
| var settings = new RefitGeneratorSettings | ||
| { | ||
| OpenApiPath = swaggerFile, | ||
| CodeGeneratorSettings = new CodeGeneratorSettings | ||
| { | ||
| InlineJsonConverters = inlineJsonConverters | ||
| } | ||
| }; | ||
| var generator = await RefitGenerator.CreateAsync(settings); | ||
| return generator.Generate(); | ||
| } | ||
| finally | ||
| { | ||
| if (File.Exists(swaggerFile)) | ||
| { | ||
| File.Delete(swaggerFile); | ||
| } | ||
|
|
||
| var directory = Path.GetDirectoryName(swaggerFile); | ||
| if (directory != null && Directory.Exists(directory)) | ||
| { | ||
| Directory.Delete(directory, true); | ||
| } | ||
| } | ||
| } | ||
| } |
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
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.