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

Compiler errors in generated client when property name matches namespace of Json library #4578

Open
PolarbearDK opened this issue Nov 10, 2023 · 6 comments

Comments

@PolarbearDK
Copy link

You get compile errors when you consume a service that has a property called Newtonsoft.
It will generate the following code:

[Newtonsoft.Json.JsonProperty("newtonsoft", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public Supported Newtonsoft { get; set; }

Compiler error:
CS0120 An object reference is required for the non-static field, method, or property 'BreakOpenApiModel.Newtonsoft'

This should be an easy fix: Just prefix with global:: to fix it:

[Newtonsoft.Json.JsonProperty("newtonsoft", Required = global::Newtonsoft.Json.Required.DisallowNull, NullValueHandling = global::Newtonsoft.Json.NullValueHandling.Ignore)]
public Supported Newtonsoft { get; set; }

If you switch to System.Text.Json then a property named System will cause similar problems.

Sample application that demonstrates the problem: github.com/PolarbearDK/OpenApiTest

I have previously mentioned this in #2243

@lahma
Copy link
Collaborator

lahma commented Nov 10, 2023

Maybe you could submit a PR to fix the problem?

@PolarbearDK
Copy link
Author

Yeah! Great idea.
I tried, but I can't seem to find the responsible code.
I think it is somewhere in NJsonSchema.CodeGeneration.CSharp.Models.PropertyModel but I'm not sure.

@lahma
Copy link
Collaborator

lahma commented Nov 11, 2023

@aunikitin
Copy link

aunikitin commented Dec 27, 2023

Hello, faced with it on 13.20.0
image
image

And I failed to find source for this. Link upper doesn't lead to master branch. On the master, there's no mention of this attributes. For now I switched to Newtonsoft serialization

@aunikitin
Copy link

@RicoSuter hello, can you give a hint, how to fix this problem?

@franklixuefei
Copy link

Hi,
I'm hitting this too after exposeJsonSerializerSettings is turned on:
image

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

4 participants