apib2swagger 1.17.0 → 1.17.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apib2swagger",
3
- "version": "1.17.0",
3
+ "version": "1.17.1",
4
4
  "description": "Convert API Blueprint to Swagger.",
5
5
  "bin": "./bin/apib2swagger.js",
6
6
  "scripts": {
@@ -32,7 +32,7 @@ function convert(mson, options) {
32
32
  return { type: 'array', items: { 'anyOf': mson.content.map((m) => convert(m, options)) } };
33
33
  }
34
34
  case 'enum':
35
- return convertEnum(mson.content);
35
+ return convertEnum(mson.content, componentsPath);
36
36
  case 'object':
37
37
  break;
38
38
  case 'boolean':
@@ -98,19 +98,38 @@ function convert(mson, options) {
98
98
  return schema;
99
99
  }
100
100
 
101
- function convertEnum(contents) {
101
+ function convertEnum(contents, componentsPath) {
102
102
  if (!contents) return null
103
- var schema = { type: '', enum: [] };
103
+ const schemaList = []
104
104
  for (var i = 0; i < contents.length; i++) {
105
105
  var content = contents[i];
106
- if (!schema.type) {
107
- schema.type = content.element;
108
- } else if (schema.type != content.element) {
109
- // WARN!! mixed type enum
106
+ switch (content.element) {
107
+ case "boolean":
108
+ case "string":
109
+ case "number":
110
+ case "array":
111
+ case "enum": // TODO
112
+ case "object":
113
+ let schema = schemaList.find(v => v.type === content.element)
114
+ if (!schema) {
115
+ schema = { type: content.element, enum: [] }
116
+ schemaList.push(schema)
117
+ }
118
+ schema.enum.push(content.content)
119
+ break
120
+ default: // CustomType
121
+ schemaList.push({ '$ref': componentsPath + escapeJSONPointer(content.element) })
122
+ break
110
123
  }
111
- schema.enum.push(content.content);
112
124
  }
113
- return schema;
125
+ switch (schemaList.length) {
126
+ case 0:
127
+ return {}
128
+ case 1:
129
+ return schemaList[0]
130
+ default:
131
+ return { oneOf: schemaList}
132
+ }
114
133
  }
115
134
 
116
135
  module.exports = convertMsonToJsonSchema;
package/src/responses.js CHANGED
@@ -3,6 +3,7 @@ const escapeJSONPointer = require('./escape_json_pointer')
3
3
  const isEqual = require('lodash.isequal')
4
4
  const http = require('http')
5
5
  const toOpenApi = require('json-schema-to-openapi-schema')
6
+ const generateSchema = require('generate-schema')
6
7
 
7
8
  const parseResponseSchema = (schema, options) => {
8
9
  if (!schema) return
@@ -71,9 +72,20 @@ const setResponseSchema = (responses, response, schema, options) => {
71
72
  responses[response.name].schema = schema
72
73
  return responses
73
74
  }
75
+ if (schema.type === 'extend') { // Workaround for invalid JSON schema
76
+ if (schema.enum.length > 0) {
77
+ schema = generateSchema.json('', schema.enum[0])
78
+ } else {
79
+ schema.type = 'object'
80
+ }
81
+ }
74
82
  // Convert JSON Schema draft04 to OpenAPI 3.0.x (type: null -> nullable: true)
75
83
  // TODO: Skip this for OpenAPI 3.1.x or later.
76
- schema = toOpenApi(schema)
84
+ try {
85
+ schema = toOpenApi(schema)
86
+ } catch (e) {
87
+ return responses // TODO: Handle error
88
+ }
77
89
 
78
90
  // In openAPI 3 the schema lives under the content type
79
91
  const contentTypeHeader = response.headers.find((h) => h.name.toLowerCase() === 'content-type')
@@ -0,0 +1,21 @@
1
+ FORMAT: 1A
2
+ HOST: http://127.0.0.1/api/v1
3
+
4
+ # Group Extensions
5
+
6
+ ## Extensions [/extensions]
7
+
8
+ ### Get extensions [GET]
9
+
10
+ + Response 200 (application/json)
11
+ + Attributes (Extension)
12
+
13
+ ## Data Structures
14
+
15
+ ### Extension (enum)
16
+
17
+ + (Foo type)
18
+
19
+ ### Foo type (object, fixed-type)
20
+
21
+ + id (string) - the id
@@ -0,0 +1,69 @@
1
+ {
2
+ "openapi": "3.0.3",
3
+ "info": {
4
+ "title": "",
5
+ "version": "1.0.0",
6
+ "description": ""
7
+ },
8
+ "servers": [
9
+ {
10
+ "url": "http://127.0.0.1/api/v1"
11
+ }
12
+ ],
13
+ "paths": {
14
+ "/extensions": {
15
+ "get": {
16
+ "responses": {
17
+ "200": {
18
+ "description": "OK",
19
+ "headers": {},
20
+ "content": {
21
+ "application/json": {
22
+ "schema": {
23
+ "title": "",
24
+ "type": "object",
25
+ "properties": {
26
+ "id": {
27
+ "type": "string"
28
+ }
29
+ }
30
+ },
31
+ "example": {
32
+ "id": ""
33
+ }
34
+ }
35
+ }
36
+ }
37
+ },
38
+ "summary": "Get extensions",
39
+ "operationId": "Get extensions",
40
+ "description": "",
41
+ "tags": [
42
+ "Extensions"
43
+ ],
44
+ "parameters": []
45
+ }
46
+ }
47
+ },
48
+ "components": {
49
+ "schemas": {
50
+ "Extension": {
51
+ "$ref": "#/components/schemas/Foo type"
52
+ },
53
+ "Foo type": {
54
+ "type": "object",
55
+ "properties": {
56
+ "id": {
57
+ "type": "string",
58
+ "description": "the id"
59
+ }
60
+ }
61
+ }
62
+ }
63
+ },
64
+ "tags": [
65
+ {
66
+ "name": "Extensions"
67
+ }
68
+ ]
69
+ }
@@ -0,0 +1,63 @@
1
+ {
2
+ "openapi": "3.0.3",
3
+ "info": {
4
+ "title": "",
5
+ "version": "1.0.0",
6
+ "description": ""
7
+ },
8
+ "servers": [
9
+ {
10
+ "url": "http://127.0.0.1/api/v1"
11
+ }
12
+ ],
13
+ "paths": {
14
+ "/extensions": {
15
+ "get": {
16
+ "responses": {
17
+ "200": {
18
+ "description": "OK",
19
+ "headers": {},
20
+ "content": {
21
+ "application/json": {
22
+ "schema": {
23
+ "$ref": "#/components/schemas/Extension"
24
+ },
25
+ "example": {
26
+ "id": ""
27
+ }
28
+ }
29
+ }
30
+ }
31
+ },
32
+ "summary": "Get extensions",
33
+ "operationId": "Get extensions",
34
+ "description": "",
35
+ "tags": [
36
+ "Extensions"
37
+ ],
38
+ "parameters": []
39
+ }
40
+ }
41
+ },
42
+ "components": {
43
+ "schemas": {
44
+ "Extension": {
45
+ "$ref": "#/components/schemas/Foo type"
46
+ },
47
+ "Foo type": {
48
+ "type": "object",
49
+ "properties": {
50
+ "id": {
51
+ "type": "string",
52
+ "description": "the id"
53
+ }
54
+ }
55
+ }
56
+ }
57
+ },
58
+ "tags": [
59
+ {
60
+ "name": "Extensions"
61
+ }
62
+ ]
63
+ }
package/test/test.js CHANGED
@@ -47,6 +47,7 @@ var remote = 'https://raw.githubusercontent.com/apiaryio/api-blueprint/format-1A
47
47
  'Issue-#38.md',
48
48
  'Issue-#49.md',
49
49
  'Issue-#57.md',
50
+ 'Issue-#72.md',
50
51
  'apiblueprint_uber.md',
51
52
  'apiblueprint_valid_simple.md',
52
53
  'schema_without_body.md',
@@ -56,7 +57,8 @@ var remote = 'https://raw.githubusercontent.com/apiaryio/api-blueprint/format-1A
56
57
  'OpenAPI3_includes.md',
57
58
  'OpenAPI3_attributes.md',
58
59
  'OpenAPI3_Issue-#58.md',
59
- 'OpenAPI3_Issue-#66.md'
60
+ 'OpenAPI3_Issue-#66.md',
61
+ 'OpenAPI3_Issue-#73.md'
60
62
  ];
61
63
 
62
64
  var fetch = function (file) {