apib2swagger 1.12.0 → 1.13.0

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.
Files changed (118) hide show
  1. package/.github/workflows/codeql-analysis.yml +71 -0
  2. package/.github/workflows/docker-publish.yml +1 -1
  3. package/.github/workflows/nodejs.yml +1 -1
  4. package/.travis.yml +2 -1
  5. package/Dockerfile +1 -1
  6. package/LICENSE +0 -0
  7. package/README.md +29 -3
  8. package/bin/apib2swagger.js +43 -17
  9. package/index.js +81 -366
  10. package/package.json +5 -3
  11. package/src/escape_json_pointer.js +0 -0
  12. package/src/mson_to_json_schema.js +12 -10
  13. package/src/parameters.js +111 -0
  14. package/src/requests.js +344 -0
  15. package/src/responses.js +181 -0
  16. package/src/util.js +127 -0
  17. package/test/input/Attributes.md +0 -0
  18. package/test/input/Issue-#15.md +0 -0
  19. package/test/input/Issue-#22.md +0 -0
  20. package/test/input/Issue-#23.md +0 -0
  21. package/test/input/Issue-#26.md +0 -0
  22. package/test/input/Issue-#29.md +0 -0
  23. package/test/input/Issue-#33.md +0 -0
  24. package/test/input/Issue-#35.md +0 -0
  25. package/test/input/Issue-#36.md +0 -0
  26. package/test/input/Issue-#38.md +0 -0
  27. package/test/input/Issue-#49.md +0 -0
  28. package/test/input/OpenAPI3_attributes.md +36 -0
  29. package/test/input/OpenAPI3_headers.md +74 -0
  30. package/test/input/OpenAPI3_includes.md +22 -0
  31. package/test/input/OpenAPI3_requests.md +162 -0
  32. package/test/input/OpenAPI3_responses.md +177 -0
  33. package/test/input/Schema.md +0 -0
  34. package/test/input/apiblueprint_uber.md +0 -0
  35. package/test/input/apiblueprint_valid_simple.md +0 -0
  36. package/test/input/includes/resource-request-body-2.json +3 -0
  37. package/test/input/includes/resource-request-body.json +3 -0
  38. package/test/input/includes/resource-request-schema.json +9 -0
  39. package/test/input/includes/resource-response-body-2.json +3 -0
  40. package/test/input/includes/resource-response-body.json +3 -0
  41. package/test/input/includes/resource-response-schema.json +9 -0
  42. package/test/input/schema_without_body.md +17 -0
  43. package/test/meta_schema.json +0 -0
  44. package/test/openAPI_30_schema.json +1662 -0
  45. package/test/output/01. Simplest API.json +0 -0
  46. package/test/output/01. Simplest API.ref.json +0 -0
  47. package/test/output/02. Resource and Actions.json +0 -0
  48. package/test/output/02. Resource and Actions.ref.json +0 -0
  49. package/test/output/03. Named Resource and Actions.json +0 -0
  50. package/test/output/03. Named Resource and Actions.ref.json +0 -0
  51. package/test/output/04. Grouping Resources.json +0 -0
  52. package/test/output/04. Grouping Resources.ref.json +0 -0
  53. package/test/output/05. Responses.json +0 -0
  54. package/test/output/05. Responses.ref.json +0 -0
  55. package/test/output/06. Requests.json +0 -0
  56. package/test/output/06. Requests.ref.json +0 -0
  57. package/test/output/07. Parameters.json +0 -0
  58. package/test/output/07. Parameters.ref.json +0 -0
  59. package/test/output/08. Attributes.json +0 -0
  60. package/test/output/08. Attributes.ref.json +0 -0
  61. package/test/output/09. Advanced Attributes.json +0 -0
  62. package/test/output/09. Advanced Attributes.ref.json +0 -0
  63. package/test/output/10. Data Structures.json +0 -0
  64. package/test/output/10. Data Structures.ref.json +0 -0
  65. package/test/output/11. Resource Model.json +0 -0
  66. package/test/output/11. Resource Model.ref.json +0 -0
  67. package/test/output/12. Advanced Action.json +0 -0
  68. package/test/output/12. Advanced Action.ref.json +0 -0
  69. package/test/output/13. Named Endpoints.json +0 -0
  70. package/test/output/13. Named Endpoints.ref.json +0 -0
  71. package/test/output/Attributes.json +0 -0
  72. package/test/output/Attributes.ref.json +0 -0
  73. package/test/output/Gist Fox API + Auth.json +0 -0
  74. package/test/output/Gist Fox API + Auth.ref.json +0 -0
  75. package/test/output/Issue-#15.json +0 -0
  76. package/test/output/Issue-#15.ref.json +0 -0
  77. package/test/output/Issue-#22.json +0 -0
  78. package/test/output/Issue-#22.ref.json +0 -0
  79. package/test/output/Issue-#23.json +0 -0
  80. package/test/output/Issue-#23.ref.json +0 -0
  81. package/test/output/Issue-#26.json +0 -0
  82. package/test/output/Issue-#26.ref.json +0 -0
  83. package/test/output/Issue-#29.json +0 -0
  84. package/test/output/Issue-#29.ref.json +0 -0
  85. package/test/output/Issue-#33.json +0 -0
  86. package/test/output/Issue-#33.ref.json +0 -0
  87. package/test/output/Issue-#35.json +0 -0
  88. package/test/output/Issue-#35.ref.json +0 -0
  89. package/test/output/Issue-#36.json +0 -0
  90. package/test/output/Issue-#36.ref.json +0 -0
  91. package/test/output/Issue-#38.bearer.json +0 -0
  92. package/test/output/Issue-#38.json +0 -0
  93. package/test/output/Issue-#38.ref.json +0 -0
  94. package/test/output/Issue-#49.json +0 -0
  95. package/test/output/Issue-#49.ref.json +0 -0
  96. package/test/output/OpenAPI3_attributes.json +199 -0
  97. package/test/output/OpenAPI3_attributes.ref.json +173 -0
  98. package/test/output/OpenAPI3_headers.json +169 -0
  99. package/test/output/OpenAPI3_headers.ref.json +169 -0
  100. package/test/output/OpenAPI3_includes.json +66 -0
  101. package/test/output/OpenAPI3_includes.ref.json +66 -0
  102. package/test/output/OpenAPI3_requests.json +326 -0
  103. package/test/output/OpenAPI3_requests.ref.json +321 -0
  104. package/test/output/OpenAPI3_responses.json +255 -0
  105. package/test/output/OpenAPI3_responses.ref.json +245 -0
  106. package/test/output/Schema.json +0 -0
  107. package/test/output/Schema.ref.json +0 -0
  108. package/test/output/apiblueprint_uber.json +0 -0
  109. package/test/output/apiblueprint_uber.ref.json +0 -0
  110. package/test/output/apiblueprint_valid_simple.json +0 -0
  111. package/test/output/apiblueprint_valid_simple.ref.json +0 -0
  112. package/test/output/schema_without_body.json +52 -0
  113. package/test/output/schema_without_body.ref.json +52 -0
  114. package/test/swagger_20_schema.json +0 -0
  115. package/test/test.js +24 -9
  116. package/test/util.test.js +151 -0
  117. package/.npmignore +0 -35
  118. 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
