Skip to content

Commit a597d5b

Browse files
committed
feat: openai compliant option
Signed-off-by: Mike Ralphson <[email protected]>
1 parent d330458 commit a597d5b

File tree

6 files changed

+52
-6
lines changed

6 files changed

+52
-6
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ Usage: openapi-extract [options] {infile} [{outfile}]
1212
Options:
1313
-h, --help Show help [boolean]
1414
--version Show version number [boolean]
15+
--openai make the definition OpenAI compliant [boolean]
1516
--server include server information [boolean]
1617
--shard shard the input to an output directory [string]
1718
-p, --path the path to extract [string]
1819
-o, --operationid the operationIds to extract [array]
1920
-m, --method the method to extract for the given path [string]
2021
-i, --info copy full info object, otherwise minimal [boolean]
22+
-d, --removeDocs remove all externalDocs properties [boolean]
2123
-r, --removeExamples remove all example/examples properties [boolean]
2224
-x, --removeExtensions remove all x- extension properties [boolean]
2325
-s, --security include security information [boolean]
@@ -42,8 +44,16 @@ The `options` object takes the same values as the CLI, for these keys and defaul
4244
* path = ''
4345
* method = ''
4446
* info = false
47+
* openai = false
48+
* removeDocs = false
4549
* removeExamples = false
50+
* removeExtensions = false
4651
* server = false
4752
* security = false
4853
* operationid = []
4954

55+
## OpenAI compliant mode
56+
57+
This option turns on the following rules:
58+
59+
1. The `description` properties must have a maximum length of 300 characters

index.js

+23-6
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const { AList } = require('./lib/AList.js');
88
// TODO add a memoized cache
99

1010
function clean(obj, property) {
11-
if (obj && (typeof obj[property] === 'object') && (!Object.keys(obj[property]).length)) {
12-
delete obj[property];
11+
if (obj && (!Array.isArray(obj[property])) && (typeof obj[property] === 'object') && (!Object.keys(obj[property]).length)) {
12+
if (property !== 'paths') delete obj[property];
1313
}
1414
return obj;
1515
}
@@ -36,10 +36,12 @@ function extract(obj, options) {
3636

3737
const defaults = {};
3838
defaults.info = false;
39+
defaults.removeDocs = false;
3940
defaults.removeExamples = false;
4041
defaults.removeExtensions = false;
4142
defaults.server = false;
4243
defaults.security = false;
44+
defaults.openai = false;
4345
defaults.operationid = [];
4446
options = Object.assign({},defaults,options);
4547

@@ -116,13 +118,13 @@ function extract(obj, options) {
116118
}
117119
else {
118120
paths = {};
119-
paths[options.path] = {};
121+
if (options.path) paths[options.path] = {};
120122
if (options.method) paths[options.path][options.method] = {};
121123
if (options.method && obj.paths[options.path][options.method]) {
122124
paths[options.path][options.method] = clone(obj.paths[options.path][options.method]);
123125
deref(paths[options.path][options.method],src,obj);
124126
}
125-
else {
127+
else if (options.path) {
126128
for (let o in obj.paths[options.path]) {
127129
if ((o !== 'description') && (o !== 'summary') &&
128130
(!o.startsWith('x-'))) {
@@ -161,6 +163,15 @@ function extract(obj, options) {
161163
deref(src.parameters,src,obj);
162164
deref(src.components,src,obj);
163165

166+
if (options.openai) {
167+
recurse(src,{},function(obj,key,state){
168+
if (obj && key === 'description' && obj.description.length > 300) {
169+
state.parent[state.pkey].description = obj.description.substring(0,300);
170+
}
171+
});
172+
}
173+
174+
clean(src,'paths');
164175
clean(src,'definitions');
165176
clean(src,'headers');
166177
clean(src,'responses');
@@ -171,20 +182,26 @@ function extract(obj, options) {
171182
clean(src.components,'schemas');
172183
clean(src,'components');
173184

185+
const al = new AList(src);
174186
if (options.removeExamples) {
175-
const al = new AList(src);
176187
for (let [value,parents] of al) {
177188
AList.deleteProperty(value, 'example');
178189
AList.deleteProperty(value, 'examples');
179190
}
180191
}
181192

182193
if (options.removeExtensions) {
183-
const al = new AList(src);
184194
for (let [value,parents] of al) {
185195
AList.deletePrefix(value, 'x-');
186196
}
187197
}
198+
199+
if (options.removeDocs) {
200+
for (let [value,parents] of al) {
201+
AList.deleteProperty(value, 'externalDocs');
202+
}
203+
}
204+
188205
return src;
189206
}
190207

openapi-extract.js

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ let argv = require('yargs')
2121
.string('path')
2222
.alias('p','path')
2323
.describe('path','the path to extract')
24+
.boolean('openai')
25+
.describe('openai','make definition OpenAI compliant')
2426
.array('operationid')
2527
.alias('o','operationid')
2628
.describe('operationid','the operationIds to extract')
@@ -35,6 +37,9 @@ let argv = require('yargs')
3537
.boolean('security')
3638
.alias('s','security')
3739
.describe('security','include security information')
40+
.boolean('removeDocs')
41+
.alias('d','removeDocs')
42+
.describe('removeDocs','remove all externalDocs properties')
3843
.boolean('removeExamples')
3944
.alias('r','removeExamples')
4045
.describe('removeExamples','remove all example/examples properties')

test/longdesc/input.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
openapi: 3.1.0
2+
info:
3+
title: API
4+
version: 1.0.0
5+
description: 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 This should get truncated...
6+
paths: {}

test/longdesc/options.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
info: true
2+
openai: true

test/longdesc/output.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
openapi: 3.1.0
2+
info:
3+
title: API
4+
version: 1.0.0
5+
description: "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
6+
paths: {}

0 commit comments

Comments
 (0)