Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tags management #60

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ function extract(obj, options) {
src.basePath = obj.basePath;
}
}
if(obj.tags) {
src.tags = obj.tags
}

src.paths = {};
if (src.openapi) {
src.components = {};
Expand Down Expand Up @@ -102,15 +106,19 @@ function extract(obj, options) {
}
let paths = {};

const usedTags = new Set();

if (options.operationid.length) {
for (let id of options.operationid) {
for (let p in obj.paths) {
for (let o in obj.paths[p]) {
let op = obj.paths[p][o];
if (op.operationId && op.operationId === id) {
if (op.operationId && op.operationId === id.trim()) {
if (!paths[p]) paths[p] = {};
paths[p][o] = clone(op);
deref(paths[p][o],src,obj);
if(op && op.tags)
usedTags.add(...op.tags);
}
}
}
Expand All @@ -123,6 +131,7 @@ function extract(obj, options) {
if (options.method && obj.paths[options.path][options.method]) {
paths[options.path][options.method] = clone(obj.paths[options.path][options.method]);
deref(paths[options.path][options.method],src,obj);
usedTags.add(...paths[options.path][o].tags);
}
else if (options.path) {
for (let o in obj.paths[options.path]) {
Expand All @@ -131,6 +140,7 @@ function extract(obj, options) {
if (!options.method || options.method === o) {
paths[options.path][o] = clone(obj.paths[options.path][o]);
deref(paths[options.path][o],src,obj);
usedTags.add(...paths[options.path][o].tags);
}
}
}
Expand All @@ -157,11 +167,11 @@ function extract(obj, options) {
}
}

deref(src.definitions,src,obj);
deref(src.headers,src,obj);
deref(src.responses,src,obj);
deref(src.parameters,src,obj);
deref(src.components,src,obj);
deref(src.definitions,src,obj);

if (options.openai) {
recurse(src,{},function(obj,key,state){
Expand Down Expand Up @@ -202,6 +212,10 @@ function extract(obj, options) {
}
}

if (src.tags) {
src.tags = src.tags.filter(tag => usedTags.has(tag.name));
}

return src;
}

Expand Down
86 changes: 86 additions & 0 deletions test/tags/input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
swagger: '2.0'
schemes:
- http
host: xkcd.com
basePath: /
info:
description: 'Webcomic of romance, sarcasm, math, and language.'
title: XKCD
version: 1.0.0
x-apisguru-categories:
- media
x-logo:
url: 'http://imgs.xkcd.com/static/terrible_small_logo.png'
x-origin:
- format: swagger
url: 'https://raw.githubusercontent.com/APIs-guru/unofficial_openapi_specs/master/xkcd.com/1.0.0/swagger.yaml'
version: '2.0'
x-providerName: xkcd.com
x-tags:
- humor
- comics
x-unofficialSpec: true
tags:
- name: fetch
description: fetches a specific entity
- name: latest
description: retrieve the latest
externalDocs:
url: 'https://xkcd.com/json.html'
securityDefinitions: {}
paths:
/info.0.json:
get:
operationId: getLatest
description: |
Fetch current comic and metadata.
responses:
'200':
description: OK
schema:
$ref: '#/definitions/comic'
tags:
- latest
'/{comicId}/info.0.json':
get:
operationId: getComic
description: |
Fetch comics and metadata by comic id.
parameters:
- in: path
name: comicId
required: true
type: number
responses:
'200':
description: OK
schema:
$ref: '#/definitions/comic'
tags:
- fetch
definitions:
comic:
properties:
alt:
type: string
day:
type: string
img:
type: string
link:
type: string
month:
type: string
news:
type: string
num:
type: number
safe_title:
type: string
title:
type: string
transcript:
type: string
year:
type: string
type: object
1 change: 1 addition & 0 deletions test/tags/options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
operationid: getLatest
46 changes: 46 additions & 0 deletions test/tags/output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
swagger: "2.0"
info:
title: XKCD
version: 1.0.0
tags:
- name: latest
description: retrieve the latest
paths:
/info.0.json:
get:
operationId: getLatest
description: |
Fetch current comic and metadata.
responses:
"200":
description: OK
schema:
$ref: "#/definitions/comic"
tags:
- latest
definitions:
comic:
properties:
alt:
type: string
day:
type: string
img:
type: string
link:
type: string
month:
type: string
news:
type: string
num:
type: number
safe_title:
type: string
title:
type: string
transcript:
type: string
year:
type: string
type: object
89 changes: 89 additions & 0 deletions test/tags3/input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
openapi: 3.0.0
info:
description: Webcomic of romance, sarcasm, math, and language.
title: XKCD
version: 1.0.0
x-apisguru-categories:
- media
x-logo:
url: http://imgs.xkcd.com/static/terrible_small_logo.png
x-origin:
- format: swagger
url: https://raw.githubusercontent.com/APIs-guru/unofficial_openapi_specs/master/xkcd.com/1.0.0/swagger.yaml
version: "2.0"
x-providerName: xkcd.com
x-tags:
- humor
- comics
x-unofficialSpec: true
externalDocs:
url: https://xkcd.com/json.html
tags:
- name: fetch
description: fetches a specific entity
- name: latest
description: retrieve the latest
paths:
/info.0.json:
get:
operationId: getLatest
description: |
Fetch current comic and metadata.
responses:
"200":
description: OK
content:
"*/*":
schema:
$ref: "#/components/schemas/comic"
tags:
- latest
"/{comicId}/info.0.json":
get:
operationId: getComic
description: |
Fetch comics and metadata by comic id.
parameters:
- in: path
name: comicId
required: true
schema:
type: number
responses:
"200":
description: OK
content:
"*/*":
schema:
$ref: "#/components/schemas/comic"
tags:
- fetch
servers:
- url: http://xkcd.com/
components:
schemas:
comic:
properties:
alt:
type: string
day:
type: string
img:
type: string
link:
type: string
month:
type: string
news:
type: string
num:
type: number
safe_title:
type: string
title:
type: string
transcript:
type: string
year:
type: string
type: object
1 change: 1 addition & 0 deletions test/tags3/options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
operationid: getLatest
49 changes: 49 additions & 0 deletions test/tags3/output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
openapi: 3.0.0
info:
title: XKCD
version: 1.0.0
tags:
- name: latest
description: retrieve the latest
paths:
/info.0.json:
get:
operationId: getLatest
description: |
Fetch current comic and metadata.
responses:
"200":
description: OK
content:
"*/*":
schema:
$ref: "#/components/schemas/comic"
tags:
- latest
components:
schemas:
comic:
properties:
alt:
type: string
day:
type: string
img:
type: string
link:
type: string
month:
type: string
news:
type: string
num:
type: number
safe_title:
type: string
title:
type: string
transcript:
type: string
year:
type: string
type: object