Skip to content

Commit

Permalink
Generate nullable string types for optional properties with date or t…
Browse files Browse the repository at this point in the history
…ime format in nullable context (#1406)
  • Loading branch information
aw129 authored Sep 26, 2023
1 parent c6f9f28 commit e12442b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,101 @@ public async Task When_generating_from_json_schema_property_is_optional_and_Gene
Assert.Contains("public object? Property { get; set; } = default!;", code);
Assert.Contains("public object Property2 { get; set; } = new object();", code);
}

[Theory]
[InlineData("date")]
[InlineData("date-time")]
[InlineData("time")]
[InlineData("time-span")]
public async Task When_generating_from_json_schema_string_property_with_date_or_time_format_is_optional_and_GenerateNullableOptionalProperties_is_not_set_then_CSharp_property(string format)
{
//// Arrange
var schemaJson = @"
{
""type"": ""object"",
""required"": [
""required""
],
""properties"": {
""required"": {
""type"": ""string"",
""format"": """ + format + @"""
},
""optional"": {
""type"": ""string"",
""format"": """ + format + @"""
}
}
}
";

var schema = await JsonSchema.FromJsonAsync(schemaJson);

//// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
SchemaType = SchemaType.OpenApi3,
DateType = "string",
DateTimeType = "string",
TimeType = "string",
TimeSpanType = "string",
GenerateNullableReferenceTypes = false,
GenerateOptionalPropertiesAsNullable = true
});
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains("public string Required { get; set; }", code);
Assert.Contains("public string Optional { get; set; }", code);
}

[Theory]
[InlineData("date")]
[InlineData("date-time")]
[InlineData("time")]
[InlineData("time-span")]
public async Task When_generating_from_json_schema_string_property_with_date_or_time_format_is_optional_and_GenerateNullableOptionalProperties_is_set_then_CSharp_property(string format)
{
//// Arrange
var schemaJson = @"
{
""type"": ""object"",
""required"": [
""required""
],
""properties"": {
""required"": {
""type"": ""string"",
""format"": """ + format + @"""
},
""optional"": {
""type"": ""string"",
""format"": """ + format + @"""
}
}
}
";

var schema = await JsonSchema.FromJsonAsync(schemaJson);

//// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
SchemaType = SchemaType.OpenApi3,
DateType = "string",
DateTimeType = "string",
TimeType = "string",
TimeSpanType = "string",
GenerateNullableReferenceTypes = true,
GenerateOptionalPropertiesAsNullable = true
});
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains("public string Required { get; set; }= default!;", code);
Assert.Contains("public string? Optional { get; set; }= default!;", code);
}
}
}
12 changes: 6 additions & 6 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,28 +160,28 @@ protected override bool IsDefinitionTypeSchema(JsonSchema schema)

private string ResolveString(JsonSchema schema, bool isNullable, string typeNameHint)
{
var nullableReferenceType = Settings.GenerateNullableReferenceTypes && isNullable ? "?" : string.Empty;

if (schema.Format == JsonFormatStrings.Date)
{
return isNullable && Settings.DateType?.ToLowerInvariant() != "string" ? Settings.DateType + "?" : Settings.DateType;
return isNullable && Settings.DateType?.ToLowerInvariant() != "string" ? Settings.DateType + "?" : Settings.DateType + nullableReferenceType;
}

if (schema.Format == JsonFormatStrings.DateTime)
{
return isNullable && Settings.DateTimeType?.ToLowerInvariant() != "string" ? Settings.DateTimeType + "?" : Settings.DateTimeType;
return isNullable && Settings.DateTimeType?.ToLowerInvariant() != "string" ? Settings.DateTimeType + "?" : Settings.DateTimeType + nullableReferenceType;
}

if (schema.Format == JsonFormatStrings.Time)
{
return isNullable && Settings.TimeType?.ToLowerInvariant() != "string" ? Settings.TimeType + "?" : Settings.TimeType;
return isNullable && Settings.TimeType?.ToLowerInvariant() != "string" ? Settings.TimeType + "?" : Settings.TimeType + nullableReferenceType;
}

if (schema.Format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return isNullable && Settings.TimeSpanType?.ToLowerInvariant() != "string" ? Settings.TimeSpanType + "?" : Settings.TimeSpanType;
return isNullable && Settings.TimeSpanType?.ToLowerInvariant() != "string" ? Settings.TimeSpanType + "?" : Settings.TimeSpanType + nullableReferenceType;
}

var nullableReferenceType = Settings.GenerateNullableReferenceTypes && isNullable ? "?" : string.Empty;

if (schema.Format == JsonFormatStrings.Uri)
{
return "System.Uri" + nullableReferenceType;
Expand Down

0 comments on commit e12442b

Please sign in to comment.