Skip to content

Commit

Permalink
Fix TypeScript enum default generation in StringLiteral mode (#1619)
Browse files Browse the repository at this point in the history
* Fix TypeScript enum default generation in StringLiteral mode

* remove raw string literal syntax
  • Loading branch information
nycdotnet authored Sep 26, 2023
1 parent cbc0f24 commit 69933dc
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,36 @@ public void When_schema_has_default_value_of_enum_it_is_generated_in_CSharp_and_
Assert.Equal("Ns.MyEnum.bar", csharpValue);
Assert.Equal("MyEnum.bar", typescriptValue);
}


/// <summary>
/// This test asserts the fix for issue #1618
/// </summary>
[Fact]
public void When_schema_has_a_default_value_for_an_enum_and_uses_enumstyle_stringliteral_it_defaults_to_the_stringliteral()
{
//// Arrange
var schema = new JsonSchema()
{
Type = JsonObjectType.String,
Enumeration =
{
"Foo",
"Bar"
},
Default = "Bar"
};

var typescriptSettings = new TypeScriptGeneratorSettings { EnumStyle = TypeScriptEnumStyle.StringLiteral };
var typescriptGenerator = new TypeScriptValueGenerator(typescriptSettings);
var typescriptTypeResolver = new TypeScriptTypeResolver(typescriptSettings);

//// Act
var typescriptValue = typescriptGenerator.GetDefaultValue(schema, true, "MyEnum", "MyEnum", true, typescriptTypeResolver);

//// Assert
Assert.Equal("\"Bar\"", typescriptValue);
}

[Fact]
public void When_schema_has_required_abstract_class_it_generates_no_default_value_for_in_CSharp_and_TypeScript()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,47 @@ public async Task When_enum_has_description_then_typescript_has_comment()
Assert.Contains(@"/** EnumDesc. *", output);
}

/// <summary>
/// This test asserts the fix for issue #1618
/// </summary>
[Fact]
public async Task When_enum_has_default_and_using_enumstyle_stringliteral_it_defaults_to_stringliteral()
{
//// Arrange
var jsonSchema = @"
{
""$schema"": ""http://json-schema.org/draft-04/schema#"",
""openapi"": ""3.1.0"",
""title"": ""TShirt"",
""type"": ""object"",
""properties"": {
""color"": {
""type"": ""string"",
""default"": ""green"",
""enum"": [""red"", ""green"", ""blue"", ""black""]
}
}
}";

var schema = await JsonSchema.FromJsonAsync(jsonSchema);
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
EnumStyle = TypeScriptEnumStyle.StringLiteral,
TypeScriptVersion = 5m,
});

//// Act
var code = generator.GenerateFile("MyFile");

//// Assert
Assert.Contains("export type MyFileColor = \"red\" | \"green\" | \"blue\" | \"black\";", code);
Assert.Contains("this.color = _data[\"color\"] !== undefined ? _data[\"color\"] : \"green\";", code);
Assert.Contains("this.color = \"green\";", code);

// This is the old code gen that used the enum prior to the fix for #1618
Assert.DoesNotContain("Color.Green", code);
}

[Fact]
public async Task When_class_has_description_then_typescript_has_comment()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ public TypeScriptValueGenerator(TypeScriptGeneratorSettings settings)
{
}

/// <summary>Gets the enum default value.</summary>
/// <param name="schema">The schema.</param>
/// <param name="actualSchema">The actual schema.</param>
/// <param name="typeNameHint">The type name hint.</param>
/// <param name="typeResolver">The type resolver.</param>
/// <returns>The enum default value.</returns>
protected override string GetEnumDefaultValue(JsonSchema schema, JsonSchema actualSchema, string typeNameHint, TypeResolverBase typeResolver)
{
if (schema?.Default is not null &&
typeResolver is TypeScriptTypeResolver { Settings.EnumStyle: TypeScriptEnumStyle.StringLiteral })
{
return GetDefaultAsStringLiteral(schema);
}

return base.GetEnumDefaultValue(schema, actualSchema, typeNameHint, typeResolver);
}

/// <summary>Gets the default value code.</summary>
/// <param name="schema">The schema.</param>
/// <param name="allowsNull">Specifies whether the default value assignment also allows null.</param>
Expand Down

0 comments on commit 69933dc

Please sign in to comment.