Skip to content

Commit

Permalink
Add support for records with properties
Browse files Browse the repository at this point in the history
  • Loading branch information
svenheden committed Apr 13, 2021
1 parent 59f04b7 commit 714e256
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const createConverter = config => {
model.BaseClasses = model.BaseClasses.filter(type => !type.match(dictionaryRegex));
}

const members = [...model.Fields, ...model.Properties];
const members = [...(model.Fields || []), ...(model.Properties || [])];
const baseClasses = model.BaseClasses && model.BaseClasses.length ? ` extends ${model.BaseClasses.join(', ')}` : '';

rows.push(`// ${filename}`);
Expand Down
7 changes: 5 additions & 2 deletions lib/csharp-models-to-json/ModelCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,18 @@ public override void VisitRecordDeclaration(RecordDeclarationSyntax node)
var model = new Model()
{
ModelName = $"{node.Identifier.ToString()}{node.TypeParameterList?.ToString()}",
Fields = node.ParameterList.Parameters
Fields = node.ParameterList?.Parameters
.Where(field => IsAccessible(field.Modifiers))
.Where(property => !IsIgnored(property.AttributeLists))
.Select((field) => new Field
{
Identifier = field.Identifier.ToString(),
Type = field.Type.ToString(),
}),
Properties = new List<Property>(),
Properties = node.Members.OfType<PropertyDeclarationSyntax>()
.Where(property => IsAccessible(property.Modifiers))
.Where(property => !IsIgnored(property.AttributeLists))
.Select(ConvertProperty),
BaseClasses = new List<string>(),
};

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "csharp-models-to-typescript",
"version": "0.21.0",
"version": "0.21.1",
"title": "C# models to TypeScript",
"author": "Jonathan Svenheden <[email protected]>",
"license": "MIT",
Expand Down

0 comments on commit 714e256

Please sign in to comment.