twskj / pretty-swag

Pretty UI for Swagger spec
MIT License
122 stars 20 forks source link

Support enumeration for query parameters #57

Open nicolasberthel opened 5 years ago

nicolasberthel commented 5 years ago

Hi, it would be great to support enumerations for simple query parameters (like strings for instance) The idea would be (like in swagger UI) to display a list of allowed value. For instance

- in: query
        name: time_format
        required: false
        description: "Parameter to change the output time format."
        type: string
        enum: [local, utc, epoch]
        default: utc

It's working for me with this adaptation of computeSchema function

function computeType(enumeration, type, schema) {
    let pre = schema === "array" ? "[" : "";
    let post = schema === "array" ? "]" : "";
    if (enumeration) {
        if (type === "string") {
            let sep = enumeration.length < 4 ? "," : ",\n";

            return pre + enumeration.map(function (x) {
                return '"' + x + '"';
            }).join(sep) + post;
        }
        else {
            return pre + enumeration + post;
        }
    }
    else {
        return pre + '"' + type + '"' + post;
    }
}

function computeSchema(schema, def, context, required) {
    if (!required) {
        required = [];
    }
    if (typeof schema === "string") {
        required.push(context.required ? true : false);
        if (schema === "array") {
            if (context.items) {
                return computeType(context.items.enum, context.items.type, schema);
            }
            else {
                return "[]";
            }
        }
        else {
            return computeType(context.enum, context.type, schema);
        }
    }
    var src = "a=" + resolveNested(schema, def, required);
    var tokens = esprima.tokenize(src, { comment: true });
    tmp = format(tokens, indent_num);
    return unEscapeComment(tmp);
}

Thanks for your support and your great work.