Skip to content

Commit

Permalink
Fix #66
Browse files Browse the repository at this point in the history
  • Loading branch information
kminami committed May 16, 2022
1 parent 3c0ace1 commit e370201
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 8 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.14.2",
"version": "1.14.3",
"description": "Convert API Blueprint to Swagger.",
"bin": "./bin/apib2swagger.js",
"scripts": {
Expand Down
17 changes: 11 additions & 6 deletions src/mson_to_json_schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ function convert(mson, options) {
schema.required = [];
schema.properties = {};
for (var j = 0; mson.content && j < mson.content.length; j++) {
var member = mson.content[j];
const member = mson.content[j];
if (member.element !== "member") continue;
schema.properties[member.content.key.content] = convert(member.content.value, options);
var propertySchema = convert(member.content.value, options);
if (member.meta && member.meta.description) {
schema.properties[member.content.key.content].description = member.meta.description;
propertySchema.description = member.meta.description;
}
var fixedType = false;
if (member.attributes && member.attributes.typeAttributes) {
Expand All @@ -68,14 +68,19 @@ function convert(mson, options) {
schema.required.push(member.content.key.content);
break;
case 'nullable':
schema.properties[member.content.key.content].type = [schema.properties[member.content.key.content].type, 'null']
if (propertySchema.$ref) {
propertySchema = {oneOf: [propertySchema, {type: 'null'}]};
} else {
propertySchema.type = [propertySchema.type, 'null']
}
break;
}
});
}
if (schema.properties[member.content.key.content].type === 'array' && !fixedType) {
schema.properties[member.content.key.content].items = {}; // reset item schema
if (propertySchema.type === 'array' && !fixedType) {
propertySchema.items = {}; // reset item schema
}
schema.properties[member.content.key.content] = propertySchema;
}

// According to schema definition, required is a stringArray, which must be non-empty
Expand Down
6 changes: 6 additions & 0 deletions test/input/OpenAPI3_Issue-#66.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Data Structures
## ImagesType (object)
+ `name` (string)

## BodyType (object)
+ images (required, nullable, ImagesType)
40 changes: 40 additions & 0 deletions test/output/OpenAPI3_Issue-#66.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"openapi": "3.0.3",
"info": {
"title": "",
"version": "1.0.0",
"description": ""
},
"paths": {},
"components": {
"schemas": {
"ImagesType": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
"BodyType": {
"type": "object",
"required": [
"images"
],
"properties": {
"images": {
"oneOf": [
{
"$ref": "#/components/schemas/ImagesType"
},
{
"nullable": true
}
]
}
}
}
}
},
"tags": []
}
40 changes: 40 additions & 0 deletions test/output/OpenAPI3_Issue-#66.ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"openapi": "3.0.3",
"info": {
"title": "",
"version": "1.0.0",
"description": ""
},
"paths": {},
"components": {
"schemas": {
"ImagesType": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
"BodyType": {
"type": "object",
"required": [
"images"
],
"properties": {
"images": {
"oneOf": [
{
"$ref": "#/components/schemas/ImagesType"
},
{
"nullable": true
}
]
}
}
}
}
},
"tags": []
}
3 changes: 2 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ var remote = 'https://raw.githubusercontent.com/apiaryio/api-blueprint/format-1A
'OpenAPI3_headers.md',
'OpenAPI3_includes.md',
'OpenAPI3_attributes.md',
'OpenAPI3_Issue-#58.md'
'OpenAPI3_Issue-#58.md',
'OpenAPI3_Issue-#66.md'
];

var fetch = function (file) {
Expand Down

0 comments on commit e370201

Please sign in to comment.