Skip to content
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

... cannot have more than one member that has the attribute 'System.Text.Json.Serialization.JsonExtensionDataAttribute'. #1447

Closed
jochenjonc opened this issue Nov 23, 2021 · 1 comment

Comments

@jochenjonc
Copy link
Contributor

I'm trying to use NSwag to generate a CSharp client (System.Text.Json) based on an OpenAPI spec from HERE. And with one of their APIs I have the following exception with the generated NSwag code.

https://developer.here.com/documentation/matrix-routing-api/8.5.0/swagger/spec.yaml

System.InvalidOperationException
The type 'Here.Routing.CalculateMatrix.Truck2' cannot have more than one member that has the attribute 'System.Text.Json.Serialization.JsonExtensionDataAttribute'.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializationDuplicateTypeAttribute(Type classType, Type attribute)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.CacheMember(Type declaringType, Type memberType, MemberInfo memberInfo, Boolean isVirtual, Nullable`1 typeNumberHandling, Boolean& propertyOrderSpecified, Dictionary`2& ignoredMembers)

The problem is that Truck2 inherits from Truck and both have the following code being generated.

private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();

[System.Text.Json.Serialization.JsonExtensionData]
public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
{
	get { return _additionalProperties; }
	set { _additionalProperties = value; }
}

The base class and the derived class both have a property AdditionalProperties with the JsonExtensionData attribute and it seems that System.Text.Json (v6.0.0) doesn't allow that.

Is there a way to prevent AdditionalProperties being added to derived classes?
And what is the reason, the element in the yaml/json that renders this?

I want to help with a PR, but I'm a bit lost in the code ;-).

I found the place where the AdditionalProperties are generated.

{%- if HasAdditionalPropertiesType -%}
private System.Collections.Generic.IDictionary<string, {{ AdditionalPropertiesType }}> _additionalProperties = new System.Collections.Generic.Dictionary<string, {{ AdditionalPropertiesType }}>();
{%- if UseSystemTextJson -%}
[System.Text.Json.Serialization.JsonExtensionData]
{%- else -%}
[Newtonsoft.Json.JsonExtensionData]
{%- endif -%}
public System.Collections.Generic.IDictionary<string, {{ AdditionalPropertiesType }}> AdditionalProperties
{
get { return _additionalProperties; }
{{PropertySetterAccessModifier}}set { _additionalProperties = value; }
}
{%- endif -%}

And the code that determines the HasAdditionalPropertiesType.

public bool HasAdditionalPropertiesType =>
!_schema.IsDictionary &&
!_schema.ActualTypeSchema.IsDictionary &&
!_schema.IsArray &&
!_schema.ActualTypeSchema.IsArray &&
(_schema.ActualTypeSchema.AllowAdditionalProperties ||
_schema.ActualTypeSchema.AdditionalPropertiesSchema != null);

But I'm uncertain what to add to prevent the AdditionalProperties being added to derived classes and it that is the right solution.

@RicoSuter
Copy link
Owner

But I'm uncertain what to add to prevent the AdditionalProperties being added to derived classes and it that is the right solution.

I'd say that is the right approach...
So in the ClassTemplateModel we need to check the base classes whether they already have this...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants