For a hack day at work, I’ve been trying to integrate our code generator into the online Swagger editor and generator. Of course, I’ve been trying to use the latest version, which appears to be 2.3.0-SNAPSHOT (although I just saw there’s also a 3.0.0 branch).
Unfortunately, it seems like inheritance (which is kind of a big deal for actual software systems) is fundamentally broken in 2.3.0.
Here’s my sample spec in YAML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | swagger: "2.0" info: description: "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters." version: "1.0.0" title: "Swagger Petstore" termsOfService: "http://swagger.io/terms/" contact: email: "apiteam@swagger.io" license: name: "Apache 2.0" url: "http://www.apache.org/licenses/LICENSE-2.0.html" host: "petstore.swagger.io" basePath: "/v2" tags: - name: "pet" description: "Everything about your Pets" externalDocs: description: "Find out more" url: "http://swagger.io" - name: "store" description: "Access to Petstore orders" - name: "user" description: "Operations about user" externalDocs: description: "Find out more about our store" url: "http://swagger.io" schemes: - "http" paths: /pet/: get: tags: - "pet" summary: "Find pet by ID" description: "Returns a single pet" operationId: "getPet" produces: - "application/xml" - "application/json" responses: 200: description: "successful operation" schema: $ref: "#/definitions/Pet" 400: description: "Invalid ID supplied" 404: description: "Pet not found" definitions: Pet: discriminator: petType required: - name - petType # required for inheritance to work properties: name: type: string petType: type: string Cat: allOf: - $ref: '#/definitions/Pet' # Cat has all properties of a Pet - properties: # extra properties only for cats huntingSkill: type: string default: lazy enum: - lazy - aggressive Dog: allOf: - $ref: '#/definitions/Pet' # Dog has all properties of a Pet - properties: # extra properties only for dogs packSize: description: The size of the pack the dog is from type: integer |
Cat and Dog extend Pet. Very simple.
The older swagger editor, which reports a version number of 2.10.4 for the swagger-api/swagger-editor GitHub project, understands this and generates the expected code:
1 2 | public class Cat extends Pet { ... |
The latest editor, unfortunately, does not!
1 | public class Cat { |
You gotta be kidding, right?