-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(oas): use
application/json
when consumes
media-type configura…
- Loading branch information
Showing
21 changed files
with
1,834 additions
and
37 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
packages/oas/src/converter/parts/Oas2MediaTypesResolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { OpenAPIV2 } from '@har-sdk/core'; | ||
|
||
export class Oas2MediaTypesResolver { | ||
private readonly DEFAULT_CONSUME_MEDIA_TYPE: OpenAPIV2.MimeTypes = [ | ||
'application/json' | ||
]; | ||
private readonly DEFAULT_PRODUCE_MEDIA_TYPE: OpenAPIV2.MimeTypes = ['*/*']; | ||
|
||
constructor(private readonly spec: OpenAPIV2.Document) {} | ||
|
||
public resolveToConsume(operation: OpenAPIV2.OperationObject) { | ||
return this.resolve(operation, 'consumes', this.DEFAULT_CONSUME_MEDIA_TYPE); | ||
} | ||
|
||
public resolveToProduce(operation: OpenAPIV2.OperationObject) { | ||
return this.resolve(operation, 'produces', this.DEFAULT_PRODUCE_MEDIA_TYPE); | ||
} | ||
|
||
private resolve( | ||
operation: OpenAPIV2.OperationObject, | ||
node: 'consumes' | 'produces', | ||
defaultMediaTypes: OpenAPIV2.MimeTypes | ||
): OpenAPIV2.MimeTypes { | ||
let mediaTypes: OpenAPIV2.MimeTypes; | ||
|
||
if (operation[node]?.length) { | ||
mediaTypes = operation[node]; | ||
} else if (this.spec[node]?.length) { | ||
mediaTypes = this.spec[node]; | ||
} | ||
|
||
mediaTypes = mediaTypes | ||
?.map((mediaType) => mediaType?.trim()) | ||
.filter(Boolean); | ||
|
||
return mediaTypes?.length ? mediaTypes : defaultMediaTypes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/oas/tests/fixtures/consumes-produces-missing.swagger.result.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[ | ||
{ | ||
"queryString": [], | ||
"url": "https://brokencrystals.com/api/v1/dummy1", | ||
"method": "POST", | ||
"headers": [ | ||
{ | ||
"name": "content-type", | ||
"value": "application/json" | ||
}, | ||
{ | ||
"name": "accept", | ||
"value": "*/*" | ||
} | ||
], | ||
"httpVersion": "HTTP/1.1", | ||
"cookies": [], | ||
"headersSize": 0, | ||
"bodySize": 0, | ||
"postData": { | ||
"mimeType": "application/json", | ||
"text": "{\"id\":42}" | ||
} | ||
} | ||
] |
26 changes: 26 additions & 0 deletions
26
packages/oas/tests/fixtures/consumes-produces-missing.swagger.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
swagger: '2.0' | ||
info: | ||
title: Future is Bright | ||
version: 1.0.0 | ||
host: brokencrystals.com | ||
basePath: /api/v1 | ||
schemes: | ||
- https | ||
paths: | ||
'/dummy1': | ||
post: | ||
parameters: | ||
- in: body | ||
name: body | ||
schema: | ||
$ref: '#/definitions/requestBody' | ||
responses: | ||
200: | ||
description: successful operation | ||
schema: {} | ||
definitions: | ||
requestBody: | ||
type: object | ||
properties: | ||
id: | ||
type: integer |
25 changes: 25 additions & 0 deletions
25
packages/oas/tests/fixtures/consumes-produces-operation.swagger.result.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[ | ||
{ | ||
"bodySize": 0, | ||
"cookies": [], | ||
"headers": [ | ||
{ | ||
"name": "content-type", | ||
"value": "application/xml" | ||
}, | ||
{ | ||
"name": "accept", | ||
"value": "application/xml" | ||
} | ||
], | ||
"headersSize": 0, | ||
"httpVersion": "HTTP/1.1", | ||
"method": "POST", | ||
"postData": { | ||
"mimeType": "application/xml", | ||
"text": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<requestBody>\n\t<id>42</id>\n</requestBody>" | ||
}, | ||
"queryString": [], | ||
"url": "https://petstore.swagger.io/v2/dummy1" | ||
} | ||
] |
30 changes: 30 additions & 0 deletions
30
packages/oas/tests/fixtures/consumes-produces-operation.swagger.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
swagger: '2.0' | ||
info: | ||
title: Swagger Petstore | ||
version: 1.0.0 | ||
host: petstore.swagger.io | ||
basePath: /v2 | ||
schemes: | ||
- https | ||
paths: | ||
'/dummy1': | ||
post: | ||
consumes: | ||
- application/xml | ||
produces: | ||
- application/xml | ||
parameters: | ||
- in: body | ||
name: body | ||
schema: | ||
$ref: '#/definitions/requestBody' | ||
responses: | ||
200: | ||
description: successful operation | ||
schema: {} | ||
definitions: | ||
requestBody: | ||
type: object | ||
properties: | ||
id: | ||
type: integer |
48 changes: 48 additions & 0 deletions
48
packages/oas/tests/fixtures/consumes-produces-override.swagger.result.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
[ | ||
{ | ||
"bodySize": 0, | ||
"cookies": [], | ||
"headers": [ | ||
{ | ||
"name": "content-type", | ||
"value": "application/json" | ||
}, | ||
{ | ||
"name": "accept", | ||
"value": "*/*" | ||
} | ||
], | ||
"headersSize": 0, | ||
"httpVersion": "HTTP/1.1", | ||
"method": "POST", | ||
"postData": { | ||
"mimeType": "application/json", | ||
"text": "{\"id\":42}" | ||
}, | ||
"queryString": [], | ||
"url": "https://petstore.swagger.io/v2/dummy1" | ||
}, | ||
{ | ||
"bodySize": 0, | ||
"cookies": [], | ||
"headers": [ | ||
{ | ||
"name": "content-type", | ||
"value": "multipart/form-data" | ||
}, | ||
{ | ||
"name": "accept", | ||
"value": "application/vnd.api+json" | ||
} | ||
], | ||
"headersSize": 0, | ||
"httpVersion": "HTTP/1.1", | ||
"method": "PUT", | ||
"postData": { | ||
"mimeType": "multipart/form-data; boundary=956888039105887155673143", | ||
"text": "--956888039105887155673143\r\nContent-Disposition: form-data; name=\"id\"\r\n\r\n42\r\n--956888039105887155673143--" | ||
}, | ||
"queryString": [], | ||
"url": "https://petstore.swagger.io/v2/dummy1" | ||
} | ||
] |
Oops, something went wrong.