Skip to content

Commit 8c9c5b6

Browse files
committed
Fix PHP jsonSerialize() methods to return correct outputs for empty objects
1 parent 787b1b2 commit 8c9c5b6

File tree

37 files changed

+205
-243
lines changed

37 files changed

+205
-243
lines changed

internal/jennies/php/rawtypes.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -604,23 +604,21 @@ func (jenny RawTypes) generateJSONSerialize(def ast.Object) string {
604604
var buffer strings.Builder
605605

606606
buffer.WriteString("/**\n")
607-
buffer.WriteString(" * @return array<string, mixed>\n")
607+
buffer.WriteString(" * @return mixed\n")
608608
buffer.WriteString(" */\n")
609-
buffer.WriteString("public function jsonSerialize(): array\n")
609+
buffer.WriteString("public function jsonSerialize(): mixed\n")
610610
buffer.WriteString("{\n")
611611

612-
buffer.WriteString(" $data = [\n")
612+
buffer.WriteString(" $data = new \\stdClass;\n")
613613

614614
for _, field := range def.Type.AsStruct().Fields {
615615
if field.Type.Nullable {
616616
continue
617617
}
618618

619-
buffer.WriteString(fmt.Sprintf(` "%s" => $this->%s,`+"\n", field.Name, formatFieldName(field.Name)))
619+
buffer.WriteString(fmt.Sprintf(` $data->%s = $this->%s;`+"\n", field.Name, formatFieldName(field.Name)))
620620
}
621621

622-
buffer.WriteString(" ];\n")
623-
624622
for _, field := range def.Type.AsStruct().Fields {
625623
if !field.Type.Nullable {
626624
continue
@@ -629,7 +627,7 @@ func (jenny RawTypes) generateJSONSerialize(def ast.Object) string {
629627
fieldName := formatFieldName(field.Name)
630628

631629
buffer.WriteString(fmt.Sprintf(" if (isset($this->%s)) {\n", fieldName))
632-
buffer.WriteString(fmt.Sprintf(` $data["%s"] = $this->%s;`+"\n", field.Name, fieldName))
630+
buffer.WriteString(fmt.Sprintf(` $data->%s = $this->%s;`+"\n", field.Name, fieldName))
633631
buffer.WriteString(" }\n")
634632
}
635633

testdata/jennies/rawtypes/arrays/PHPRawTypes/src/Arrays/SomeStruct.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ public static function fromArray(array $inputData): self
3030
}
3131

3232
/**
33-
* @return array<string, mixed>
33+
* @return mixed
3434
*/
35-
public function jsonSerialize(): array
35+
public function jsonSerialize(): mixed
3636
{
37-
$data = [
38-
"FieldAny" => $this->fieldAny,
39-
];
37+
$data = new \stdClass;
38+
$data->FieldAny = $this->fieldAny;
4039
return $data;
4140
}
4241
}

testdata/jennies/rawtypes/constant_references/PHPRawTypes/src/ConstantReferences/ParentStruct.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ public static function fromArray(array $inputData): self
2727
}
2828

2929
/**
30-
* @return array<string, mixed>
30+
* @return mixed
3131
*/
32-
public function jsonSerialize(): array
32+
public function jsonSerialize(): mixed
3333
{
34-
$data = [
35-
"myEnum" => $this->myEnum,
36-
];
34+
$data = new \stdClass;
35+
$data->myEnum = $this->myEnum;
3736
return $data;
3837
}
3938
}

testdata/jennies/rawtypes/constant_references/PHPRawTypes/src/ConstantReferences/Struct.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ public static function fromArray(array $inputData): self
3232
}
3333

3434
/**
35-
* @return array<string, mixed>
35+
* @return mixed
3636
*/
37-
public function jsonSerialize(): array
37+
public function jsonSerialize(): mixed
3838
{
39-
$data = [
40-
"myValue" => $this->myValue,
41-
"myEnum" => $this->myEnum,
42-
];
39+
$data = new \stdClass;
40+
$data->myValue = $this->myValue;
41+
$data->myEnum = $this->myEnum;
4342
return $data;
4443
}
4544
}

