-
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…
…tion missing closes #210
- Loading branch information
Showing
15 changed files
with
4,008 additions
and
1,825 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
import { isOASV2 } from '../../utils'; | ||
|
||
import { OpenAPIV2 } from '@har-sdk/core'; | ||
|
||
export class Oas2MediaTypesResolver { | ||
private readonly DEFAULT_MEDIA_TYPE = 'application/json'; | ||
|
||
constructor(private readonly spec: OpenAPIV2.Document) {} | ||
|
||
public resolveToConsume(operation: OpenAPIV2.OperationObject) { | ||
let mediaTypes = this.resolve(operation, 'consumes'); | ||
if (!mediaTypes?.length) { | ||
mediaTypes = [this.DEFAULT_MEDIA_TYPE]; | ||
} | ||
|
||
return mediaTypes; | ||
} | ||
|
||
public resolveToProduce(operation: OpenAPIV2.OperationObject) { | ||
const mediaTypes = this.resolve(operation, 'produces'); | ||
|
||
return mediaTypes?.length ? mediaTypes : undefined; | ||
} | ||
|
||
private resolve( | ||
operation: OpenAPIV2.OperationObject, | ||
node: 'consumes' | 'produces' | ||
): OpenAPIV2.MimeTypes { | ||
let mediaTypes: OpenAPIV2.MimeTypes; | ||
|
||
if (operation[node]?.length) { | ||
mediaTypes = operation[node]; | ||
} else if (isOASV2(this.spec) && this.spec[node]?.length) { | ||
mediaTypes = this.spec[node]; | ||
} | ||
|
||
return mediaTypes?.map((mediaType) => mediaType?.trim()).filter(Boolean); | ||
} | ||
} |
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
Oops, something went wrong.