Skip to content

Commit

Permalink
Resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah authored Feb 3, 2025
1 parent b55775c commit adb08d4
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/Writing/OpenApiSpecGenerators/BaseGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected function generateEndpointParametersSpec(OutputEndpointData $endpoint):
return $parameters;
}

protected function generateEndpointRequestBodySpec(OutputEndpointData $endpoint)
protected function generateEndpointRequestBodySpec(OutputEndpointData $endpoint): array|\stdClass
{
$body = [];

Expand Down Expand Up @@ -368,9 +368,9 @@ protected function generateResponseContentSpec(?string $responseContent, OutputE

case 'object':
$properties = collect($decoded)->mapWithKeys(function ($value, $key) use ($endpoint) {
return [$key => $this->generateSchemaForValue($value, $endpoint, $key)];
return [$key => $this->generateSchemaForResponseValue($value, $endpoint, $key)];
})->toArray();
$required = $this->filterRequiredFields($endpoint, array_keys($properties));
$required = $this->filterRequiredResponseFields($endpoint, array_keys($properties));

$data = [
'application/json' => [
Expand Down Expand Up @@ -467,15 +467,21 @@ public function generateFieldData($field): array

return $fieldData;
} else if ($field->type === 'object') {
return [
$data = [
'type' => 'object',
'description' => $field->description ?: '',
'example' => $field->example,
'nullable'=> $field->nullable,
'properties' => $this->objectIfEmpty(collect($field->__fields)->mapWithKeys(function ($subfield, $subfieldName) {
return [$subfieldName => $this->generateFieldData($subfield)];
})->all()),
'required' => collect($field->__fields)->filter(fn ($f) => $f['required'])->keys()->toArray(),
];
// The spec doesn't allow for an empty `required` array. Must have something there.
if (empty($data['required'])) {
unset($data['required']);
}
return $data;
} else {
$schema = [
'type' => static::normalizeTypeName($field->type),
Expand All @@ -497,17 +503,17 @@ public function generateFieldData($field): array
* object)}, and possibly a description for each property. The $endpoint and $path are used for looking up response
* field descriptions.
*/
public function generateSchemaForValue(mixed $value, OutputEndpointData $endpoint, string $path): array
public function generateSchemaForResponseValue(mixed $value, OutputEndpointData $endpoint, string $path): array
{
if ($value instanceof \stdClass) {
$value = (array)$value;
$properties = [];
// Recurse into the object
foreach ($value as $subField => $subValue) {
$subFieldPath = sprintf('%s.%s', $path, $subField);
$properties[$subField] = $this->generateSchemaForValue($subValue, $endpoint, $subFieldPath);
$properties[$subField] = $this->generateSchemaForResponseValue($subValue, $endpoint, $subFieldPath);
}
$required = $this->filterRequiredFields($endpoint, array_keys($properties), $path);
$required = $this->filterRequiredResponseFields($endpoint, array_keys($properties), $path);

$schema = [
'type' => 'object',
Expand Down Expand Up @@ -541,11 +547,11 @@ public function generateSchemaForValue(mixed $value, OutputEndpointData $endpoin

if ($typeOfEachItem === 'object') {
$schema['items']['properties'] = collect($sample)->mapWithKeys(function ($v, $k) use ($endpoint, $path) {
return [$k => $this->generateSchemaForValue($v, $endpoint, "$path.$k")];
return [$k => $this->generateSchemaForResponseValue($v, $endpoint, "$path.$k")];
})->toArray();
}

$required = $this->filterRequiredFields($endpoint, array_keys($schema['items']['properties']), $path);
$required = $this->filterRequiredResponseFields($endpoint, array_keys($schema['items']['properties']), $path);
if ($required) {
$schema['required'] = $required;
}
Expand All @@ -558,7 +564,7 @@ public function generateSchemaForValue(mixed $value, OutputEndpointData $endpoin
/**
* Given an enpoint and a set of object keys at a path, return the properties that are specified as required.
*/
public function filterRequiredFields(OutputEndpointData $endpoint, array $properties, string $path = ''): array
public function filterRequiredResponseFields(OutputEndpointData $endpoint, array $properties, string $path = ''): array
{
$required = [];
foreach ($properties as $property) {
Expand Down

0 comments on commit adb08d4

Please sign in to comment.