-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better support for complex inheritance (#229)
* Better support for complex inheritance The current implementation had several issues: 1. If there are classes `A` -> `B` -> `C`, `C` will be `sealed`, `A` and `B` will be `data`. This is incorrect, `B` should be `open`. 2. All properties in `B` and `C` should be open: descendants can override them 3. If class has no own properties, there can be two cases: 3.1. It's a leaf (like `A`), then it should be `data object`, not just object 3.2. It's a node (like `B`), then it should be `open class` This commit addresses all these cases. There is still one important issue I don't immediately know how to fix: all descendants should have an `override` modifier on fields, overriding fields in the whole tree. * additional test * no need for non-nullability for child classes * add missing approval files --------- Co-authored-by: Ivan Ponomarev <[email protected]>
- Loading branch information
1 parent
7342a04
commit 101eb4a
Showing
7 changed files
with
159 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/test/java/ru/curs/hurdygurdy/CodegenTest.deepInheritance.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
|
||
--- | ||
/com | ||
--- | ||
/com/example | ||
--- | ||
/com/example/dto | ||
--- | ||
/com/example/dto/A.java | ||
package com.example.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
import lombok.Data; | ||
|
||
@Data | ||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
@JsonTypeInfo( | ||
use = JsonTypeInfo.Id.NAME, | ||
include = JsonTypeInfo.As.PROPERTY, | ||
property = "type" | ||
) | ||
@JsonSubTypes({ | ||
@JsonSubTypes.Type(value = B.class, name = "b"), | ||
@JsonSubTypes.Type(value = C.class, name = "c")}) | ||
public class A { | ||
} | ||
--- | ||
/com/example/dto/B.java | ||
package com.example.dto; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
import lombok.Data; | ||
|
||
@Data | ||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
public class B extends A { | ||
} | ||
--- | ||
/com/example/dto/C.java | ||
package com.example.dto; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
import lombok.Data; | ||
|
||
@Data | ||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
public class C extends B { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/test/java/ru/curs/hurdygurdy/KCodegenTest.deepInheritance.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
|
||
--- | ||
/com | ||
--- | ||
/com/example | ||
--- | ||
/com/example/dto | ||
--- | ||
/com/example/dto/A.kt | ||
package com.example.dto | ||
|
||
import com.fasterxml.jackson.`annotation`.JsonSubTypes | ||
import com.fasterxml.jackson.`annotation`.JsonTypeInfo | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies | ||
import com.fasterxml.jackson.databind.`annotation`.JsonNaming | ||
|
||
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy::class) | ||
@JsonTypeInfo( | ||
use = JsonTypeInfo.Id.NAME, | ||
include = JsonTypeInfo.As.PROPERTY, | ||
property = "type", | ||
) | ||
@JsonSubTypes( | ||
JsonSubTypes.Type(value = B::class, name = "b"), | ||
JsonSubTypes.Type(value = C::class, name = "c"), | ||
) | ||
public sealed class A() | ||
--- | ||
/com/example/dto/B.kt | ||
package com.example.dto | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies | ||
import com.fasterxml.jackson.databind.`annotation`.JsonNaming | ||
|
||
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy::class) | ||
public open class B : A() | ||
--- | ||
/com/example/dto/C.kt | ||
package com.example.dto | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies | ||
import com.fasterxml.jackson.databind.`annotation`.JsonNaming | ||
|
||
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy::class) | ||
public data object C : B() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
openapi: 3.0.1 | ||
info: | ||
title: "Deep inheritance API" | ||
description: | | ||
Test | ||
version: "0.1" | ||
|
||
paths: | ||
|
||
|
||
components: | ||
schemas: | ||
A: | ||
description: A | ||
properties: | ||
type: | ||
type: string | ||
discriminator: | ||
propertyName: type | ||
mapping: | ||
'b': '#/components/schemas/B' | ||
'c': '#/components/schemas/C' | ||
B: | ||
description: B | ||
allOf: | ||
- $ref: "#/components/schemas/A" | ||
|
||
C: | ||
description: C | ||
allOf: | ||
- $ref: "#/components/schemas/B" |