@@ -67,4 +67,4 @@ jobs:
67
67
  echo IMAGE_ID=$IMAGE_ID
68
68
  echo VERSION=$VERSION
69
69
  docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
70
- docker push $IMAGE_ID:$VERSION
70
+ docker push $IMAGE_ID:$VERSION
@@ -9,7 +9,7 @@ jobs:
9
9
 
10
10
  strategy:
11
11
  matrix:
12
- node-version: [10.x, 12.x]
12
+ node-version: [12.x, 14.x, 16.x]
13
13
 
14
14
  steps:
15
15
  - uses: actions/checkout@v1
package/.travis.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  language: node_js
2
2
  node_js:
3
+ - "16"
4
+ - "14"
3
5
  - "12"
4
- - "10"
5
6
  after_success: npm run coverage
package/Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM node:10-alpine
1
+ FROM node:16-alpine
2
2
 
3
3
  RUN npm install -g apib2swagger
4
4
 
package/LICENSE CHANGED
File without changes
package/README.md CHANGED
@@ -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
- - Node.js 10.x, 12.x or higher
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 = { preferReference: true, bearerAsApikey: false };
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);
@@ -70,9 +94,11 @@ You can also run apib2swagger inside a docker container.
70
94
  $ docker run -it --rm -v $(pwd):/docs kminami/apib2swagger -i /docs/api.md -o /docs/swagger.json
71
95
  ```
72
96
 
97
+ You can use container images in GitHub Container Registory like `ghcr.io/kminami/apib2swagger` or `ghcr.io/kminami/apib2swagger:1.12.0` instead of Docker Hub.
98
+
73
99
  ## License
74
100
 
75
- Copyright (c) 2015 Keisuke Minami
101
+ Copyright (c) 2021 Keisuke Minami
76
102
 
77
103
  MIT
78
104
 
@@ -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
- apibData = apibIncludeDirective.includeDirective(includePath, apibData);
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 = { preferReference: opts['prefer-reference'], bearerAsApikey: opts['bearer-apikey'] };
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);
@@ -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
- console.log(request.url);
129
- var path = request.url.split('?')[0];
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 (path === '/') {
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
- var file = 'swagger-ui-master/dist' + path;
140
- if (!fs.existsSync(file)) {
141
- response.statusCode = 404;
142
- response.end();
143
- return;
144
- }
145
- response.statusCode = 200;
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 + '/ ...');