apib2swagger 1.12.0 → 1.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/codeql-analysis.yml +71 -0
- package/.github/workflows/docker-publish.yml +1 -1
- package/.github/workflows/nodejs.yml +1 -1
- package/.travis.yml +2 -1
- package/Dockerfile +1 -1
- package/LICENSE +0 -0
- package/README.md +30 -9
- package/bin/apib2swagger.js +44 -18
- package/index.js +82 -366
- package/package.json +7 -4
- package/src/escape_json_pointer.js +0 -0
- package/src/mson_to_json_schema.js +12 -10
- package/src/parameters.js +111 -0
- package/src/requests.js +357 -0
- package/src/responses.js +186 -0
- package/src/util.js +125 -0
- package/test/input/Attributes.md +0 -0
- package/test/input/Issue-#15.md +0 -0
- package/test/input/Issue-#22.md +0 -0
- package/test/input/Issue-#23.md +0 -0
- package/test/input/Issue-#26.md +0 -0
- package/test/input/Issue-#29.md +0 -0
- package/test/input/Issue-#33.md +0 -0
- package/test/input/Issue-#35.md +0 -0
- package/test/input/Issue-#36.md +0 -0
- package/test/input/Issue-#38.md +0 -0
- package/test/input/Issue-#49.md +0 -0
- package/test/input/Issue-#57.md +14 -0
- package/test/input/OpenAPI3_Issue-#58.md +31 -0
- package/test/input/OpenAPI3_attributes.md +36 -0
- package/test/input/OpenAPI3_headers.md +74 -0
- package/test/input/OpenAPI3_includes.md +22 -0
- package/test/input/OpenAPI3_requests.md +162 -0
- package/test/input/OpenAPI3_responses.md +177 -0
- package/test/input/Schema.md +0 -0
- package/test/input/apiblueprint_uber.md +0 -0
- package/test/input/apiblueprint_valid_simple.md +0 -0
- package/test/input/includes/resource-request-body-2.json +3 -0
- package/test/input/includes/resource-request-body.json +3 -0
- package/test/input/includes/resource-request-schema.json +9 -0
- package/test/input/includes/resource-response-body-2.json +3 -0
- package/test/input/includes/resource-response-body.json +3 -0
- package/test/input/includes/resource-response-schema.json +9 -0
- package/test/input/schema_without_body.md +17 -0
- package/test/meta_schema.json +0 -0
- package/test/openAPI_30_schema.json +1662 -0
- package/test/output/01. Simplest API.json +0 -0
- package/test/output/01. Simplest API.ref.json +0 -0
- package/test/output/02. Resource and Actions.json +0 -0
- package/test/output/02. Resource and Actions.ref.json +0 -0
- package/test/output/03. Named Resource and Actions.json +0 -0
- package/test/output/03. Named Resource and Actions.ref.json +0 -0
- package/test/output/04. Grouping Resources.json +0 -0
- package/test/output/04. Grouping Resources.ref.json +0 -0
- package/test/output/05. Responses.json +0 -0
- package/test/output/05. Responses.ref.json +0 -0
- package/test/output/06. Requests.json +0 -0
- package/test/output/06. Requests.ref.json +0 -0
- package/test/output/07. Parameters.json +0 -0
- package/test/output/07. Parameters.ref.json +0 -0
- package/test/output/08. Attributes.json +0 -0
- package/test/output/08. Attributes.ref.json +0 -0
- package/test/output/09. Advanced Attributes.json +0 -0
- package/test/output/09. Advanced Attributes.ref.json +0 -0
- package/test/output/10. Data Structures.json +0 -0
- package/test/output/10. Data Structures.ref.json +0 -0
- package/test/output/11. Resource Model.json +0 -0
- package/test/output/11. Resource Model.ref.json +0 -0
- package/test/output/12. Advanced Action.json +0 -0
- package/test/output/12. Advanced Action.ref.json +0 -0
- package/test/output/13. Named Endpoints.json +0 -0
- package/test/output/13. Named Endpoints.ref.json +0 -0
- package/test/output/Attributes.json +0 -0
- package/test/output/Attributes.ref.json +0 -0
- package/test/output/Gist Fox API + Auth.json +0 -0
- package/test/output/Gist Fox API + Auth.ref.json +0 -0
- package/test/output/Issue-#15.json +0 -0
- package/test/output/Issue-#15.ref.json +0 -0
- package/test/output/Issue-#22.json +0 -0
- package/test/output/Issue-#22.ref.json +0 -0
- package/test/output/Issue-#23.json +0 -0
- package/test/output/Issue-#23.ref.json +0 -0
- package/test/output/Issue-#26.json +0 -0
- package/test/output/Issue-#26.ref.json +0 -0
- package/test/output/Issue-#29.json +0 -0
- package/test/output/Issue-#29.ref.json +0 -0
- package/test/output/Issue-#33.json +0 -0
- package/test/output/Issue-#33.ref.json +0 -0
- package/test/output/Issue-#35.json +0 -0
- package/test/output/Issue-#35.ref.json +0 -0
- package/test/output/Issue-#36.json +0 -0
- package/test/output/Issue-#36.ref.json +0 -0
- package/test/output/Issue-#38.bearer.json +0 -0
- package/test/output/Issue-#38.json +0 -0
- package/test/output/Issue-#38.ref.json +0 -0
- package/test/output/Issue-#49.json +0 -0
- package/test/output/Issue-#49.ref.json +0 -0
- package/test/output/Issue-#57.json +44 -0
- package/test/output/Issue-#57.ref.json +44 -0
- package/test/output/OpenAPI3_Issue-#58.json +202 -0
- package/test/output/OpenAPI3_Issue-#58.ref.json +163 -0
- package/test/output/OpenAPI3_attributes.json +199 -0
- package/test/output/OpenAPI3_attributes.ref.json +173 -0
- package/test/output/OpenAPI3_headers.json +169 -0
- package/test/output/OpenAPI3_headers.ref.json +169 -0
- package/test/output/OpenAPI3_includes.json +66 -0
- package/test/output/OpenAPI3_includes.ref.json +66 -0
- package/test/output/OpenAPI3_requests.json +326 -0
- package/test/output/OpenAPI3_requests.ref.json +321 -0
- package/test/output/OpenAPI3_responses.json +255 -0
- package/test/output/OpenAPI3_responses.ref.json +245 -0
- package/test/output/Schema.json +0 -0
- package/test/output/Schema.ref.json +0 -0
- package/test/output/apiblueprint_uber.json +0 -0
- package/test/output/apiblueprint_uber.ref.json +0 -0
- package/test/output/apiblueprint_valid_simple.json +0 -0
- package/test/output/apiblueprint_valid_simple.ref.json +0 -0
- package/test/output/schema_without_body.json +52 -0
- package/test/output/schema_without_body.ref.json +52 -0
- package/test/swagger_20_schema.json +0 -0
- package/test/test.js +26 -9
- package/test/util.test.js +151 -0
- package/.npmignore +0 -35
- package/package-lock.json +0 -2021
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# For most projects, this workflow file will not need changing; you simply need
|
|
2
|
+
# to commit it to your repository.
|
|
3
|
+
#
|
|
4
|
+
# You may wish to alter this file to override the set of languages analyzed,
|
|
5
|
+
# or to provide custom queries or build logic.
|
|
6
|
+
name: "CodeQL"
|
|
7
|
+
|
|
8
|
+
on:
|
|
9
|
+
push:
|
|
10
|
+
branches: [master]
|
|
11
|
+
pull_request:
|
|
12
|
+
# The branches below must be a subset of the branches above
|
|
13
|
+
branches: [master]
|
|
14
|
+
schedule:
|
|
15
|
+
- cron: '0 13 * * 0'
|
|
16
|
+
|
|
17
|
+
jobs:
|
|
18
|
+
analyze:
|
|
19
|
+
name: Analyze
|
|
20
|
+
runs-on: ubuntu-latest
|
|
21
|
+
|
|
22
|
+
strategy:
|
|
23
|
+
fail-fast: false
|
|
24
|
+
matrix:
|
|
25
|
+
# Override automatic language detection by changing the below list
|
|
26
|
+
# Supported options are ['csharp', 'cpp', 'go', 'java', 'javascript', 'python']
|
|
27
|
+
language: ['javascript']
|
|
28
|
+
# Learn more...
|
|
29
|
+
# https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#overriding-automatic-language-detection
|
|
30
|
+
|
|
31
|
+
steps:
|
|
32
|
+
- name: Checkout repository
|
|
33
|
+
uses: actions/checkout@v2
|
|
34
|
+
with:
|
|
35
|
+
# We must fetch at least the immediate parents so that if this is
|
|
36
|
+
# a pull request then we can checkout the head.
|
|
37
|
+
fetch-depth: 2
|
|
38
|
+
|
|
39
|
+
# If this run was triggered by a pull request event, then checkout
|
|
40
|
+
# the head of the pull request instead of the merge commit.
|
|
41
|
+
- run: git checkout HEAD^2
|
|
42
|
+
if: ${{ github.event_name == 'pull_request' }}
|
|
43
|
+
|
|
44
|
+
# Initializes the CodeQL tools for scanning.
|
|
45
|
+
- name: Initialize CodeQL
|
|
46
|
+
uses: github/codeql-action/init@v1
|
|
47
|
+
with:
|
|
48
|
+
languages: ${{ matrix.language }}
|
|
49
|
+
# If you wish to specify custom queries, you can do so here or in a config file.
|
|
50
|
+
# By default, queries listed here will override any specified in a config file.
|
|
51
|
+
# Prefix the list here with "+" to use these queries and those in the config file.
|
|
52
|
+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
|
53
|
+
|
|
54
|
+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
|
55
|
+
# If this step fails, then you should remove it and run the build manually (see below)
|
|
56
|
+
- name: Autobuild
|
|
57
|
+
uses: github/codeql-action/autobuild@v1
|
|
58
|
+
|
|
59
|
+
# ℹ️ Command-line programs to run using the OS shell.
|
|
60
|
+
# 📚 https://git.io/JvXDl
|
|
61
|
+
|
|
62
|
+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
|
63
|
+
# and modify them (or add more) to build your code if your project
|
|
64
|
+
# uses a compiled language
|
|
65
|
+
|
|
66
|
+
#- run: |
|
|
67
|
+
# make bootstrap
|
|
68
|
+
# make release
|
|
69
|
+
|
|
70
|
+
- name: Perform CodeQL Analysis
|
|
71
|
+
uses: github/codeql-action/analyze@v1
|
package/.travis.yml
CHANGED
package/Dockerfile
CHANGED
package/LICENSE
CHANGED
|
File without changes
|
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# apib2swagger
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
4
|
[](https://coveralls.io/github/kminami/apib2swagger?branch=master)
|
|
5
5
|
[](https://badge.fury.io/js/apib2swagger)
|
|
6
6
|
|
|
7
|
-
Convert [API Blueprint]
|
|
7
|
+
Convert [API Blueprint](https://apiblueprint.org/) to [Swagger 2.0](http://swagger.io/) or [OpenAPI 3.0](https://github.com/OAI/OpenAPI-Specification).
|
|
8
8
|
|
|
9
9
|
Supported versions:
|
|
10
10
|
- API Blueprint 1A9
|
|
@@ -13,7 +13,8 @@ Supported versions:
|
|
|
13
13
|
- VERSION -> .info.version
|
|
14
14
|
- [Include directive](https://github.com/danielgtaylor/aglio#including-files)
|
|
15
15
|
- Swagger 2.0
|
|
16
|
-
-
|
|
16
|
+
- OpenAPI 3.0.3
|
|
17
|
+
- Node.js 12.x, 14.x, 16.x or higher
|
|
17
18
|
|
|
18
19
|
## Install
|
|
19
20
|
|
|
@@ -30,6 +31,9 @@ $ apib2swagger -i api.md -o swagger.json
|
|
|
30
31
|
$ apib2swagger -i api.md --yaml -o swagger.yaml
|
|
31
32
|
$ apib2swagger -i api.md --prefer-reference
|
|
32
33
|
$ apib2swagger -i api.md --bearer-apikey
|
|
34
|
+
$ apib2swagger -i api.md --open-api-3
|
|
35
|
+
$ apib2swagger -i api.md --info-title "My API Document Title"
|
|
36
|
+
$ apib2swagger -i api.md --prefer-file-ref
|
|
33
37
|
```
|
|
34
38
|
|
|
35
39
|
Without -i option it reads from STDIN, without -o option writes to STDOUT.
|
|
@@ -43,13 +47,33 @@ SwaggerUI will be automatically downloaded to current dir.
|
|
|
43
47
|
```shell
|
|
44
48
|
$ apib2swagger -i api.md -s
|
|
45
49
|
$ apib2swagger -i api.md -s -p 3000
|
|
50
|
+
|
|
51
|
+
# When using file references and running the SwaggerUI server, you can specify the source
|
|
52
|
+
# directory with the -sd flag. It will check the input directory and execution directory
|
|
53
|
+
# if -sd is not given.
|
|
54
|
+
$ apib2swagger -i api.md -s --prefer-file-ref -sd ~/project/src/
|
|
46
55
|
```
|
|
47
56
|
|
|
48
57
|
Use as a library.
|
|
49
58
|
```javascript
|
|
50
59
|
var apib2swagger = require('apib2swagger'),
|
|
51
60
|
apib = '...',
|
|
52
|
-
options = {
|
|
61
|
+
options = {
|
|
62
|
+
preferReference: true,
|
|
63
|
+
|
|
64
|
+
// optional (Swagger 2.0 only).
|
|
65
|
+
bearerAsApikey: false,
|
|
66
|
+
|
|
67
|
+
// optional. swagger 2.0 is used by default.
|
|
68
|
+
openApi3: true,
|
|
69
|
+
|
|
70
|
+
// optional. title will be grabbed from blueprint if not specified.
|
|
71
|
+
infoTitle: 'My API Document Title',
|
|
72
|
+
|
|
73
|
+
// optional (Open API 3 only).
|
|
74
|
+
// will set a $ref to the given file path instead of including the file contents.
|
|
75
|
+
preferFileRef: true
|
|
76
|
+
};
|
|
53
77
|
|
|
54
78
|
apib2swagger.convert(apib, options, function (error, result) {
|
|
55
79
|
if (!error) console.log(result.swagger);
|
|
@@ -67,14 +91,11 @@ cat api.md | npx apib2swagger > swagger.json
|
|
|
67
91
|
You can also run apib2swagger inside a docker container.
|
|
68
92
|
|
|
69
93
|
```bash
|
|
70
|
-
$ docker run -it --rm -v $(pwd):/docs kminami/apib2swagger -i /docs/api.md -o /docs/swagger.json
|
|
94
|
+
$ docker run -it --rm -v $(pwd):/docs ghcr.io/kminami/apib2swagger -i /docs/api.md -o /docs/swagger.json
|
|
71
95
|
```
|
|
72
96
|
|
|
73
97
|
## License
|
|
74
98
|
|
|
75
|
-
Copyright (c)
|
|
99
|
+
Copyright (c) 2021 Keisuke Minami
|
|
76
100
|
|
|
77
101
|
MIT
|
|
78
|
-
|
|
79
|
-
[API Blueprint]: https://apiblueprint.org/ "API Blueprint"
|
|
80
|
-
[Swagger]: http://swagger.io/ "Swagger"
|
package/bin/apib2swagger.js
CHANGED
|
@@ -8,7 +8,8 @@ var fs = require('fs'),
|
|
|
8
8
|
nopt = require('nopt'),
|
|
9
9
|
yaml = require('js-yaml'),
|
|
10
10
|
apibIncludeDirective = require('apib-include-directive'),
|
|
11
|
-
apib2swagger = require('../index.js')
|
|
11
|
+
apib2swagger = require('../index.js'),
|
|
12
|
+
util = require('../src/util');
|
|
12
13
|
|
|
13
14
|
var options = nopt({
|
|
14
15
|
'input': String,
|
|
@@ -19,14 +20,19 @@ var options = nopt({
|
|
|
19
20
|
'yaml': Boolean,
|
|
20
21
|
'prefer-reference': Boolean,
|
|
21
22
|
'bearer-apikey': Boolean,
|
|
22
|
-
'help': Boolean
|
|
23
|
+
'help': Boolean,
|
|
24
|
+
'open-api-3': Boolean,
|
|
25
|
+
'info-title': String,
|
|
26
|
+
'prefer-file-ref': Boolean,
|
|
27
|
+
'source-dir': String
|
|
23
28
|
}, {
|
|
24
29
|
'i': ['--input'],
|
|
25
30
|
'o': ['--output'],
|
|
26
31
|
's': ['--server'],
|
|
27
32
|
'p': ['--port'],
|
|
28
33
|
'y': ['--yaml'],
|
|
29
|
-
'h': ['--help']
|
|
34
|
+
'h': ['--help'],
|
|
35
|
+
'sd': ['--source-dir']
|
|
30
36
|
});
|
|
31
37
|
|
|
32
38
|
if (options.help) {
|
|
@@ -47,8 +53,12 @@ if (options.help) {
|
|
|
47
53
|
console.log(" -s --server Run http server with SwaggerUI.");
|
|
48
54
|
console.log(" -p --port <port> Use port for the http server.");
|
|
49
55
|
console.log(" -y --yaml Output YAML");
|
|
56
|
+
console.log(" -sd --source-dir Specify a source directory to serve file references from.");
|
|
50
57
|
console.log(" --prefer-reference Refer to definitions as possible");
|
|
51
58
|
console.log(" --bearer-apikey Convert Bearer headers to apiKey security schema instead of oauth2")
|
|
59
|
+
console.log(" --open-api-3 Output as OpenAPI 3.0 instead of the default Swagger 2.0")
|
|
60
|
+
console.log(" --info-title Optional info.title")
|
|
61
|
+
console.log(" --prefer-file-ref Create refs to file paths instead of importing the content.")
|
|
52
62
|
process.exit();
|
|
53
63
|
}
|
|
54
64
|
|
|
@@ -63,7 +73,11 @@ on('data', (chunk) => {
|
|
|
63
73
|
apibData += chunk;
|
|
64
74
|
}).on('end', () => {
|
|
65
75
|
try {
|
|
66
|
-
|
|
76
|
+
apibData = util.normalizeIncludes(apibData)
|
|
77
|
+
|
|
78
|
+
if (!options['prefer-file-ref']){
|
|
79
|
+
apibData = apibIncludeDirective.includeDirective(includePath, apibData);
|
|
80
|
+
}
|
|
67
81
|
} catch(e) {
|
|
68
82
|
console.log(e.toString());
|
|
69
83
|
return;
|
|
@@ -88,7 +102,13 @@ function processBlueprint(blueprint, opts) {
|
|
|
88
102
|
return;
|
|
89
103
|
}
|
|
90
104
|
|
|
91
|
-
var options = {
|
|
105
|
+
var options = {
|
|
106
|
+
preferReference: opts['prefer-reference'],
|
|
107
|
+
bearerAsApikey: opts['bearer-apikey'],
|
|
108
|
+
openApi3: opts['open-api-3'],
|
|
109
|
+
infoTitle: opts['info-title'],
|
|
110
|
+
preferFileRef: opts['prefer-file-ref']
|
|
111
|
+
};
|
|
92
112
|
apib2swagger.convert(blueprint, options, function(error, result) {
|
|
93
113
|
if (error) {
|
|
94
114
|
console.log(error);
|
|
@@ -106,7 +126,7 @@ function processBlueprint(blueprint, opts) {
|
|
|
106
126
|
return runServer(swagger);
|
|
107
127
|
}
|
|
108
128
|
if (opts.yaml) {
|
|
109
|
-
var data = yaml.
|
|
129
|
+
var data = yaml.dump(swagger);
|
|
110
130
|
if (output !== '-') {
|
|
111
131
|
fs.writeFileSync(output, data);
|
|
112
132
|
} else {
|
|
@@ -123,28 +143,34 @@ function processBlueprint(blueprint, opts) {
|
|
|
123
143
|
});
|
|
124
144
|
}
|
|
125
145
|
|
|
146
|
+
function serveFile(response, path){
|
|
147
|
+
response.statusCode = 200;
|
|
148
|
+
response.write(fs.readFileSync(path));
|
|
149
|
+
response.end();
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
126
153
|
function runServer(swagger) {
|
|
127
154
|
var server = http.createServer(function(request, response) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
if (path === '/swagger.json') {
|
|
155
|
+
var filePath = request.url.split('?')[0];
|
|
156
|
+
if (filePath === '/swagger.json') {
|
|
131
157
|
response.statusCode = 200;
|
|
132
158
|
response.write(JSON.stringify(swagger));
|
|
133
159
|
response.end();
|
|
134
|
-
} else if (
|
|
160
|
+
} else if (filePath === '/') {
|
|
135
161
|
response.statusCode = 302;
|
|
136
162
|
response.setHeader('Location', '/index.html?url=/swagger.json');
|
|
137
163
|
response.end();
|
|
138
164
|
} else {
|
|
139
|
-
|
|
140
|
-
if (
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
response.statusCode =
|
|
146
|
-
response.write(fs.readFileSync(file));
|
|
165
|
+
const swaggerFile = 'swagger-ui-master/dist' + filePath;
|
|
166
|
+
if (fs.existsSync(swaggerFile)) return serveFile(response, swaggerFile)
|
|
167
|
+
|
|
168
|
+
const file = util.getAbsolutePath(options, filePath)
|
|
169
|
+
if (file) return serveFile(response, file)
|
|
170
|
+
|
|
171
|
+
response.statusCode = 404;
|
|
147
172
|
response.end();
|
|
173
|
+
return;
|
|
148
174
|
}
|
|
149
175
|
});
|
|
150
176
|
console.log('Serving http://0.0.0.0:' + port + '/ ...');
|