Skip to content

Commit

Permalink
Merge pull request #84 from digocesar/optional-when-ommit-default-value
Browse files Browse the repository at this point in the history
Nullable when EmitDefaultValue is False
  • Loading branch information
digocesar authored Jan 27, 2025
2 parents 215087e + 9b8ab01 commit 345d7a8
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ $ npm install --save csharp-models-to-typescript
"locale": "en-US"
},
"numericEnums": false,
"validateEmitDefaultValue": false,
"omitFilePathComment": false,
"omitSemicolon": false,
"stringLiteralTypesInsteadOfEnums": false,
Expand Down
4 changes: 3 additions & 1 deletion converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ const createConverter = config => {
}

const convertProperty = property => {
const optional = property.Type.endsWith('?');
const optional = property.Type.endsWith('?') || (config.validateEmitDefaultValue &&
property.ExtraInfo != null &&
!property.ExtraInfo.EmitDefaultValue);
const identifier = convertIdentifier(optional ? `${property.Identifier.split(' ')[0]}?` : property.Identifier.split(' ')[0]);

const type = parseType(property.Type);
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const converter = createConverter({
camelCaseOptions: config.camelCaseOptions || {},
camelCaseEnums: config.camelCaseEnums || false,
numericEnums: config.numericEnums || false,
validateEmitDefaultValue: config.validateEmitDefaultValue || false,
omitFilePathComment: config.omitFilePathComment || false,
omitSemicolon: config.omitSemicolon || false,
stringLiteralTypesInsteadOfEnums: config.stringLiteralTypesInsteadOfEnums || false
Expand Down
1 change: 1 addition & 0 deletions lib/csharp-models-to-json/ExtraInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public class ExtraInfo
public string ObsoleteMessage { get; set; }
public string Summary { get; set; }
public string Remarks { get; set; }
public bool EmitDefaultValue { get; set; }
}
}
1 change: 1 addition & 0 deletions lib/csharp-models-to-json/ModelCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ private static bool IsAccessible(SyntaxTokenList modifiers) => modifiers.All(mod
ObsoleteMessage = Util.GetObsoleteMessage(property.AttributeLists),
Summary = Util.GetSummaryMessage(property),
Remarks = Util.GetRemarksMessage(property),
EmitDefaultValue = Util.GetEmitDefaultValue(property.AttributeLists),
}
};
}
Expand Down
17 changes: 17 additions & 0 deletions lib/csharp-models-to-json/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,22 @@ public static DocumentationCommentTriviaSyntax GetDocumentationCommentTriviaSynt

return null;
}

internal static bool GetEmitDefaultValue(SyntaxList<AttributeListSyntax> attributeLists)
{
var dataMemberAttribute = attributeLists
.SelectMany(attributeList => attributeList.Attributes)
.FirstOrDefault(attribute => attribute.Name.ToString().Equals("DataMember") || attribute.Name.ToString().Equals("DataMemberAttribute"));

if (dataMemberAttribute?.ArgumentList == null)
return true;

var emitDefaultValueArgument = dataMemberAttribute.ArgumentList.Arguments.FirstOrDefault(x => x.ToString().StartsWith("EmitDefaultValue"));

if (emitDefaultValueArgument == null)
return true;

return !emitDefaultValueArgument.ToString().EndsWith("false");
}
}
}
39 changes: 39 additions & 0 deletions lib/csharp-models-to-json_test/ModelCollector_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,44 @@ public enum A {
Assert.That(model.Values["F"].Value, Is.EqualTo("0x00001a"));
}

[Test]
public void ReturnEmmitDefaultValueInfo()
{
var tree = CSharpSyntaxTree.ParseText(@"
public class A
{
[DataMember(EmitDefaultValue = false)]
public bool Prop1 { get; set; }
[DataMember(EmitDefaultValue = true)]
public bool Prop2 { get; set; }
[DataMember( EmitDefaultValue = false )]
public bool Prop3 { get; set; }
[DataMember]
public bool Prop4 { get; set; }
public bool Prop5 { get; set; }
}"
);

var root = (CompilationUnitSyntax)tree.GetRoot();

var modelCollector = new ModelCollector();
modelCollector.Visit(root);

Assert.That(modelCollector.Models, Is.Not.Null);
Assert.That(modelCollector.Models.Count, Is.EqualTo(1));

var properties = modelCollector.Models.First().Properties;

Assert.That(properties.First(x => x.Identifier == "Prop1").ExtraInfo.EmitDefaultValue, Is.False);
Assert.That(properties.First(x => x.Identifier == "Prop2").ExtraInfo.EmitDefaultValue, Is.True);
Assert.That(properties.First(x => x.Identifier == "Prop3").ExtraInfo.EmitDefaultValue, Is.False);
Assert.That(properties.First(x => x.Identifier == "Prop4").ExtraInfo.EmitDefaultValue, Is.True);
Assert.That(properties.First(x => x.Identifier == "Prop5").ExtraInfo.EmitDefaultValue, Is.True);
}

}
}
1 change: 1 addition & 0 deletions test-files/TestFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class TestClass
[Obsolete("obsolete test prop")]
public string StringProperty { get; set; }

[DataMember(EmitDefaultValue = false)]
public DateTime DateTimeProperty { get; set; }

public bool BooleanProperty { get; set; }
Expand Down
1 change: 1 addition & 0 deletions test-files/test-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"includeComments": true,
"numericEnums": true,
"validateEmitDefaultValue": true,
"omitSemicolon": true,
"omitFilePathComment": true,
"stringLiteralTypesInsteadOfEnums": false,
Expand Down

0 comments on commit 345d7a8

Please sign in to comment.