<commit_before>var url = require('url'),
    http = require('http'),
    protagonist = require('protagonist'),
    UriTemplate = require('uritemplate');

var apib2swagger = module.exports.convertParsed = function(apib) {
    //console.log(JSON.stringify(apib, null, 4));
    var swagger = {};
    swagger.swagger = '2.0';
    swagger.info = {
        'title': apib.name,
        'version': '',
        'description': apib.description
    }
    for (var i = 0; i < apib.metadata.length; i++) {
        var meta = apib.metadata[i];
        //console.log(meta);
        if (meta.name.toLowerCase() === 'host') {
            var urlParts = url.parse(meta.value);
            swagger.host = urlParts.host;
            swagger.basePath = urlParts.pathname;
            swagger.schemes = [urlParts.protocol.replace(':','')];
        }
    }
    swagger.paths = {};
    swagger.definitions = {};
    for (var i = 0; i < apib.content.length; i++) {
        // description in Resource group section is discarded
        var category = apib.content[i];
        if (category.element !== 'category') continue;
        for (var j = 0; j < category.content.length; j++) {
            var content = category.content[j];
            if (content.element === 'resource') {
                // (name, description) in Resource section are discarded
                var uriTemplate = UriTemplate.parse(content.uriTemplate),
                    pathName = swaggerPathName(uriTemplate),
                    groupName = category.attributes ? category.attributes.name : '(no tags)';
                swagger.paths[pathName] = swaggerPath(content, groupName, uriTemplate);
                continue;
            }
            if (content.element === 'copy') {
                continue;
            }
            if (content.element === 'dataStructure') {
                swagger.definitions[content.name.literal] = jsonSchemaFromMSON(content);
                continue;
            }
        }
    }
    return swagger;
}

function swaggerPathName(uriTemplate) {
    var params = {};
    for (var i = 0; i < uriTemplate.expressions.length; i++) {
        var exp = uriTemplate.expressions[i];
        if (!exp.varspecs) continue;
        if (exp.operator.symbol === '?') continue; // query
        for (var j = 0; j < exp.varspecs.length; j++) {
            var spec = exp.varspecs[j];
            params[spec.varname] = '{' + spec.varname + '}';
        }
    }
    return decodeURIComponent(uriTemplate.expand(params));
}

function swaggerPath(resource, tag, uriTemplate) {
    var definitions = {}, // TODO: global json schema
        scheme = searchDataStructure(resource.content); // Attributes 1
    if (resource.name && scheme) {
        definitions[resource.name] = scheme;
    }
    if (resource.model.content) {
        scheme = searchDataStructure(resource.model.content); // Attribute 2
        if (resource.model.name && scheme) {
            definitions[resource.model.name + 'Model'] = scheme;
        }
    }
    var path = {};
    //path.parameters = swaggerParameters(resource.parameters, uriTemplate);
    var pathParams = swaggerParameters(resource.parameters, uriTemplate); // for swagger ui
    for (var k = 0; k < resource.actions.length; k++) {
        var action = resource.actions[k];
        path[action.method.toLowerCase()] = swaggerOperation(pathParams, uriTemplate, action, tag);
    }
    return path;
}

var swaggerOperation = function (pathParams, uriTemplate, action, tag) {
    var operation = {
        'responses': swaggerResponses(action.examples),
        'summary': action.name,
        'description': action.description,
        'tags': [tag],
        'parameters': pathParams.concat(swaggerParameters(action.parameters, uriTemplate))
    }
    //operation.produces = [];
    //for (var key in operation.responses) {
    //    var response = operation.responses[key];
    //    for (var mime in response.examples) {
    //        operation.produces.push(mime);
    //    }
    //}
    // body parameter (schema)
    var schema = [],
        scheme = searchDataStructure(action.content); // Attributes 3
    if (scheme) schema.push(scheme);
    for (var j = 0; j < action.examples.length; j++) {
        var example = action.examples[j];
        for (var l = 0; l < example.requests.length; l++) {
            var request = example.requests[l];
            scheme = searchDataStructure(request.content); // Attributes 4
            if (scheme) schema.push(scheme);
            if (request.reference) {
                schema.push({'$ref': '#/definitions/' + request.reference.id + 'Model'});
            }
        }
        //for (var l = 0; l < example.responses.length; l++) {
        //    var response = example.responses[l];
        //    scheme = searchDataStructure(response.content));
        //    if (scheme) schema.push(scheme);
        //}
    }
    if (schema.length > 0) {
        operation.parameters.push({name: 'body', in: 'body', schema: {allOf: schema}});
    }
    return operation;
}
<commit_msg>modify body parameters's schema for visibility on swagger ui.<commit_after>var url = require('url'),
    http = require('http'),
    protagonist = require('protagonist'),
    UriTemplate = require('uritemplate');

