phoenixnap / springmvc-raml-plugin

Spring MVC - RAML Spec Synchroniser Plugin. A Maven plugin designed to Generate Server & Client code in Spring from a RAML API descriptor and conversely, a RAML API document from the SpringMVC Server implementation.
Apache License 2.0
136 stars 84 forks source link

Add support for union types with inheritance #298

Closed lctncld closed 5 years ago

lctncld commented 5 years ago

Currently union type interpreter generates code correctly only if union types don't extend other user-defined types.

For example let's look at the schema used for a unit test

types:
  Phone:
    type: object
    properties:
      manufacturer:
        type: string
      numberOfSIMCards:
        type: number
  Notebook:
    type: object
    properties:
      manufacturer:
        type: string
      numberOfUSBPorts:
        type: number
  Device:
    type: Phone | Notebook

If I change Phone and Notebook types to extend another custom type

types:
  Manufactured:
    type: object
    properties:
     manufacturer:
        type: string
  Phone:
    type: Manufactured
    properties:
      numberOfSIMCards:
        type: number
  Notebook:
    type: Manufactured
    properties:
      numberOfUSBPorts:
        type: number
  Device:
    type: Phone | Notebook

Interpreter ignores Phone/Notebook types and Device POJO looks like this:

public class Device implements Serializable {
    protected Manufactured Phone;
    protected Manufactured Notebook;
}

This PR contains a fix for this behavior, I believe expected result is similar to

public class Device implements Serializable {
  protected com.gen.test.model.Phone Phone;
  protected com.gen.test.model.Notebook Notebook;
}

Could you please review it and maybe propose a better solution?

stojsavljevic commented 5 years ago

Hi, thanks for reaching us.

  Device:
    type: Phone | Notebook

represents union type. This means Device is either Phone or Notebook. So it should be either public class Device extends Phone or public class Device extends Notebook but I don't see the way to accomplish both with Java. Regarding your solution - I don't feel attributes are expected here. POJO's attributes are always generated based on type's properties.

If you think we should discuss the topic more, please open an issue so the conversation can continue there.

Thanks.

lctncld commented 5 years ago

Thanks, I think we can continue in https://github.com/phoenixnap/springmvc-raml-plugin/issues/289 since you've already asked the question there.