Skip to content

Don't generate properties marked with json ignore attribute #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/WebApiTestApplication/Controllers/TestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class AnotherClass
public int Number { get; set; }
public string Name { get; set; }
public string[] List { get; set; }
[JsonIgnore]
public string JsonIgnoredProperty { get; set; }
}

public class DerivedClassWithShadowedProperty : AnotherClass
Expand Down
7 changes: 7 additions & 0 deletions src/WebApiToTypeScript/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,12 @@ public static bool HasCustomAttribute(ParameterDefinition parameter, string attr
return parameter.HasCustomAttributes
&& parameter.CustomAttributes.Any(a => a.AttributeType.Name == attributeName);
}

public static bool HasCustomAttribute(PropertyDefinition property, string attributeName)
{
return property.HasCustomAttributes
&& property.CustomAttributes.Any(a => a.AttributeType.Name == attributeName);
}

}
}
2 changes: 1 addition & 1 deletion src/WebApiToTypeScript/Interfaces/InterfaceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ private List<MemberWithCSharpType> GetMembers(TypeDefinition typeDefinition)
});

var properties = typeDefinition.Properties
.Where(p => p.GetMethod != null && p.GetMethod.IsPublic && !p.IsSpecialName)
.Where(p => p.GetMethod != null && p.GetMethod.IsPublic && !p.IsSpecialName && !Helpers.HasCustomAttribute(p, "JsonIgnoreAttribute"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to have a config, something like:
"IgnorePropertiesWithAttributes" and then specify attributes - those properties with those attributes will be ignored?
Might be better than having a magic string

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea, but I am fine with this being here for now, since it is the most occuring case, and we can figure out a scheme for enhanced attribute support/handling as a separate issue?

.Select(p => new MemberWithCSharpType
{
Name = p.Name,
Expand Down