var apib2swagger = module.exports.convertParsed = function(apib) {
    //console.log(JSON.stringify(apib, null, 4));
    var swagger = {};
    swagger.swagger = '2.0';
    swagger.info = {
        'title': apib.name,
        'version': '',
        'description': apib.description
    }
    for (var i = 0; i < apib.metadata.length; i++) {
        var meta = apib.metadata[i];
        //console.log(meta);
        if (meta.name.toLowerCase() === 'host') {
            var urlParts = url.parse(meta.value);
            swagger.host = urlParts.host;
            swagger.basePath = urlParts.pathname;
            swagger.schemes = [urlParts.protocol.replace(':','')];
        }
    }
    swagger.paths = {};
    swagger.definitions = {};
    for (var i = 0; i < apib.content.length; i++) {
        // description in Resource group section is discarded
        var category = apib.content[i];
        if (category.element !== 'category') continue;
        for (var j = 0; j < category.content.length; j++) {
            var content = category.content[j];
            if (content.element === 'resource') {
                // (name, description) in Resource section are discarded
                var uriTemplate = UriTemplate.parse(content.uriTemplate),
                    pathName = swaggerPathName(uriTemplate),
                    groupName = category.attributes ? category.attributes.name : '(no tags)';
                swagger.paths[pathName] = swaggerPath(content, groupName, uriTemplate, swagger.definitions);
                continue;
            }
            if (content.element === 'copy') {
                continue;
            }
            if (content.element === 'dataStructure') {
                swagger.definitions[content.name.literal] = jsonSchemaFromMSON(content);
                continue;
            }
        }
    }
    return swagger;
}

function swaggerPathName(uriTemplate) {
    var params = {};
    for (var i = 0; i < uriTemplate.expressions.length; i++) {
        var exp = uriTemplate.expressions[i];
        if (!exp.varspecs) continue;
        if (exp.operator.symbol === '?') continue; // query
        for (var j = 0; j < exp.varspecs.length; j++) {
            var spec = exp.varspecs[j];
            params[spec.varname] = '{' + spec.varname + '}';
        }
    }
    return decodeURIComponent(uriTemplate.expand(params));
}

function swaggerPath(resource, tag, uriTemplate, definitions) {
    var scheme = searchDataStructure(resource.content); // Attributes 1
    if (resource.name && scheme) {
        definitions[resource.name] = scheme;
    }
    if (resource.model.content) {
        scheme = searchDataStructure(resource.model.content); // Attribute 2
        if (resource.model.name && scheme) {
            definitions[resource.model.name + 'Model'] = scheme;
        }
    }
    var path = {};
    //path.parameters = swaggerParameters(resource.parameters, uriTemplate);
    var pathParams = swaggerParameters(resource.parameters, uriTemplate); // for swagger ui
    for (var k = 0; k < resource.actions.length; k++) {
        var action = resource.actions[k];
        path[action.method.toLowerCase()] = swaggerOperation(pathParams, uriTemplate, action, tag);
    }
    return path;
}

var swaggerOperation = function (pathParams, uriTemplate, action, tag) {
    var operation = {
        'responses': swaggerResponses(action.examples),
        'summary': action.name,
        'description': action.description,
        'tags': [tag],
        'parameters': pathParams.concat(swaggerParameters(action.parameters, uriTemplate))
    }
    //operation.produces = [];
    //for (var key in operation.responses) {
    //    var response = operation.responses[key];
    //    for (var mime in response.examples) {
    //        operation.produces.push(mime);
    //    }
    //}
    // body parameter (schema)
    var schema = [],
        scheme = searchDataStructure(action.content); // Attributes 3
    if (scheme) schema.push(scheme);
    for (var j = 0; j < action.examples.length; j++) {
        var example = action.examples[j];
        for (var l = 0; l < example.requests.length; l++) {
            var request = example.requests[l];
            scheme = searchDataStructure(request.content); // Attributes 4
            if (scheme) schema.push(scheme);
            if (request.reference) {
                schema.push({'$ref': '#/definitions/' + request.reference.id + 'Model'});
            }
        }
        //for (var l = 0; l < example.responses.length; l++) {
        //    var response = example.responses[l];
        //    scheme = searchDataStructure(response.content));
        //    if (scheme) schema.push(scheme);
        //}
    }
    if (schema.length == 1) {
        operation.parameters.push({name: 'body', in: 'body', schema: schema[0]});
    } else if (schema.length > 1) {
        operation.parameters.push({name: 'body', in: 'body', schema: {allOf: schema}});
    }
    return operation;
}

{'fraction_non_alphanumeric': 0.14142899147309615, 'fraction_numerical': 0.0037243947858473, 'mean_word_length': 2.226051217198862, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '558665', 'n_tokens_mistral': 2941, 'n_tokens_neox': 2896, 'n_words': 657}