-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
How to get rid of all abstract classes, generated in the Models section? #2998
Comments
You need to be a bit more precise. Your localhost help won't help (we can't access your localhost...). If many classes gets included, that usually means that you set a parameter or a return type to a type that was not meant to be serialized. NSwag isn't supposed to generate 50 classes for simple cases like yours. If those advices didn't help, please share a minimal code that reproduces your issue. |
Well, I've updated my question above, and right after that, I've got your response - thanks for the answer! I understand that you can't access my localhost link - it just a sample of URL where I found Models section :) Here is a piece of code I tried to use in the Startup.cs:
but here I can't figure out how to filter all that Models that are in the Models list on the Swagger page... The goal of my question is to get rid of all abstract classes and classes that are not parameters/results of WebAPI methods... |
Are these the base classes of your models ? In your APIs, you should use DTO objects (objects that are made specifically for the json serialization/deserialization), and not your internal objects, otherwise NSwag will try to serialize the complete object hierarchy. |
I don't think that this is a good idea to show (at least) abstract classes... It would be good to have some method to control Models like you control Operations: |
You could edit the generated items, but I think there's value in generating every abstract class in the hierarchy. Say you have the Apple and Orange classes, based on the Fruit abstract class. You have two solutions for your specific use case. Either you use a DocumentProcessor, or you clean up your API by only using serializable DTO objects without a base class, and copy the values in your internal classes manually. |
You can flatten all inheritance chains with FlattenInheritanceHierarchy config: or you add the [JsonSchemaFlatten] attribute to the classes which should not have base classes: |
Hello!
ASP.NET Core 3.1, WebAPI.
I have just about 8 methods (like Login, Register... Ping.. and so on), but services.AddSwaggerDocument(..) generates about 50+ classes in the swagger document (https://localhost:44398/swagger/index.html), below in the Models section. These classes came from the whole solution and also there are a lot of abstract classes over here. All of these classes are just public classes. Also, I do not use attributes (like [DataMember] or [Json...]) to control serialization...
Question is - how to control, what class should be there in that section? I planned to have just method parameters there... I tried to use services.AddSwaggerDocument(opt => {../* here */..}) construction in the Startup.cs but had no luck...
Thanks!
The text was updated successfully, but these errors were encountered: