Repository navigation
Closed
Description
Description
When a type is constructed using allOf, and also includes a discriminator property (so it can serve as the base class for inherited types) the inherited types do not extend from it.
If instead, the "allOf" construct is collapsed and all the properties inlined, the inheritance works as expected.
In the example below, the generated code for LevelTwoTypeOne is missing the "extends LevelOne", while the generated code for LevelTwoTypeTwo includes "extends InlinedLevelOne" as expected.
Swagger-codegen version
2.2.0
Swagger declaration file content or url
---
swagger: '2.0'
info:
version: 0.0.0
title: Simple API
paths:
/:
get:
responses:
200:
description: OK
definitions:
Base:
type: object
properties:
name:
type: string
LevelOne:
allOf:
- $ref: '#/definitions/Base'
- type: object
discriminator: resource_type
properties:
color:
type: string
resource_type:
type: string
required: [resource_type]
InlinedLevelOne:
type: object
discriminator: resource_type
properties:
name:
type: string
color:
type: string
resource_type:
type: string
required: [resource_type]
LevelTwoTypeOne:
type: object
allOf:
- $ref: '#/definitions/LevelOne'
- type: object
properties:
odor:
type: string
LevelTwoTypeTwo:
type: object
allOf:
- $ref: '#/definitions/InlinedLevelOne'
- type: object
properties:
height:
type: string
```
##### Command line used for generation
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/inheritance.yaml -l java --language-specific-primitives integer,Long -o samples/client/nsx/java/default -DhideGenerationTimestamp=true"
java $JAVA_OPTS -jar $executable $ags
##### Steps to reproduce
After running swagger-codegen, I observe:
LevelTwoTypeOne.java's class declaration is:
public class LevelTwoTypeOne {
I expected it to inherit from LevelOne.
LevelTwoTypeTwo's class declaration is:
public class LevelTwoTypeTwo extends InlinedLevelOne {
as expected.
##### Related issues
Sounds a bit similar to https://github.com/swagger-api/swagger-codegen/issues/3474. However, I hand-applied the related fix (https://github.com/medallia/swagger-codegen/commit/d460a024ef5b54c94b7c10a28ea98679c532a75e) and it didn't change the behavior.
##### Suggest a Fix
Something about using allOf to describe a base class seems to be the problem.
Activity
asafbennatan commentedon Aug 13, 2016
in an older version that worked (version 2.0) , but subclasses had the fields of supertype as well as their own,
my guess is that that bug fix went wrong,
P.S
that happens in android client as well
wing328 commentedon Aug 15, 2016
@cbornet when you've time, I wonder if you can take a look.
cbornet commentedon Aug 23, 2016
It looks like the same as #3474 : we don't look in for the discriminator in allOf recursively. I'm working on something.
fix some inheritance/composition issues and add allOf unit tests
wing328 commentedon Oct 3, 2016
@ggood PR merged into master. Please pull the latest master to give it a try.
ggood commentedon Nov 30, 2016
Sorry for not responding sooner - this addresses the issue for me. Thanks for the fix.
karux commentedon Jan 23, 2017
does the project have a test exercising this use case?
wing328 commentedon Jan 24, 2017
@karux currently, we've the following tests related to inheritance in the test spec: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml#L968-L996
Please feel free to suggest more test cases covering different issues.