-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(handler): added new handler for no custome media types
- Loading branch information
1 parent
3c58380
commit 3bb48ad
Showing
16 changed files
with
8,778 additions
and
14 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
openapi: 3.0.1 | ||
servers: | ||
- url: 'https://api.geodatasource.com' | ||
info: | ||
contact: | ||
x-twitter: _geodatasource | ||
description: 'GeoDataSourceâ„¢ Web Service is a REST API enable user to lookup for a city by using latitude and longitude coordinate. It will return the result in either JSON or XML containing the information of country, region, city, latitude and longitude. Visit https://www.geodatasource.com/web-service for further information.' | ||
title: GeoDataSource Location Search | ||
version: '1.0' | ||
x-apisguru-categories: | ||
- location | ||
x-logo: | ||
url: 'https://api.apis.guru/v2/cache/logo/https_twitter.com__geodatasource_profile_image.png' | ||
x-origin: | ||
- converter: | ||
url: 'https://github.com/lucybot/api-spec-converter' | ||
version: 2.7.31 | ||
format: openapi | ||
url: 'https://app.swaggerhub.com/apiproxy/schema/file/geodatasource/geodatasource-location-search/1.0/swagger.yaml' | ||
version: '3.0' | ||
x-preferred: true | ||
x-providerName: geodatasource.com | ||
paths: | ||
/city: | ||
get: | ||
description: Get City name by using latitude and longitude | ||
parameters: | ||
- in: query | ||
name: key | ||
required: true | ||
schema: | ||
type: string | ||
- in: query | ||
name: lng | ||
required: true | ||
schema: | ||
type: number | ||
- in: query | ||
name: lat | ||
required: true | ||
schema: | ||
type: number | ||
- in: query | ||
name: format | ||
schema: | ||
enum: | ||
- json | ||
- xml | ||
type: string | ||
responses: | ||
'200': | ||
content: | ||
custom-media-type: | ||
examples: | ||
'0': | ||
value: '{"country":"","region":"","city":"","latitude":"","longitude":""}' | ||
schema: | ||
type: string | ||
description: Get response from longitude latitude lookup |
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,59 @@ | ||
openapi: 3.0.1 | ||
servers: | ||
- url: 'https://api.geodatasource.com' | ||
info: | ||
contact: | ||
x-twitter: _geodatasource | ||
description: 'GeoDataSourceâ„¢ Web Service is a REST API enable user to lookup for a city by using latitude and longitude coordinate. It will return the result in either JSON or XML containing the information of country, region, city, latitude and longitude. Visit https://www.geodatasource.com/web-service for further information.' | ||
title: GeoDataSource Location Search | ||
version: '1.0' | ||
x-apisguru-categories: | ||
- location | ||
x-logo: | ||
url: 'https://api.apis.guru/v2/cache/logo/https_twitter.com__geodatasource_profile_image.png' | ||
x-origin: | ||
- converter: | ||
url: 'https://github.com/lucybot/api-spec-converter' | ||
version: 2.7.31 | ||
format: openapi | ||
url: 'https://app.swaggerhub.com/apiproxy/schema/file/geodatasource/geodatasource-location-search/1.0/swagger.yaml' | ||
version: '3.0' | ||
x-preferred: true | ||
x-providerName: geodatasource.com | ||
paths: | ||
/city: | ||
get: | ||
description: Get City name by using latitude and longitude | ||
parameters: | ||
- in: query | ||
name: key | ||
required: true | ||
schema: | ||
type: string | ||
- in: query | ||
name: lng | ||
required: true | ||
schema: | ||
type: number | ||
- in: query | ||
name: lat | ||
required: true | ||
schema: | ||
type: number | ||
- in: query | ||
name: format | ||
schema: | ||
enum: | ||
- json | ||
- xml | ||
type: string | ||
responses: | ||
'200': | ||
content: | ||
application/vnd.teste.123: | ||
examples: | ||
'0': | ||
value: '{"country":"","region":"","city":"","latitude":"","longitude":""}' | ||
schema: | ||
type: string | ||
description: Get response from longitude latitude lookup |
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,45 @@ | ||
import path from 'path'; | ||
import { parse } from '../../../index'; | ||
import { noCustomMediaType } from '../../../rules/handlers'; | ||
import { RuleFault, Severity } from '../../../rules/rule-fault'; | ||
|
||
describe('noCustomMediaType function', () => { | ||
const apiFileWithoutErrors = path.join(__dirname, '..', '..', 'data', 'openapi-3.0', 'swagger.yml'); | ||
const apiFileWithCustomMediaType = path.join(__dirname, '..', '..', 'data', 'with-custom-media-type', 'swagger.yml'); | ||
const apiFileWithVendorMediaType = path.join(__dirname, '..', '..', 'data', 'with-vendor-media-type', 'swagger.yml'); | ||
|
||
test('should have no faults', async () => { | ||
const api = await parse(apiFileWithoutErrors); | ||
|
||
const faults: RuleFault[] = []; | ||
|
||
noCustomMediaType(api, faults); | ||
|
||
expect(faults.length).toBe(0); | ||
}); | ||
|
||
test('should have one fault with error', async () => { | ||
const api = await parse(apiFileWithCustomMediaType); | ||
|
||
const faults: RuleFault[] = []; | ||
|
||
noCustomMediaType(api, faults); | ||
|
||
expect(faults.length).toBe(1); | ||
expect(faults[0].errors.length).toBe(1); | ||
expect(faults[0].errors[0].severity).toBe(Severity.error); | ||
}); | ||
|
||
test('should have one fault with warning', async () => { | ||
const api = await parse(apiFileWithVendorMediaType); | ||
|
||
const faults: RuleFault[] = []; | ||
|
||
noCustomMediaType(api, faults); | ||
|
||
expect(faults.length).toBe(1); | ||
expect(faults[0].errors.length).toBe(1); | ||
expect(faults[0].errors[0].severity).toBe(Severity.warning); | ||
}); | ||
|
||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare module 'media-typer' { | ||
function test(mediaType: string): 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { OpenAPI } from "openapi-types"; | ||
import mime from 'mime-types'; | ||
import { RuleFault, Severity, RuleFaultContent } from "../rule-fault"; | ||
import { produceRuleFaultForPathMethod, pushFault } from "./util"; | ||
|
||
const faults = { | ||
nonStandardMediaType: `This content media-type doesn't follow RFC 6838 standard media-types`, | ||
vndMediaType: `This content media-type uses custom vendor definitions in RFC 6838 and it should be replaced as a standard one` | ||
}; | ||
|
||
const produceNonStandardMediaType = (path: string, method: string, httpStatus: string): RuleFault => { | ||
return { | ||
value: produceRuleFaultForPathMethod(path, method, httpStatus), | ||
errors: [ | ||
{ | ||
severity: Severity.error, | ||
message: faults.nonStandardMediaType | ||
} as RuleFaultContent | ||
] | ||
}; | ||
}; | ||
|
||
const produceVendorMediaType = (path: string, method: string, httpStatus: string): RuleFault => { | ||
return { | ||
value: produceRuleFaultForPathMethod(path, method, httpStatus), | ||
errors: [ | ||
{ | ||
severity: Severity.warning, | ||
message: faults.vndMediaType | ||
} as RuleFaultContent | ||
] | ||
}; | ||
}; | ||
|
||
const isStandardMediaType = (mediaType: string) => { | ||
return !!mime.extension(mediaType); | ||
}; | ||
|
||
const containsVendorInMediaType = (mediaType: string) => { | ||
const vendorPattern = /^\w+\/vnd(\.\w+)+$/; | ||
return vendorPattern.test(mediaType); | ||
}; | ||
|
||
export const noCustomMediaType = (api: OpenAPI.Document, ruleFaults: RuleFault[]) => { | ||
const apiParsed: any = api; | ||
|
||
Object.entries(apiParsed.paths).forEach(([path, pathValue]) => { | ||
Object.entries(pathValue as any).forEach(([method, methodValue]) => { | ||
const responses = Object.keys((methodValue as any).responses); | ||
|
||
responses.forEach(response => { | ||
const mediaTypes = Object.keys((methodValue as any).responses[response].content); | ||
|
||
mediaTypes.forEach(mediaType => { | ||
const mainMediaType = mediaType.split(';')[0]; | ||
|
||
if(containsVendorInMediaType(mainMediaType)) { | ||
pushFault(produceVendorMediaType(path, method, response), ruleFaults); | ||
} else if(!isStandardMediaType(mainMediaType)) { | ||
pushFault(produceNonStandardMediaType(path, method, response), ruleFaults); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
}, | ||
"files": [ | ||
"src/index.ts", | ||
"src/cli.ts" | ||
"src/cli.ts", | ||
"src/declaration.d.ts" | ||
] | ||
} |
Oops, something went wrong.