Skip to content

Commit

Permalink
Fix enum of MSON
Browse files Browse the repository at this point in the history
  • Loading branch information
kminami committed Oct 22, 2023
1 parent 1c0d727 commit d948b25
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apib2swagger",
"version": "1.17.0",
"version": "1.17.1",
"description": "Convert API Blueprint to Swagger.",
"bin": "./bin/apib2swagger.js",
"scripts": {
Expand Down
37 changes: 28 additions & 9 deletions src/mson_to_json_schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function convert(mson, options) {
return { type: 'array', items: { 'anyOf': mson.content.map((m) => convert(m, options)) } };
}
case 'enum':
return convertEnum(mson.content);
return convertEnum(mson.content, componentsPath);
case 'object':
break;
case 'boolean':
Expand Down Expand Up @@ -98,19 +98,38 @@ function convert(mson, options) {
return schema;
}

function convertEnum(contents) {
function convertEnum(contents, componentsPath) {
if (!contents) return null
var schema = { type: '', enum: [] };
const schemaList = []
for (var i = 0; i < contents.length; i++) {
var content = contents[i];
if (!schema.type) {
schema.type = content.element;
} else if (schema.type != content.element) {
// WARN!! mixed type enum
switch (content.element) {
case "boolean":
case "string":
case "number":
case "array":
case "enum": // TODO
case "object":
let schema = schemaList.find(v => v.type === content.element)
if (!schema) {
schema = { type: content.element, enum: [] }
schemaList.push(schema)
}
schema.enum.push(content.content)
break
default: // CustomType
schemaList.push({ '$ref': componentsPath + escapeJSONPointer(content.element) })
break
}
schema.enum.push(content.content);
}
return schema;
switch (schemaList.length) {
case 0:
return {}
case 1:
return schemaList[0]
default:
return { oneOf: schemaList}
}
}

module.exports = convertMsonToJsonSchema;
14 changes: 13 additions & 1 deletion src/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const escapeJSONPointer = require('./escape_json_pointer')
const isEqual = require('lodash.isequal')
const http = require('http')
const toOpenApi = require('json-schema-to-openapi-schema')
const generateSchema = require('generate-schema')

const parseResponseSchema = (schema, options) => {
if (!schema) return
Expand Down Expand Up @@ -71,9 +72,20 @@ const setResponseSchema = (responses, response, schema, options) => {
responses[response.name].schema = schema
return responses
}
if (schema.type === 'extend') { // Workaround for invalid JSON schema
if (schema.enum.length > 0) {
schema = generateSchema.json('', schema.enum[0])
} else {
schema.type = 'object'
}
}
// Convert JSON Schema draft04 to OpenAPI 3.0.x (type: null -> nullable: true)
// TODO: Skip this for OpenAPI 3.1.x or later.
schema = toOpenApi(schema)
try {
schema = toOpenApi(schema)
} catch (e) {
return responses // TODO: Handle error
}

// In openAPI 3 the schema lives under the content type
const contentTypeHeader = response.headers.find((h) => h.name.toLowerCase() === 'content-type')
Expand Down
21 changes: 21 additions & 0 deletions test/input/OpenAPI3_Issue-#73.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FORMAT: 1A
HOST: http://127.0.0.1/api/v1

# Group Extensions

## Extensions [/extensions]

### Get extensions [GET]

+ Response 200 (application/json)
+ Attributes (Extension)

## Data Structures

### Extension (enum)

+ (Foo type)

### Foo type (object, fixed-type)

+ id (string) - the id
69 changes: 69 additions & 0 deletions test/output/OpenAPI3_Issue-#73.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"openapi": "3.0.3",
"info": {
"title": "",
"version": "1.0.0",
"description": ""
},
"servers": [
{
"url": "http://127.0.0.1/api/v1"
}
],
"paths": {
"/extensions": {
"get": {
"responses": {
"200": {
"description": "OK",
"headers": {},
"content": {
"application/json": {
"schema": {
"title": "",
"type": "object",
"properties": {
"id": {
"type": "string"
}
}
},
"example": {
"id": ""
}
}
}
}
},
"summary": "Get extensions",
"operationId": "Get extensions",
"description": "",
"tags": [
"Extensions"
],
"parameters": []
}
}
},
"components": {
"schemas": {
"Extension": {
"$ref": "#/components/schemas/Foo type"
},
"Foo type": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "the id"
}
}
}
}
},
"tags": [
{
"name": "Extensions"
}
]
}
63 changes: 63 additions & 0 deletions test/output/OpenAPI3_Issue-#73.ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"openapi": "3.0.3",
"info": {
"title": "",
"version": "1.0.0",
"description": ""
},
"servers": [
{
"url": "http://127.0.0.1/api/v1"
}
],
"paths": {
"/extensions": {
"get": {
"responses": {
"200": {
"description": "OK",
"headers": {},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Extension"
},
"example": {
"id": ""
}
}
}
}
},
"summary": "Get extensions",
"operationId": "Get extensions",
"description": "",
"tags": [
"Extensions"
],
"parameters": []
}
}
},
"components": {
"schemas": {
"Extension": {
"$ref": "#/components/schemas/Foo type"
},
"Foo type": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "the id"
}
}
}
}
},
"tags": [
{
"name": "Extensions"
}
]
}
4 changes: 3 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var remote = 'https://raw.githubusercontent.com/apiaryio/api-blueprint/format-1A
'Issue-#38.md',
'Issue-#49.md',
'Issue-#57.md',
'Issue-#72.md',
'apiblueprint_uber.md',
'apiblueprint_valid_simple.md',
'schema_without_body.md',
Expand All @@ -56,7 +57,8 @@ var remote = 'https://raw.githubusercontent.com/apiaryio/api-blueprint/format-1A
'OpenAPI3_includes.md',
'OpenAPI3_attributes.md',
'OpenAPI3_Issue-#58.md',
'OpenAPI3_Issue-#66.md'
'OpenAPI3_Issue-#66.md',
'OpenAPI3_Issue-#73.md'
];

var fetch = function (file) {
Expand Down

0 comments on commit d948b25

Please sign in to comment.