Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Headers support for OpenApi and Postman parsers #10

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified builds/sdkgenerator
Binary file not shown.
2 changes: 2 additions & 0 deletions src/Contracts/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
interface Parser
{
public function parse(): ApiSpecification;

public function getExcludedHeaders(): array;
}
3 changes: 3 additions & 0 deletions src/Data/Generator/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Endpoint
* @param Parameter[] $queryParameters
* @param Parameter[] $pathParameters
* @param Parameter[] $bodyParameters
* @param Parameter[] $headerParameters
*/
public function __construct(
public string $name,
Expand All @@ -22,6 +23,7 @@ public function __construct(
public array $queryParameters = [],
public array $pathParameters = [],
public array $bodyParameters = [],
public array $headerParameters = [],
) {
}

Expand All @@ -34,6 +36,7 @@ public function allParameters(): array
...$this->pathParameters,
...$this->bodyParameters,
...$this->queryParameters,
...$this->headerParameters,
];
}

Expand Down
14 changes: 14 additions & 0 deletions src/Generators/RequestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ protected function generateRequestClass(Endpoint $endpoint): PhpFile
MethodGeneratorHelper::generateArrayReturnMethod($classType, 'defaultQuery', $queryParams, withArrayFilterWrapper: true);
}

// Priority 4. - Header Parameters
if (! empty($endpoint->headerParameters)) {
$headerParams = collect($endpoint->headerParameters)
->reject(fn (Parameter $parameter) => in_array($parameter->name, $this->config->ignoredQueryParams))
->values()
->toArray();

foreach ($headerParams as $headerParam) {
MethodGeneratorHelper::addParameterAsPromotedProperty($classConstructor, $headerParam);
}

MethodGeneratorHelper::generateArrayReturnMethod($classType, 'defaultHeaders', $headerParams, withArrayFilterWrapper: true);
}

$namespace
->addUse(SaloonHttpMethod::class)
->addUse(DateTime::class)
Expand Down
10 changes: 8 additions & 2 deletions src/Parsers/OpenApiParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,18 @@ protected function parseEndpoint(Operation $operation, $pathParams, string $path
// TODO: Check if this differs between spec versions
pathParameters: $pathParams + $this->mapParams($operation->parameters, 'path'),
bodyParameters: [], // TODO: implement "definition" parsing
headerParameters: $this->mapParams($operation->parameters, 'header', $this->getExcludedHeaders()),
);
}

/**
* @param OpenApiParameter[] $parameters
* @return Parameter[] array
*/
protected function mapParams(array $parameters, string $in): array
protected function mapParams(array $parameters, string $in, array $exclude = []): array
{
return collect($parameters)
->filter(fn (OpenApiParameter $parameter) => $parameter->in == $in)
->filter(fn (OpenApiParameter $parameter) => $parameter->in == $in && ! in_array($parameter->name, $exclude))
->map(fn (OpenApiParameter $parameter) => new Parameter(
type: $this->mapSchemaTypeToPhpType($parameter->schema?->type),
nullable: $parameter->required == false,
Expand All @@ -107,4 +108,9 @@ protected function mapSchemaTypeToPhpType($type): string
default => 'mixed',
};
}

public function getExcludedHeaders(): array
{
return ['Authorization', 'Content-Type', 'Accept'];
}
}
32 changes: 31 additions & 1 deletion src/Parsers/PostmanCollectionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function parseEndpoint(Item $item): ?Endpoint
queryParameters: $this->parseQueryParameters($item),
pathParameters: $this->parsePathParameters($item),
bodyParameters: $this->parseBodyParameters($item),

headerParameters: $this->parseHeaderParameters($item),
);
}

Expand Down Expand Up @@ -151,4 +151,34 @@ protected function parseBodyParameters(Item $item): array
})
->toArray();
}

protected function parseHeaderParameters(Item $item): array
{
return collect($item->request->header)
->map(function ($param) {
$headerName = Arr::get($param, 'key');
if (! $headerName) {
return null;
}

// Exclude headers already handled in Connectors
if (in_array($headerName, $this->getExcludedHeaders())) {
return null;
}

return new Parameter(
type: 'string',
nullable: false,
name: $headerName,
);
})
->filter()
->values()
->toArray();
}

public function getExcludedHeaders(): array
{
return ['Authorization', 'Content-Type', 'Accept'];
}
}