-
-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow setting x-cSharpExistingType on schemas
to override default resolved type
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/NJsonSchema.CodeGeneration.CSharp.Tests/ResolveExistingTypeTests.cs
This file contains 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,57 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace NJsonSchema.CodeGeneration.CSharp.Tests; | ||
|
||
public class ResolveExistingTypeTests | ||
{ | ||
[Fact] | ||
public async Task String_schema_can_use_existing_type() | ||
{ | ||
var json = @"{ | ||
""type"": ""object"", | ||
""properties"": { | ||
""ip"": { | ||
""type"": ""string"", | ||
""pattern"": ""^\\d{1,3}(\\.\\d{1,3}){3}$"", | ||
""x-cSharpExistingType"": ""System.Net.IPAddress"" | ||
} | ||
} | ||
}"; | ||
|
||
var schema = await JsonSchema.FromJsonAsync(json); | ||
|
||
// Act | ||
var code = new CSharpGenerator(schema).GenerateFile("MyClass"); | ||
|
||
//// Act | ||
Assert.Contains("public System.Net.IPAddress Ip { get; set; }", code); | ||
} | ||
|
||
[Fact] | ||
public async Task String_schema_can_use_existing_type_in_definition() | ||
{ | ||
var json = @"{ | ||
""type"": ""object"", | ||
""properties"": { | ||
""ip"": { ""$ref"": ""#/definitions/ip"" } | ||
}, | ||
""definitions"": { | ||
""ip"": { | ||
""type"": ""string"", | ||
""pattern"": ""^\\d{1,3}(\\.\\d{1,3}){3}$"", | ||
""x-cSharpExistingType"": ""System.Net.IPAddress"" | ||
} | ||
} | ||
}"; | ||
|
||
var schema = await JsonSchema.FromJsonAsync(json); | ||
|
||
// Act | ||
var code = new CSharpGenerator(schema).GenerateFile("MyClass"); | ||
|
||
//// Act | ||
Assert.Contains("public System.Net.IPAddress Ip { get; set; }", code); | ||
} | ||
} |
This file contains 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