Skip to content

Commit

Permalink
OAS: Don't include forbidden headers (fixes #625)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Apr 8, 2023
1 parent eb5e1b3 commit 56d589a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 22 deletions.
5 changes: 5 additions & 0 deletions src/Writing/OpenAPISpecWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ protected function generateEndpointParametersSpec(OutputEndpointData $endpoint):

if (count($endpoint->headers)) {
foreach ($endpoint->headers as $name => $value) {
if (in_array($name, ['Content-Type', 'content-type', 'Accept', 'accept']))
// These headers are not allowed in the spec.
// https://swagger.io/docs/specification/describing-parameters/#header-parameters
continue;

$parameters[] = [
'in' => 'header',
'name' => $name,
Expand Down
13 changes: 0 additions & 13 deletions tests/Fixtures/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ paths:
example: NotSoCustom
schema:
type: string
-
in: header
name: Content-Type
description: ''
example: multipart/form-data
schema:
type: string
responses: { }
tags:
- 'Group A'
Expand Down Expand Up @@ -280,12 +273,6 @@ paths:
example: NotSoCustom
schema:
type: string
- in: header
name: Content-Type
description: ''
example: application/json
schema:
type: string
responses: {}
tags:
- Group A
Expand Down
11 changes: 2 additions & 9 deletions tests/Unit/OpenAPISpecWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,14 @@ public function adds_headers_correctly_as_parameters_on_operation_object()
$results = $this->generate($groups);

$this->assertEquals([], $results['paths']['/path1']['get']['parameters']);
$this->assertCount(2, $results['paths']['/path1']['post']['parameters']);
$this->assertEquals([
'in' => 'header',
'name' => 'Content-Type',
'description' => '',
'example' => 'application/json',
'schema' => ['type' => 'string'],
], $results['paths']['/path1']['post']['parameters'][0]);
$this->assertCount(1, $results['paths']['/path1']['post']['parameters']);
$this->assertEquals([
'in' => 'header',
'name' => 'Extra-Header',
'description' => '',
'example' => 'Some-example',
'schema' => ['type' => 'string'],
], $results['paths']['/path1']['post']['parameters'][1]);
], $results['paths']['/path1']['post']['parameters'][0]);
}

/** @test */
Expand Down

0 comments on commit 56d589a

Please sign in to comment.