Browse Source

OAS: Don't include forbidden headers (fixes #625)

shalvah 2 years ago
parent
commit
56d589a484

+ 5 - 0
src/Writing/OpenAPISpecWriter.php

@@ -185,6 +185,11 @@ class OpenAPISpecWriter
 
         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,

+ 0 - 13
tests/Fixtures/openapi.yaml

@@ -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'
@@ -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

+ 2 - 9
tests/Unit/OpenAPISpecWriterTest.php

@@ -174,21 +174,14 @@ class OpenAPISpecWriterTest extends TestCase
         $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 */