Skip to content

Commit

Permalink
Improved ConstructorInterfaceType
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Oct 12, 2017
1 parent 87f7d0b commit 4de7683
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
Expand Up @@ -74,5 +74,40 @@ public async Task When_constructor_interface_and_conversion_code_is_generated_th
bar: { [key: string] : Skill[]; };
}".Replace("\r", "").Replace("\n", "")));
}

[TestMethod]
public async Task When_array_of_string_dictionary_is_used_with_ConvertConstructorInterfaceData_then_it_should_be_ignored()
{
//// Arrange
var json = @"
{
""type"": ""object"",
""properties"": {
""custom4"": {
""type"": ""array"",
""items"": {
""type"": ""object"",
""additionalProperties"": {
""type"": ""string""
}
}
}
}
}";
var schema = await JsonSchema4.FromJsonAsync(json);

//// Act
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
GenerateConstructorInterface = true,
ConvertConstructorInterfaceData = true

});

var output = generator.GenerateFile("MyClass");

//// Assert
Assert.IsTrue(output.Contains("custom4: { [key: string] : string; }[];"));
}
}
}
Expand Up @@ -50,7 +50,7 @@ public PropertyModel(ClassTemplateModel classTemplateModel, JsonProperty propert
public override string Type => _resolver.Resolve(_property.ActualPropertySchema, _property.IsNullable(_settings.SchemaType), GetTypeNameHint());

/// <summary>Gets the type of the property in the initializer interface.</summary>
public string ConstructorInterfaceType => _settings.ConvertConstructorInterfaceData && SupportsConstructorConversion ?
public string ConstructorInterfaceType => _settings.ConvertConstructorInterfaceData ?
_resolver.ResolveConstructorInterfaceName(_property.ActualPropertySchema, _property.IsNullable(_settings.SchemaType), GetTypeNameHint()) :
Type;

Expand All @@ -59,10 +59,7 @@ public bool SupportsConstructorConversion
{
get
{
var interfaceType = _resolver.ResolveConstructorInterfaceName(_property.ActualPropertySchema,
_property.IsNullable(_settings.SchemaType), GetTypeNameHint());

if (Type == interfaceType)
if (Type == ConstructorInterfaceType)
return false;

if (IsArray)
Expand Down
Expand Up @@ -158,7 +158,9 @@ private string ResolveArrayOrTuple(JsonSchema4 schema, string typeNameHint, bool
{
if (schema.Item != null)
{
var prefix = addInterfacePrefix && schema.Item?.ActualSchema.Type.HasFlag(JsonObjectType.Object) == true ? "I" : "";
var isObject = schema.Item?.ActualSchema.Type.HasFlag(JsonObjectType.Object) == true;
var isDictionary = schema.Item?.ActualSchema.IsDictionary == true;
var prefix = addInterfacePrefix && isObject && !isDictionary ? "I" : "";
return string.Format("{0}[]", prefix + Resolve(schema.Item, true, typeNameHint)); // TODO: Make typeNameHint singular if possible
}

Expand Down

0 comments on commit 4de7683

Please sign in to comment.