testdata/jennies/rawtypes/constant_references/PHPRawTypes/src/ConstantReferences/StructA.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ public static function fromArray(array $inputData): self
2121
}
2222

2323
/**
24-
* @return array<string, mixed>
24+
* @return mixed
2525
*/
26-
public function jsonSerialize(): array
26+
public function jsonSerialize(): mixed
2727
{
28-
$data = [
29-
"myEnum" => $this->myEnum,
30-
];
28+
$data = new \stdClass;
29+
$data->myEnum = $this->myEnum;
3130
return $data;
3231
}
3332
}

testdata/jennies/rawtypes/constant_references/PHPRawTypes/src/ConstantReferences/StructB.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ public static function fromArray(array $inputData): self
3030
}
3131

3232
/**
33-
* @return array<string, mixed>
33+
* @return mixed
3434
*/
35-
public function jsonSerialize(): array
35+
public function jsonSerialize(): mixed
3636
{
37-
$data = [
38-
"myEnum" => $this->myEnum,
39-
"myValue" => $this->myValue,
40-
];
37+
$data = new \stdClass;
38+
$data->myEnum = $this->myEnum;
39+
$data->myValue = $this->myValue;
4140
return $data;
4241
}
4342
}

testdata/jennies/rawtypes/constraints/PHPRawTypes/src/Constraints/RefStruct.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@ public static function fromArray(array $inputData): self
3838
}
3939

4040
/**
41-
* @return array<string, mixed>
41+
* @return mixed
4242
*/
43-
public function jsonSerialize(): array
43+
public function jsonSerialize(): mixed
4444
{
45-
$data = [
46-
"labels" => $this->labels,
47-
"tags" => $this->tags,
48-
];
45+
$data = new \stdClass;
46+
$data->labels = $this->labels;
47+
$data->tags = $this->tags;
4948
return $data;
5049
}
5150
}

testdata/jennies/rawtypes/constraints/PHPRawTypes/src/Constraints/SomeStruct.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,18 @@ public static function fromArray(array $inputData): self
4646
}
4747

4848
/**
49-
* @return array<string, mixed>
49+
* @return mixed
5050
*/
51-
public function jsonSerialize(): array
51+
public function jsonSerialize(): mixed
5252
{
53-
$data = [
54-
"id" => $this->id,
55-
"title" => $this->title,
56-
];
53+
$data = new \stdClass;
54+
$data->id = $this->id;
55+
$data->title = $this->title;
5756
if (isset($this->maybeId)) {
58-
$data["maybeId"] = $this->maybeId;
57+
$data->maybeId = $this->maybeId;
5958
}
6059
if (isset($this->refStruct)) {
61-
$data["refStruct"] = $this->refStruct;
60+
$data->refStruct = $this->refStruct;
6261
}
6362
return $data;
6463
}

testdata/jennies/rawtypes/dashboard/PHPRawTypes/src/Dashboard/Dashboard.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@ public static function fromArray(array $inputData): self
3939
}
4040

4141
/**
42-
* @return array<string, mixed>
42+
* @return mixed
4343
*/
44-
public function jsonSerialize(): array
44+
public function jsonSerialize(): mixed
4545
{
46-
$data = [
47-
"title" => $this->title,
48-
];
46+
$data = new \stdClass;
47+
$data->title = $this->title;
4948
if (isset($this->panels)) {
50-
$data["panels"] = $this->panels;
49+
$data->panels = $this->panels;
5150
}
5251
return $data;
5352
}

testdata/jennies/rawtypes/dashboard/PHPRawTypes/src/Dashboard/DataSourceRef.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,16 @@ public static function fromArray(array $inputData): self
3232
}
3333

3434
/**
35-
* @return array<string, mixed>
35+
* @return mixed
3636
*/
37-
public function jsonSerialize(): array
37+
public function jsonSerialize(): mixed
3838
{
39-
$data = [
40-
];
39+
$data = new \stdClass;
4140
if (isset($this->type)) {
42-
$data["type"] = $this->type;
41+
$data->type = $this->type;
4342
}
4443
if (isset($this->uid)) {
45-
$data["uid"] = $this->uid;
44+
$data->uid = $this->uid;
4645
}
4746
return $data;
4847
}

0 commit comments

Comments
 (0)