Skip to content

Commit 3fe3c9b

Browse files
committed
fix dotnet and graphql
1 parent 9bab746 commit 3fe3c9b

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

src/SDK/Language/DotNet.php

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,17 @@ public function getParamExample(array $param): string
285285
$output .= $example;
286286
break;
287287
case self::TYPE_OBJECT:
288-
$output .= ($example === '{}')
289-
? '[object]'
290-
: (($formatted = json_encode(json_decode($example, true), JSON_PRETTY_PRINT))
291-
? 'new ' . preg_replace('/\n/', "\n ", $formatted)
292-
: 'new ' . $example);
288+
if ($example === '{}') {
289+
$output .= '[object]';
290+
} else {
291+
$decoded = json_decode($example, true);
292+
if ($decoded && is_array($decoded)) {
293+
$csharpObject = $this->formatCSharpAnonymousObject($decoded, 1);
294+
$output .= 'new ' . $csharpObject;
295+
} else {
296+
$output .= 'new ' . $example;
297+
}
298+
}
293299
break;
294300
case self::TYPE_BOOLEAN:
295301
$output .= ($example) ? 'true' : 'false';
@@ -458,4 +464,48 @@ public function getFilters(): array
458464
}),
459465
];
460466
}
467+
468+
/**
469+
* Format a PHP array as a C# anonymous object
470+
*/
471+
private function formatCSharpAnonymousObject(array $data, int $indentLevel = 0): string
472+
{
473+
$propertyIndent = str_repeat(' ', $indentLevel + 1);
474+
$properties = [];
475+
476+
foreach ($data as $key => $value) {
477+
$formattedValue = $this->formatCSharpValue($value, $indentLevel + 2);
478+
$properties[] = $propertyIndent . $key . ' = ' . $formattedValue;
479+
}
480+
481+
if (count($properties) === 1) {
482+
return '{ ' . trim($properties[0]) . ' }';
483+
}
484+
485+
$baseIndent = str_repeat(' ', $indentLevel);
486+
return "{\n" . implode(",\n", $properties) . "\n" . $baseIndent . "}";
487+
}
488+
489+
/**
490+
* Format a value for C# anonymous object property
491+
*/
492+
private function formatCSharpValue($value, int $indentLevel = 0): string
493+
{
494+
if (is_string($value)) {
495+
return '"' . $value . '"';
496+
} elseif (is_bool($value)) {
497+
return $value ? 'true' : 'false';
498+
} elseif (is_numeric($value)) {
499+
return (string) $value;
500+
} elseif (is_array($value)) {
501+
if (array_keys($value) !== range(0, count($value) - 1)) {
502+
return $this->formatCSharpAnonymousObject($value, $indentLevel);
503+
} else {
504+
$items = array_map(fn($item) => $this->formatCSharpValue($item, $indentLevel), $value);
505+
return 'new[] { ' . implode(', ', $items) . ' }';
506+
}
507+
} else {
508+
return 'null';
509+
}
510+
}
461511
}

src/SDK/Language/GraphQL.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,8 @@ public function getParamExample(array $param): string
137137
self::TYPE_ARRAY, self::TYPE_FILE, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
138138
self::TYPE_BOOLEAN => ($example) ? 'true' : 'false',
139139
self::TYPE_OBJECT => ($example === '{}')
140-
? '"{}"'
141-
: (($formatted = json_encode(json_decode($example, true), JSON_PRETTY_PRINT))
142-
? preg_replace('/\n/', "\n ", $formatted)
143-
: $example),
140+
? '"{}"'
141+
: '"' . str_replace('"', '\\"', json_encode(json_decode($example, true))) . '"',
144142
self::TYPE_STRING => '"' . $example . '"',
145143
};
146144
}

0 commit comments

Comments
 (0)