From 9cba1fde98ea881f5090f8c5f2ed8e729a62a33d Mon Sep 17 00:00:00 2001 From: Christoph Kappestein Date: Wed, 9 Oct 2024 11:58:10 +0200 Subject: [PATCH] update generic test case --- src/Generator/Python.php | 3 +++ tests/Generator/resource/csharp/csharp_oop.cs | 9 ++++++--- tests/Generator/resource/go/go_oop.go | 5 ++++- tests/Generator/resource/html/html_oop.htm | 5 +++-- tests/Generator/resource/java/java_oop.java | 17 ++++++++++++++--- .../resource/jsonschema/jsonschema_import.json | 1 + .../resource/jsonschema/jsonschema_oop.json | 1 + tests/Generator/resource/kotlin/kotlin_oop.kt | 7 ++++--- tests/Generator/resource/php/php_oop.php | 15 +++++++++++++-- .../resource/protobuf/protobuf_oop.txt | 9 ++++++--- tests/Generator/resource/python/python_oop.py | 9 ++++++--- tests/Generator/resource/ruby/ruby_oop.rb | 5 +++-- tests/Generator/resource/rust/rust_oop.rs | 10 ++++++++++ tests/Generator/resource/source_oop.json | 6 ++++++ tests/Generator/resource/swift/swift_oop.swift | 2 ++ .../resource/typeschema/typeschema_import.json | 5 +++++ .../resource/typeschema/typeschema_oop.json | 5 +++++ .../resource/typescript/typescript_oop.ts | 8 +++++--- .../resource/visualbasic/visualbasic_oop.vb | 9 ++++++--- 19 files changed, 103 insertions(+), 28 deletions(-) diff --git a/src/Generator/Python.php b/src/Generator/Python.php index d0837523..08c360bb 100644 --- a/src/Generator/Python.php +++ b/src/Generator/Python.php @@ -70,6 +70,9 @@ protected function writeStruct(Code\Name $name, array $properties, ?string $exte $parts = []; if (!empty($extends)) { + if (!empty($templates)) { + $extends.= $this->generator->getGenericType($templates); + } $parts[] = $extends; } else { $parts[] = 'BaseModel'; diff --git a/tests/Generator/resource/csharp/csharp_oop.cs b/tests/Generator/resource/csharp/csharp_oop.cs index 1c4a6440..c7fbd500 100644 --- a/tests/Generator/resource/csharp/csharp_oop.cs +++ b/tests/Generator/resource/csharp/csharp_oop.cs @@ -18,23 +18,26 @@ public class Student : Human } using System.Text.Json.Serialization; -public class Map +public class Map { [JsonPropertyName("totalResults")] public int? TotalResults { get; set; } + [JsonPropertyName("parent")] + public P? Parent { get; set; } + [JsonPropertyName("entries")] public System.Collections.Generic.List? Entries { get; set; } } using System.Text.Json.Serialization; -public class StudentMap : Map +public class StudentMap : Map { } using System.Text.Json.Serialization; -public class HumanMap : Map +public class HumanMap : Map { } diff --git a/tests/Generator/resource/go/go_oop.go b/tests/Generator/resource/go/go_oop.go index 7fc804b2..442db156 100644 --- a/tests/Generator/resource/go/go_oop.go +++ b/tests/Generator/resource/go/go_oop.go @@ -11,16 +11,19 @@ type Student struct { type StudentMap struct { TotalResults int `json:"totalResults"` + Parent *Human `json:"parent"` Entries []Student `json:"entries"` } type HumanMap struct { TotalResults int `json:"totalResults"` + Parent *Human `json:"parent"` Entries []Human `json:"entries"` } -type Map[T any] struct { +type Map[P any, T any] struct { TotalResults int `json:"totalResults"` + Parent P `json:"parent"` Entries []T `json:"entries"` } diff --git a/tests/Generator/resource/html/html_oop.htm b/tests/Generator/resource/html/html_oop.htm index 60308091..f060bab1 100644 --- a/tests/Generator/resource/html/html_oop.htm +++ b/tests/Generator/resource/html/html_oop.htm @@ -7,10 +7,11 @@ "matricleNumber": String, }
FieldDescription
matricleNumberString
-

Map<T>

{
+

Map<P, T>

{
   "totalResults": Integer,
+  "parent": P,
   "entries": Array (T),
-}
FieldDescription
totalResultsInteger
entriesArray (T)
+}
FieldDescription
totalResultsInteger
parentP
entriesArray (T)

StudentMap extends Map

{
 }
FieldDescription
diff --git a/tests/Generator/resource/java/java_oop.java b/tests/Generator/resource/java/java_oop.java index 479b30da..36c2c573 100644 --- a/tests/Generator/resource/java/java_oop.java +++ b/tests/Generator/resource/java/java_oop.java @@ -43,8 +43,9 @@ public String getMatricleNumber() { import com.fasterxml.jackson.annotation.JsonGetter; import com.fasterxml.jackson.annotation.JsonSetter; -public class Map { +public class Map { private Integer totalResults; + private P parent; private java.util.List entries; @JsonSetter("totalResults") @@ -57,6 +58,16 @@ public Integer getTotalResults() { return this.totalResults; } + @JsonSetter("parent") + public void setParent(P parent) { + this.parent = parent; + } + + @JsonGetter("parent") + public P getParent() { + return this.parent; + } + @JsonSetter("entries") public void setEntries(java.util.List entries) { this.entries = entries; @@ -70,12 +81,12 @@ public java.util.List getEntries() { import com.fasterxml.jackson.annotation.JsonGetter; import com.fasterxml.jackson.annotation.JsonSetter; -public class StudentMap extends Map { +public class StudentMap extends Map { } import com.fasterxml.jackson.annotation.JsonGetter; import com.fasterxml.jackson.annotation.JsonSetter; -public class HumanMap extends Map { +public class HumanMap extends Map { } import com.fasterxml.jackson.annotation.JsonGetter; diff --git a/tests/Generator/resource/jsonschema/jsonschema_import.json b/tests/Generator/resource/jsonschema/jsonschema_import.json index 85c2b84f..7feaa01a 100644 --- a/tests/Generator/resource/jsonschema/jsonschema_import.json +++ b/tests/Generator/resource/jsonschema/jsonschema_import.json @@ -27,6 +27,7 @@ "totalResults": { "type": "integer" }, + "parent": [], "entries": { "type": "array", "items": [] diff --git a/tests/Generator/resource/jsonschema/jsonschema_oop.json b/tests/Generator/resource/jsonschema/jsonschema_oop.json index efe6c2f1..35728faf 100644 --- a/tests/Generator/resource/jsonschema/jsonschema_oop.json +++ b/tests/Generator/resource/jsonschema/jsonschema_oop.json @@ -27,6 +27,7 @@ "totalResults": { "type": "integer" }, + "parent": [], "entries": { "type": "array", "items": [] diff --git a/tests/Generator/resource/kotlin/kotlin_oop.kt b/tests/Generator/resource/kotlin/kotlin_oop.kt index 5f267592..f63ae4ef 100644 --- a/tests/Generator/resource/kotlin/kotlin_oop.kt +++ b/tests/Generator/resource/kotlin/kotlin_oop.kt @@ -7,15 +7,16 @@ open class Student : Human { var matricleNumber: String? = null } -open class Map { +open class Map { var totalResults: Int? = null + var parent: P? = null var entries: Array? = null } -open class StudentMap : Map { +open class StudentMap : Map { } -open class HumanMap : Map { +open class HumanMap : Map { } open class RootSchema { diff --git a/tests/Generator/resource/php/php_oop.php b/tests/Generator/resource/php/php_oop.php index 67471f99..c2d10e6f 100644 --- a/tests/Generator/resource/php/php_oop.php +++ b/tests/Generator/resource/php/php_oop.php @@ -57,11 +57,13 @@ public function jsonSerialize() : object } /** + * @template P * @template T */ class Map implements \JsonSerializable, \PSX\Record\RecordableInterface { protected ?int $totalResults = null; + protected ?P $parent = null; /** * @var array|null */ @@ -74,6 +76,14 @@ public function getTotalResults() : ?int { return $this->totalResults; } + public function setParent(?P $parent) : void + { + $this->parent = $parent; + } + public function getParent() : ?P + { + return $this->parent; + } /** * @param array|null $entries */ @@ -93,6 +103,7 @@ public function toRecord() : \PSX\Record\RecordInterface /** @var \PSX\Record\Record $record */ $record = new \PSX\Record\Record(); $record->put('totalResults', $this->totalResults); + $record->put('parent', $this->parent); $record->put('entries', $this->entries); return $record; } @@ -103,14 +114,14 @@ public function jsonSerialize() : object } /** - * @extends Map + * @extends Map */ class StudentMap extends Map implements \JsonSerializable, \PSX\Record\RecordableInterface { } /** - * @extends Map + * @extends Map */ class HumanMap extends Map implements \JsonSerializable, \PSX\Record\RecordableInterface { diff --git a/tests/Generator/resource/protobuf/protobuf_oop.txt b/tests/Generator/resource/protobuf/protobuf_oop.txt index b926d40d..77c81c0e 100644 --- a/tests/Generator/resource/protobuf/protobuf_oop.txt +++ b/tests/Generator/resource/protobuf/protobuf_oop.txt @@ -11,17 +11,20 @@ message Student { message StudentMap { optional int64 totalResults = 1 [json_name="totalResults"]; - optional repeated Student entries = 2 [json_name="entries"]; + optional Human parent = 2 [json_name="parent"]; + optional repeated Student entries = 3 [json_name="entries"]; } message HumanMap { optional int64 totalResults = 1 [json_name="totalResults"]; - optional repeated Human entries = 2 [json_name="entries"]; + optional Human parent = 2 [json_name="parent"]; + optional repeated Human entries = 3 [json_name="entries"]; } message Map { optional int64 totalResults = 1 [json_name="totalResults"]; - optional repeated T entries = 2 [json_name="entries"]; + optional P parent = 2 [json_name="parent"]; + optional repeated T entries = 3 [json_name="entries"]; } message RootSchema { diff --git a/tests/Generator/resource/python/python_oop.py b/tests/Generator/resource/python/python_oop.py index 4da8560b..88e3775b 100644 --- a/tests/Generator/resource/python/python_oop.py +++ b/tests/Generator/resource/python/python_oop.py @@ -18,9 +18,11 @@ class Student(Human): from pydantic import BaseModel, Field, GetCoreSchemaHandler from pydantic_core import CoreSchema, core_schema from typing import Any, Dict, Generic, List, Optional, TypeVar, UserList, UserDict +P = TypeVar("P") T = TypeVar("T") -class Map(BaseModel, Generic[T]): +class Map(BaseModel, Generic[P], Generic[T]): total_results: Optional[int] = Field(default=None, alias="totalResults") + parent: Optional[P] = Field(default=None, alias="parent") entries: Optional[List[T]] = Field(default=None, alias="entries") pass @@ -28,8 +30,9 @@ class Map(BaseModel, Generic[T]): from pydantic_core import CoreSchema, core_schema from typing import Any, Dict, Generic, List, Optional, TypeVar, UserList, UserDict from .map import Map +from .human import Human from .student import Student -class StudentMap(Map): +class StudentMap(Map[Human, Student]): pass from pydantic import BaseModel, Field, GetCoreSchemaHandler @@ -37,7 +40,7 @@ class StudentMap(Map): from typing import Any, Dict, Generic, List, Optional, TypeVar, UserList, UserDict from .map import Map from .human import Human -class HumanMap(Map): +class HumanMap(Map[Human, Human]): pass from pydantic import BaseModel, Field, GetCoreSchemaHandler diff --git a/tests/Generator/resource/ruby/ruby_oop.rb b/tests/Generator/resource/ruby/ruby_oop.rb index 7930996f..6b358407 100644 --- a/tests/Generator/resource/ruby/ruby_oop.rb +++ b/tests/Generator/resource/ruby/ruby_oop.rb @@ -17,10 +17,11 @@ def initialize(matricle_number) end class Map - attr_accessor :total_results, :entries + attr_accessor :total_results, :parent, :entries - def initialize(total_results, entries) + def initialize(total_results, parent, entries) @total_results = total_results + @parent = parent @entries = entries end end diff --git a/tests/Generator/resource/rust/rust_oop.rs b/tests/Generator/resource/rust/rust_oop.rs index e507a01a..f6c56c00 100644 --- a/tests/Generator/resource/rust/rust_oop.rs +++ b/tests/Generator/resource/rust/rust_oop.rs @@ -27,12 +27,16 @@ pub struct Student { use serde::{Serialize, Deserialize}; use map::Map; +use human::Human; use student::Student; #[derive(Serialize, Deserialize)] pub struct StudentMap { #[serde(rename = "totalResults")] total_results: Option, + #[serde(rename = "parent")] + parent: Option, + #[serde(rename = "entries")] entries: Option>, @@ -46,6 +50,9 @@ pub struct HumanMap { #[serde(rename = "totalResults")] total_results: Option, + #[serde(rename = "parent")] + parent: Option, + #[serde(rename = "entries")] entries: Option>, @@ -57,6 +64,9 @@ pub struct Map { #[serde(rename = "totalResults")] total_results: Option, + #[serde(rename = "parent")] + parent: Option

, + #[serde(rename = "entries")] entries: Option>, diff --git a/tests/Generator/resource/source_oop.json b/tests/Generator/resource/source_oop.json index 73f7632e..48d49a41 100644 --- a/tests/Generator/resource/source_oop.json +++ b/tests/Generator/resource/source_oop.json @@ -25,6 +25,7 @@ "type": "struct", "parent": "Map", "template": { + "P": "Human", "T": "Student" } }, @@ -32,6 +33,7 @@ "type": "struct", "parent": "Map", "template": { + "P": "Human", "T": "Human" } }, @@ -41,6 +43,10 @@ "totalResults": { "type": "integer" }, + "parent": { + "type": "generic", + "name": "P" + }, "entries": { "type": "array", "items": { diff --git a/tests/Generator/resource/swift/swift_oop.swift b/tests/Generator/resource/swift/swift_oop.swift index 936aa80f..c4f15d61 100644 --- a/tests/Generator/resource/swift/swift_oop.swift +++ b/tests/Generator/resource/swift/swift_oop.swift @@ -18,10 +18,12 @@ class Student: Human { class Map: Codable { var totalResults: Int + var parent: P var entries: Array enum CodingKeys: String, CodingKey { case totalResults = "totalResults" + case parent = "parent" case entries = "entries" } } diff --git a/tests/Generator/resource/typeschema/typeschema_import.json b/tests/Generator/resource/typeschema/typeschema_import.json index 9415c62d..5d54673c 100644 --- a/tests/Generator/resource/typeschema/typeschema_import.json +++ b/tests/Generator/resource/typeschema/typeschema_import.json @@ -18,6 +18,7 @@ "type": "reference", "target": "my_import:Map", "template": { + "P": "my_import:Human", "T": "my_import:Human" } } @@ -28,6 +29,9 @@ "totalResults": { "type": "integer" }, + "parent": { + "type": "generic" + }, "entries": { "type": "array", "schema": { @@ -63,6 +67,7 @@ "type": "reference", "target": "my_import:Map", "template": { + "P": "my_import:Human", "T": "my_import:Student" } } diff --git a/tests/Generator/resource/typeschema/typeschema_oop.json b/tests/Generator/resource/typeschema/typeschema_oop.json index f7586ec5..5147408c 100644 --- a/tests/Generator/resource/typeschema/typeschema_oop.json +++ b/tests/Generator/resource/typeschema/typeschema_oop.json @@ -18,6 +18,7 @@ "type": "reference", "target": "Map", "template": { + "P": "Human", "T": "Human" } } @@ -28,6 +29,9 @@ "totalResults": { "type": "integer" }, + "parent": { + "type": "generic" + }, "entries": { "type": "array", "schema": { @@ -63,6 +67,7 @@ "type": "reference", "target": "Map", "template": { + "P": "Human", "T": "Student" } } diff --git a/tests/Generator/resource/typescript/typescript_oop.ts b/tests/Generator/resource/typescript/typescript_oop.ts index d0b2abeb..9787e820 100644 --- a/tests/Generator/resource/typescript/typescript_oop.ts +++ b/tests/Generator/resource/typescript/typescript_oop.ts @@ -8,19 +8,21 @@ export class Student extends Human { matricleNumber?: string } -export class Map { +export class Map { totalResults?: number + parent?: P entries?: Array } import {Map} from "./Map"; +import {Human} from "./Human"; import {Student} from "./Student"; -export class StudentMap extends Map { +export class StudentMap extends Map { } import {Map} from "./Map"; import {Human} from "./Human"; -export class HumanMap extends Map { +export class HumanMap extends Map { } import {StudentMap} from "./StudentMap"; diff --git a/tests/Generator/resource/visualbasic/visualbasic_oop.vb b/tests/Generator/resource/visualbasic/visualbasic_oop.vb index f7e3f1d8..e33c4b3f 100644 --- a/tests/Generator/resource/visualbasic/visualbasic_oop.vb +++ b/tests/Generator/resource/visualbasic/visualbasic_oop.vb @@ -17,10 +17,13 @@ Public Class Student End Class Imports System.Text.Json.Serialization -Public Class Map(Of T) +Public Class Map(Of P, T) Public Property TotalResults As Integer + + Public Property Parent As P + Public Property Entries As T() @@ -28,12 +31,12 @@ End Class Imports System.Text.Json.Serialization Public Class StudentMap - Inherits Map(Of Student) + Inherits Map(Of Human, Student) End Class Imports System.Text.Json.Serialization Public Class HumanMap - Inherits Map(Of Human) + Inherits Map(Of Human, Human) End Class Imports System.Text.Json.Serialization