Swagger codegen inheritance

By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

In Swagger UI and Editor these objects show up as I wish them to: Child inherits the ab and c fields from Parent and has a few additional ones.

However, while the Parent class looked as expected, my Child class consisted of the following:. Which lacks even extends. When using a discriminator it works, but I don't really want Polymorphism - just plain inheritance. How can I accomplish this with Swagger Codegen? Learn more.

Asked 3 years, 6 months ago. Active 2 years, 8 months ago. Viewed 2k times. Relevant pom. Which version are you using? I was using 2. Not only that, but maven also gives me an error when I try to enter 2.

I just tried manually installing the 2. Generating the stubs with that, however, yielded the exact same result as above. I'll update my post with my pom. I checked with the versions you mentioned and with the one that's currently the master branch and I got your results. For some reason the one I cloned from master 3 months ago works almost fine, here is the output.

But there is an issue on github that might be related to your problem. Active Oldest Votes.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

In my "simplified" API, all responses are derived inherit from a base "response" class. The response class is composed of a header filled with metadata, and the body which contains the core data the the user is requesting. The response in JSON is laid out such that all the metadata is on the first "layer" and the body is a single attribute called "body" as such. However, I am certain that either this is the wrong way to do things, or that my implementation is incorrect.

I have been unable to find an example of inheritance in the swagger 2. I am pretty certain that this "discriminator" has a large part to play, but not sure what I need to do.

It would also be great if I could specify an ErrorResponse class that inherits from response where the "result" attribute in the header is always set to "error". Both error and successful responses use the "response" object. As a beginner in swagger I don't find the official documentation about polimorphism and composition easy to undestand, because it lacks an example.

When I searched the net, there are lots of good examples refering to swagger 1. For swagger 2. Note, that allOf is enough for this to work and no discriminator field is used. This is good, because it works and this is important, as I think, tools will be able to generate code without discriminator field. All the answers here are excellent already, but I just want to add a minor note about composition versus inheritance.

The Swagger 2. It would be if you could reference the base definition of Pet as an input parameter, then pick Cat or enter a Cat JSON object as the value for the input request, and have this acceptable to Swagger UI. The best I could get working was to set additionalProperties to true on the base object e. Since the additional properties are allowed Swagger UI generated a valid input request payload.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

swagger codegen inheritance

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community. Already on GitHub? Sign in to your account. In our swagger. This works when using the Swagger Editor. When using swagger-codegen-maven-plugin with no configOptions set, we get the following:.

swagger codegen inheritance

Cat implements all of Pet 's properties itself, instead of extending it. Although it's currently not necessary online, or for other languages, like TypeScript. Adds support for polymorphism.

Jesse Collis - Automatically Generating your API Client with Swagger and Swagger Codegen

The discriminator is the schema property name that is used to differentiate between other schema that inherit this schema. When used, the value MUST be the name of this schema or any schema that inherits it. And that's precisely not how it's used in the workaround: Neither is type a property, nor is it required. But maybe I'm just reading that bit al wrong If I don't have a discriminator property, how do I set that up? I was relying on the old generation that used extends and honestly prefer that behavior over the duplication of fields.

I didn't want nor can, to be honest alter the model attributes just for having that. Skip to content. Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up. New issue. Jump to bottom. Copy link Quote reply. Description In our swagger. This comment has been minimized.

Sign in to view. In other words, I'm a bit confused as to why this actually does work at allI will start by saying I'm not sure how to even search for this, I tried but couldn't find a post like my problem. Currently I'm having some troubles with how to hanlde inheritance in swagger 3. The api this swagger is for uses alot of the same fields accross its different endpoints. To reduce the workload around updates to these fields I pulled all of the fields out into their own section in the schema.

This has been fine for the documentation side but codegen is now generating models for each seperate field and getting really confused because of it.

I have a couple of questions I'm wanting to ask here. Is this the correct way to handle repeating fields? Assuming the answer to my first question isn't I'm doing everything wrong how can I reformat my swagger file to codegen only the actual models. This ends up generating swagger filed with models for each field like userId, deviceId, and so on. What I really want is the update and postRepsonse models. Switch To: SmartBear. License Portal.

Sign In Register. SmartBear Support Resources. Community WFH. Turn on suggestions. Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type. Showing results for. Search instead for. Did you mean:. Codegen and inheritance. Start a topic. Frequent Visitor. Assuming the answer to my first question isn't I'm doing everything wrong how can I reformat my swagger file to codegen only the actual models openapi: 3.

