Skip to content

Commit e6a278b

Browse files
Copilotlahma
andauthored
SampleJsonDataGenerator: generate GUID for uuid/guid format strings (#1895)
* Initial plan * Add UUID/GUID format support to SampleJsonDataGenerator Co-authored-by: lahma <[email protected]> * Use JsonFormatStrings.Uuid constant instead of string literal "uuid" Co-authored-by: lahma <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: lahma <[email protected]>
1 parent e81f092 commit e6a278b

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/NJsonSchema.Tests/Generation/SampleJsonDataGeneratorTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,38 @@ public async Task PropertyWithDefaultDefiniton()
452452

453453
//exclusiveMaximum
454454

455+
[Theory]
456+
[InlineData("uuid")]
457+
[InlineData("guid")]
458+
public async Task PropertyWithGuidOrUuidFormatGeneratesGuid(string format)
459+
{
460+
// Arrange
461+
var data = $@"{{
462+
""$schema"": ""http://json-schema.org/draft-04/schema#"",
463+
""title"": ""test schema"",
464+
""type"": ""object"",
465+
""required"": [
466+
""identifier""
467+
],
468+
""properties"": {{
469+
""identifier"": {{
470+
""type"": ""string"",
471+
""format"": ""{format}""
472+
}}
473+
}}
474+
}}";
475+
var generator = new SampleJsonDataGenerator();
476+
var schema = await JsonSchema.FromJsonAsync(data);
477+
478+
// Act
479+
var testJson = generator.Generate(schema);
480+
481+
// Assert
482+
var identifierValue = testJson.SelectToken("identifier")?.Value<string>();
483+
Assert.NotNull(identifierValue);
484+
Assert.True(System.Guid.TryParse(identifierValue, out _), $"Expected a GUID but got: {identifierValue}");
485+
}
486+
455487
[Fact]
456488
public async Task PropertyExclusiveMinimumDefiniton()
457489
{

src/NJsonSchema/Generation/SampleJsonDataGenerator.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ private static JToken HandleStringType(JsonSchema schema, JsonSchemaProperty? pr
174174
{
175175
return JToken.FromObject(DateTimeOffset.UtcNow.ToString("o"));
176176
}
177+
#pragma warning disable CS0618 // Type or member is obsolete
178+
else if (schema.Format == JsonFormatStrings.Guid || schema.Format == JsonFormatStrings.Uuid)
179+
#pragma warning restore CS0618 // Type or member is obsolete
180+
{
181+
return JToken.FromObject(Guid.NewGuid().ToString());
182+
}
177183
else if (property != null)
178184
{
179185
return JToken.FromObject(property.Name);

0 commit comments

Comments
 (0)