Skip to content

[Java] Subclass with attribute allOf issue #2096

Closed
@hiveship

Description

@hiveship

i'm facing a strange behaviour with Swagger Codegen while generating Java code (JAXRS / JAXRS-CXF and Java).

Considering the Swagger definitions:

  event:
    properties:
      mediumUUID:
        type: string
        format: uuid

  messageEvent:
    allOf:  
      - $ref: '#/definitions/event'
      - type: object
        required:
          - messageId
          - message
        properties:
          messageId:
            type: integer
            format: int32
          message:
            type: string

I exepect to get a class "Event":

public class Event   {
  private UUID mediumUUID = null;

and a subclass "MessageEvent" like this:

public class MessageEvent extends Event  {
  // get access to "mediumUUID" from super.getMediumUUID()
  private Integer messageId = null;
  private String message = null;

But with the current Swagger codegen, I got:

public class Event   {

  private UUID mediumUUID = null;
public class InputEvent extends Event  {

  private String name = null;
  private String value = null;

  private UUID mediumUUID = null; // PROBLEM !

This behaviour can cause issue. MessageEvent is a subclass of Event, so in my code I can do:

MessageEvent messageEvent = new MessageEvent();
messageEvent.setMediumUUID(foo);
// messageEvent.getMediumUUID() -> "foo"
Event event = messageEvent;
//event.getMediumUUID() -> null ! Because not calling the right getter/setter

And then I will got 'null' !

I can help to solve this issue but I need some help to point me where I need to do changes.

What we want is "don't put the parent attributes in a sub class".

In the .mustache template file I can found:

public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}}  {

So with that, we can know if the class 'classname' has a parent or not. We now need something to get the list of all attributes of the parent. And then, don't generate them.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions