-
-
Notifications
You must be signed in to change notification settings - Fork 150
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
'Required' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.RequiredAttribute' and 'Json.Schema.Generation.RequiredAttribute'? #702
Comments
You'll need to specify which one you mean using a Either that or you can include the namespace with the attribute usage: [System.ComponentModel.DataAnnotations.Required] |
I am familiar with aliases, I just failed to create one that worked, I also tried to be explicit with the json.schema.generation.jsonschemabuilder, but then the fromtype did not resolve. I do not want to change all my existing It does seem like a general problem when the schema package uses the same attribute as used in the standard dataannotations package though, anything you could do to not require all users to disambiguate, or document a solution that does not require me to change all my code? |
I don't know what went wrong on your end, but
Luckily, a solution for that is already documented, it's the |
I think I misunderstood, I do want my existing I am looking for an alias to apply only to to var schemaBuilder = new Json.Schema.JsonSchemaBuilder();
var schema = schemaBuilder.Json.Schema.Generation.FromType<Parent2>() /*this does not work for extension methods*/
.Title("PlexCleaner Configuration Schema")
.Id(new Uri(SchemaUri))
.Build(); But, |
That's not possible, because that's not how attribute annotations work in .NET. This is not even related to |
If I were to summarize, it is not possible to |
About a week ago, I released JsonSchema.Net.Generation.DataAnnotations, which provides support for the DataAnnotations namespace. Regarding wanting |
Docs?
Than you for joining the conversation, I did cover this in my replies, I don't control all the code I use, and I don't want to be forced to disambiguate from a standard library when I include your code.
|
As stated in the follow-up tweet, I'm working on them.
The base generation library doesn't expect DataAnnotations to be present. Naming conflicts happen all of the time, even within the standard libraries.
I'm sorry that you don't agree with my decision, but a very reasonable workaround exists.
That method is very strongly typed: you're passing the type in as the generic parameter. I'm not sure what you mean here. |
Ok, I feel we are getting of on the wrong foot, thank you for your work, it is just a bit difficult to add your library to code that kinda worked with Json.Net. Asking me to look at announcement for details, and when I ask for docs say not ready as I said, is not really helping, I mean what did you want me to do? I don't know what the very reasonable workaround is if I cannot change code I use that is using In a previous comment I gave an actual code example of how I'd like to use but can't use |
This suite of libraries uses System.Text.Json, which is a completely different system from Newtonsoft.Json. I expect you understand this. However because it's a different system, you should expect that there will be some transitioning and that not everything will work the same. Surely you've seen this just between the two JSON serializers. The transition from Newtonsoft's schema generation to my schema generation should also carry the same expectation that things are going to work differently.
Why can't you change the code? If you're having an ambiguity problem, then you have the code because it's being compiled. Is the code that carries the
You don't have to call it as an extension method. Extension methods are just static methods, and you can call them that way. If you don't want to bring the namespace into scope, you can add the namespace to the extension class. var schema = Json.Schema.Generation.JsonSchemaBuilderExtensions.FromType<MyClass>(builder).Build(); All of the builder methods are extension methods, actually. |
Calling as static will be a solution as I can avoid needing to use This requires var schemaBuilder = new JsonSchemaBuilder();
var schema = schemaBuilder.FromType<Parent2>()
.Title("PlexCleaner Configuration Schema")
.Id(new Uri(SchemaUri))
.Build();
var jsonSchema = JsonSerializer.Serialize(schema); This should not require const string SchemaUri = "https://raw.githubusercontent.com/ptr727/PlexCleaner/main/PlexCleaner.schema.json";
var schemaBuilder = new Json.Schema.JsonSchemaBuilder();
var schema = Json.Schema.Generation.JsonSchemaBuilderExtensions.FromType<Parent2>(schemaBuilder)
.Title("PlexCleaner Configuration Schema") // Does not resolve?
.Id(new Uri(SchemaUri))
.Build();
var jsonSchema = JsonSerializer.Serialize(schema); My C# foo is not strong enough, advice on how to make this work with full namespaces specified? |
You should still be able to include the normal builder extensions: using Json.Schema;
var builder = new JsonSchemaBuilder()
.Title("PlexCleaner Configuration Schema")
.Id(new Uri(SchemaUri));
var schema = Json.Schema.Generation.JsonSchemaBuilderExtensions.FromType<Parent2>(builder)
.Build(); Only |
Thank you, that works great. If I want to be pedantic I can also use |
It's the same way. The extension class is Out of curiousity, why are you so opposed to adding |
Oops, sorry, figured that out right after I commented. It is not that I am opposed to not using In my open source projects yeah I can just go and change all my |
Okay, so here's my explanation: The attributes in System.ComponentModel.DataAnnotations have no effect on System.Text.Json's serialization. The two namespaces are completely disjoint. DataAnnotations is used to annotate ASP.Net DTOs and provide validation constraints. I actually go over some of this in a blog post. Essentially, the model is deserialized (ignoring the DataAnnotations attributes), then the model is validated against the attributes in a separate operation. When I was designing schema generation, my focus was System.Text.Json. Since the data annotations and serialization do not interact, there was no reason to consider them. I only output my own DataAnnotations extension package recently due to clients requesting it, and then reluctantly so. Regarding I have no intention of renaming my attribute. It's aptly named, and in the rare event conflicts arise, the language has the mechanisms needed to resolve the ambiguity. |
Understood, for reference, some projects I work on do use the same code in DTO's in DB's and JSON serialization, thus the potential conflict, anyway I now have ways of not using using at all if need be. Side questions, could not find in docs, what is the correct |
Theoretically, I should be including it. It's nominally 2020-12, but pretty much everything should be compatible with all of them. |
Not in my testing? |
I don't understand your question. I mean that I should be including a |
In my testing Closing as the advice I got here let me resolve the namespace issue. |
Documentation
Nuget Package
JsonSchema.Net.Generation
Package Version
4.3.0
How can I help? Please provide as much context as possible.
Hi, I am migrating from newtonsoft to text.json, with some issues I am still trying to resolve along the way.
I am also looking for a more "native" text.json schema from code builder, and found jsonschema.net.generation as a replacement for newtonsoft.json.scheme.
(I am really looking for a configuration to build schemas for all nor
[Obsolete]
items (properties, attributes, fields), but with newtonsoft I need to declare my gettersinternal
to not have a current schema with old config.)New code and old code:
Problem is I use
[Obsolete]
and[Required]
extensively, and now get this error:E.g.
As the
FromType
seems like an extension, I could not find a way to be more explicit with the namespaces.Any ideas how to resolve the ambiguity?
Code of Conduct
The text was updated successfully, but these errors were encountered: