Skip to content

Commit

Permalink
Apply feedback from code review
Browse files Browse the repository at this point in the history
* remove import of JsonSerializable
* add test for mixed (int and string) enums
  • Loading branch information
markuspoerschke committed Aug 25, 2023
1 parent 1304e30 commit 048c9e1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Asyncapi;
use JsonSerializable;
final class Root implements JsonSerializable
final class Root implements \\\\JsonSerializable
{
private ?string $email;
Expand Down
7 changes: 2 additions & 5 deletions src/generators/php/presets/JsonSerializablePreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ import { PhpRenderer } from '../PhpRenderer';
import { ConstrainedMetaModel } from '../../../models';

function renderSelf({
content,
renderer
content
}: {
content: string;
renderer: PhpRenderer<ConstrainedMetaModel>;
}): string {
renderer.dependencyManager.addDependency('use JsonSerializable;');

const contentLines = content.split('\n');
contentLines[0] += ` implements JsonSerializable`;
contentLines[0] += ` implements \\JsonSerializable`;

return contentLines.join('\n');
}
Expand Down
12 changes: 12 additions & 0 deletions test/generators/php/presets/JsonSerializablePreset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@ describe('PHP_JSON_SERIALIZABLE_PRESET', () => {
expect(models).toHaveLength(1);
expect(models[0].result).toMatchSnapshot();
});

test('should render jsonSerialize method for enum with mixed types', async () => {
const doc = {
$id: 'Enumm',
type: 'enum',
enum: [1, 'B']
};

const models = await generator.generate(doc);
expect(models).toHaveLength(1);
expect(models[0].result).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PHP_JSON_SERIALIZABLE_PRESET should render jsonSerialize method for class 1`] = `
"final class Clazz implements JsonSerializable
"final class Clazz implements \\\\JsonSerializable
{
private ?string $prop;
private ?string $propMinusWithMinusDash;
Expand Down Expand Up @@ -29,7 +29,7 @@ exports[`PHP_JSON_SERIALIZABLE_PRESET should render jsonSerialize method for cla
`;
exports[`PHP_JSON_SERIALIZABLE_PRESET should render jsonSerialize method for enum 1`] = `
"enum Enumm implements JsonSerializable
"enum Enumm implements \\\\JsonSerializable
{
case VALUE_MINUS_A;
case VALUE_MINUS_B;
Expand All @@ -44,3 +44,20 @@ exports[`PHP_JSON_SERIALIZABLE_PRESET should render jsonSerialize method for enu
}
"
`;
exports[`PHP_JSON_SERIALIZABLE_PRESET should render jsonSerialize method for enum with mixed types 1`] = `
"enum Enumm implements \\\\JsonSerializable
{
case NUMBER_1;
case B;
public function jsonSerialize(): mixed
{
return match($this) {
self::NUMBER_1 => 1,
self::B => \\"B\\",
};
}
}
"
`;

0 comments on commit 048c9e1

Please sign in to comment.