Labels: Swagger Codegen. All forum topics Previous Topic Next Topic. New Here? Join us and watch the welcome video:. Read the April News.In this example, the obj1 value is mapped to the Object1 model that is defined in the same spec, obj2 — to Object2and the value system matches the sysObject model that is located in an external file.

All these objects must have the objectType property with the value "obj1""obj2" or "system"respectively. Did not find what you were looking for? Ask the community Found a mistake? Let us know. Sign up here: SwaggerHub Swagger Inspector. Have an account? Sign in here: SwaggerHub Swagger Inspector. Instead of describing these properties for each schema repeatedly, you can describe the schemas as a composition of the common property set and schema-specific properties.

Note: When validating the data, servers and clients will validate the combined model against each model it consists of. It is recommended to avoid using conflicting properties like properties that have the same names, but different data types.

swagger codegen inheritance

Polymorphism In your API, you can have request and responses that can be described by several alternative schemas. In OpenAPI 3.

swagger codegen inheritance

The discriminator is used with anyOf or oneOf keywords only. It is important that all the models mentioned below anyOf or oneOf contain the property that the discriminator specifies. This means, for example, that in our code above, both simpleObject and complexObject must have the objectType property.

This property is required in these schemas: schemas: simpleObject: type: object required: - objectType properties: objectType: type: string … complexObject: type: object required: - objectType properties: objectType: type: string … The discriminator keyword can be used by various API consumers. One possible example are code generation tools: they can use discriminator to generate program statements that typecast request data to appropriate object type based on the discriminator property value.

Mapping Type Names It is implied, that the property to which discriminator refers, contains the name of the target schema.

In the example above, the objectType property should contain either simpleObjector complexObject string. If the property values do not match the schema names, you can map the values to the names.

SwaggerHub Swagger Inspector.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account. Skip to content. Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up.

Generating java model with inheritance from swagger specification

New issue. Jump to bottom. Milestone v2. Copy link Quote reply. Description swagger-codegen 2. This works in 2. Swagger-codegen version 2. Generate Java client, look at Cat. No "extends Pet". This comment has been minimized. Sign in to view. Verified that with the latest master it DOES work. I am also seeing this when trying with 3. Sign up for free to join this conversation on GitHub. Already have an account?

Sign in to comment.

Linked pull requests. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window.Monday, June 11, Coding is all about not repeating yourself and standing on the shoulders of giants.

Subscribe to RSS

These two properties can be really seen working for you when you build an ASP. With a toolset like NSwagit's incredibly easy to simple write your backend code and have an auto-generated documentation, along with a nice web frontend to make first steps with the API and even generated schemas for all your models.

However, my data model had one catch: Inheritance. As soon as you leave the golden path, you're about to encounter problems. For me, I noticed that the generated swagger. I've followed the documentation for setting up inheritance and got good-looking results at first, but noticed that the generated clients did not include any properties for derived classes.

Generating Correct Schema with Inheritance in ASP.NET Core with NSwag

That was not optimal, so I dug a bit further. However, after reading the official Swagger documentation about inheritanceor the allOf property, you'll see this schema given:. Which, besides being in YAML, has one distinct difference: The Dog class does not define a properties list itself, but keeps both a reference to the base class as well as all its own properties in its allOf list.

So what we need to do is somehow configure the NSwag generator on our server to follow this behavior. The fix is actually quite easy, but took me some time to figure out. You can supply your own ISchemaProcessor on the server side when generating swagger. The InheritanceSchemaProcessor just checks if the current schema has entries in its allOf property. If it has, we simply add its own properties there, too. Your schema processor will be called for every individual model schema in your API.

Hi, my name's George! I love coding and blogging about it. I focus on all things around. Net, Web Development and DevOps. GD Dangl. Blog. Update, November 29, The behavior is now fixed with the latest version of NSwagso this workaround is no longer necessary.

The NSwag docs work with this example: Which should lead to a Swagger definition like the following: However, after reading the official Swagger documentation about inheritanceor the allOf property, you'll see this schema given: Which, besides being in YAML, has one distinct difference: The Dog class does not define a properties list itself, but keeps both a reference to the base class as well as all its own properties in its allOf list.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *