<commit_before>'use strict';

const path = require('path');
const util = require('util');
const up = require('url');

const yaml = require('js-yaml');
const safejson = require('safe-json-stringify');
const uri = require('urijs');
const URITemplate = require('urijs/src/URITemplate');
const dot = require('dot');
dot.templateSettings.strip = false;
dot.templateSettings.varname = 'data';

const xml = require('jgexml/json2xml.js');
const jptr = require('reftools/lib/jptr.js').jptr;
const dereference = require('reftools/lib/dereference.js').dereference;
const clone = require('reftools/lib/clone.js').clone;
const swagger2openapi = require('swagger2openapi');

const common = require('./common.js');

let templates;

function convertToToc(source,data) {
    let resources = {};
    resources[data.translations.defaultTag] = { count: 0, methods: {} };
    if (source.tags) {
        for (let tag of source.tags) {
            resources[tag.name] = { count: 0, methods: {}, description: tag.description, externalDocs: tag.externalDocs };
        }
    }
    for (var p in source.paths) {
        for (var m in source.paths[p]) {
            if ((m !== 'parameters') && (m !== 'summary') && (m !== 'description') && (!m.startsWith('x-'))) {
                var method = {};
                method.operation = source.paths[p][m];
                method.pathItem = source.paths[p];
                method.verb = m;
                method.path = p;
                method.pathParameters = source.paths[p].parameters;
                var sMethodUniqueName = (method.operation.operationId ? method.operation.operationId : m + '_' + p).split('/').join('_');
                if (data.options.tocSummary && method.operation.summary) {
                    sMethodUniqueName = method.operation.summary;
                }
                method.slug = sMethodUniqueName.toLowerCase().split(' ').join('-'); // TODO {, } and : ?
                var tagName = data.translations.defaultTag;
                if (method.operation.tags && method.operation.tags.length > 0) {
                    tagName = getTagGroup(method.operation.tags[0], data.options.tagGroups);
                }
                if (!resources[tagName]) {
                    resources[tagName] = { count: 0, methods: {} };
                }
                resources[tagName].count++;
                resources[tagName].methods[sMethodUniqueName] = method;
            }
        }
    }
    for (let r in resources) {
        if (resources[r].count <= 0) delete resources[r];
    }
    return resources;
}

function getTagGroup(tag, tagGroups){
    if (tagGroups) {
        for (let group of tagGroups) {
            if (group.tags.indexOf(tag) > -1) {
                return group.title;
            }
        }
    }
    return tag;
}

function fakeProdCons(data) {
    data.produces = [];
    data.consumes = [];
    data.bodyParameter = {};
    data.bodyParameter.exampleValues = {};
    for (var r in data.operation.responses) {
        var response = data.operation.responses[r];
        for (var prod in response.content) {
            data.produces.push(prod);
        }
    }
    let op = data.method.operation;
    if (op.requestBody) {
        for (var rb in op.requestBody.content) {
            data.consumes.push(rb);
            if (!data.bodyParameter.exampleValues.object) {
                data.bodyParameter.present = true;
                data.bodyParameter.contentType = rb;
                if (op.requestBody["x-widdershins-oldRef"]) {
                    data.bodyParameter.refName = op.requestBody["x-widdershins-oldRef"].replace('#/components/requestBodies/','');
                }
                data.bodyParameter.schema = op.requestBody.content[rb].schema;
                if (op.requestBody.content[rb].examples) {
                    let key = Object.keys(op.requestBody.content[rb].examples)[0];
                    data.bodyParameter.exampleValues.object = op.requestBody.content[rb].examples[key].value;
                    data.bodyParameter.exampleValues.description = op.requestBody.content[rb].examples[key].description;
                }
                else {
                    data.bodyParameter.exampleValues.object = common.getSample(op.requestBody.content[rb].schema,data.options,{skipReadOnly:true},data.api);
                }
                if (typeof data.bodyParameter.exampleValues.object === 'object') {
                    data.bodyParameter.exampleValues.json = safejson(data.bodyParameter.exampleValues.object,null,2);
                }
                else {
                    data.bodyParameter.exampleValues.json = data.bodyParameter.exampleValues.object;
                }
            }
        }
    }
}

function getParameters(data) {

    function stupidity(varname) {
        let s = encodeURIComponent(varname);
        s = s.split('-').join('%2D');
        s = s.split('$').join('%24');
        s = s.split('.').join('%2E');
        s = s.split('(').join('%28');
        s = s.split(')').join('%29');
        return s;
    }

    data.allHeaders = [];
    data.headerParameters = [];
    data.requiredParameters = [];
    let uriTemplateStr = data.method.path.split('/ /').join('/+/');
    let requiredUriTemplateStr = uriTemplateStr;
    var templateVars = {};

    if (data.consumes.length) {
        var contentType = {};
        contentType.name = 'Content-Type';
        contentType.type = 'string';
        contentType.in = 'header';
        contentType.exampleValues = {};
        contentType.exampleValues.json = "'" + data.consumes[0] + "'";
        contentType.exampleValues.object = data.consumes[0];
        data.allHeaders.push(contentType);
    }
    if (data.produces.length) {
        var accept = {};
        accept.name = 'Accept';
        accept.type = 'string';
        accept.in = 'header';
        accept.exampleValues = {};
        accept.exampleValues.json = "'" + data.produces[0] + "'";
        accept.exampleValues.object = data.produces[0];
        data.allHeaders.push(accept);
    }
<commit_msg>openapi3; fix for hard-coded path parameters<commit_after>'use strict';

const path = require('path');
const util = require('util');
const up = require('url');

const yaml = require('js-yaml');
const safejson = require('safe-json-stringify');
const uri = require('urijs');
const URITemplate = require('urijs/src/URITemplate');
const dot = require('dot');
dot.templateSettings.strip = false;
dot.templateSettings.varname = 'data';

const xml = require('jgexml/json2xml.js');
const jptr = require('reftools/lib/jptr.js').jptr;
const dereference = require('reftools/lib/dereference.js').dereference;
const clone = require('reftools/lib/clone.js').clone;
const swagger2openapi = require('swagger2openapi');

const common = require('./common.js');

let templates;

function convertToToc(source,data) {
    let resources = {};
    resources[data.translations.defaultTag] = { count: 0, methods: {} };
    if (source.tags) {
        for (let tag of source.tags) {
            resources[tag.name] = { count: 0, methods: {}, description: tag.description, externalDocs: tag.externalDocs };
        }
    }
    for (var p in source.paths) {
        for (var m in source.paths[p]) {
            if ((m !== 'parameters') && (m !== 'summary') && (m !== 'description') && (!m.startsWith('x-'))) {
                var method = {};
                method.operation = source.paths[p][m];
                method.pathItem = source.paths[p];
                method.verb = m;
                method.path = p;
                method.pathParameters = source.paths[p].parameters;
                var sMethodUniqueName = (method.operation.operationId ? method.operation.operationId : m + '_' + p).split('?')[0].split('/').join('_');
                if (data.options.tocSummary && method.operation.summary) {
                    sMethodUniqueName = method.operation.summary;
                }
                method.slug = sMethodUniqueName.toLowerCase().split(' ').join('-'); // TODO {, } and : ?
                var tagName = data.translations.defaultTag;
                if (method.operation.tags && method.operation.tags.length > 0) {
                    tagName = getTagGroup(method.operation.tags[0], data.options.tagGroups);
                }
                if (!resources[tagName]) {
                    resources[tagName] = { count: 0, methods: {} };
                }
                resources[tagName].count++;
                resources[tagName].methods[sMethodUniqueName] = method;
            }
        }
    }
    for (let r in resources) {
        if (resources[r].count <= 0) delete resources[r];
    }
    return resources;
}

function getTagGroup(tag, tagGroups){
    if (tagGroups) {
        for (let group of tagGroups) {
            if (group.tags.indexOf(tag) > -1) {
                return group.title;
            }
        }
    }
    return tag;
}

function fakeProdCons(data) {
    data.produces = [];
    data.consumes = [];
    data.bodyParameter = {};
    data.bodyParameter.exampleValues = {};
    for (var r in data.operation.responses) {
        var response = data.operation.responses[r];
        for (var prod in response.content) {
            data.produces.push(prod);
        }
    }
    let op = data.method.operation;
    if (op.requestBody) {
        for (var rb in op.requestBody.content) {
            data.consumes.push(rb);
            if (!data.bodyParameter.exampleValues.object) {
                data.bodyParameter.present = true;
                data.bodyParameter.contentType = rb;
                if (op.requestBody["x-widdershins-oldRef"]) {
                    data.bodyParameter.refName = op.requestBody["x-widdershins-oldRef"].replace('#/components/requestBodies/','');
                }
                data.bodyParameter.schema = op.requestBody.content[rb].schema;
                if (op.requestBody.content[rb].examples) {
                    let key = Object.keys(op.requestBody.content[rb].examples)[0];
                    data.bodyParameter.exampleValues.object = op.requestBody.content[rb].examples[key].value;
                    data.bodyParameter.exampleValues.description = op.requestBody.content[rb].examples[key].description;
                }
                else {
                    data.bodyParameter.exampleValues.object = common.getSample(op.requestBody.content[rb].schema,data.options,{skipReadOnly:true},data.api);
                }
                if (typeof data.bodyParameter.exampleValues.object === 'object') {
                    data.bodyParameter.exampleValues.json = safejson(data.bodyParameter.exampleValues.object,null,2);
                }
                else {
                    data.bodyParameter.exampleValues.json = data.bodyParameter.exampleValues.object;
                }
            }
        }
    }
}

function getParameters(data) {

    function stupidity(varname) {
        let s = encodeURIComponent(varname);
        s = s.split('-').join('%2D');
        s = s.split('$').join('%24');
        s = s.split('.').join('%2E');
        s = s.split('(').join('%28');
        s = s.split(')').join('%29');
        return s;
    }

    data.allHeaders = [];
    data.headerParameters = [];
    data.requiredParameters = [];
    let uriTemplateStr = data.method.path.split('?')[0].split('/ /').join('/+/');
    let requiredUriTemplateStr = uriTemplateStr;
    var templateVars = {};

    if (data.consumes.length) {
        var contentType = {};
        contentType.name = 'Content-Type';
        contentType.type = 'string';
        contentType.in = 'header';
        contentType.exampleValues = {};
        contentType.exampleValues.json = "'" + data.consumes[0] + "'";
        contentType.exampleValues.object = data.consumes[0];
        data.allHeaders.push(contentType);
    }
    if (data.produces.length) {
        var accept = {};
        accept.name = 'Accept';
        accept.type = 'string';
        accept.in = 'header';
        accept.exampleValues = {};
        accept.exampleValues.json = "'" + data.produces[0] + "'";
        accept.exampleValues.object = data.produces[0];
        data.allHeaders.push(accept);
    }

{'fraction_non_alphanumeric': 0.1471241668723772, 'fraction_numerical': 0.004196494692668477, 'mean_word_length': 2.1430049133695372, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '45444', 'n_tokens_mistral': 3534, 'n_tokens_neox': 3430, 'n_words': 593}