From b124824a8d554f0d694b7f55851434c0b45b85f2 Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Thu, 12 Jun 2014 03:56:54 -0400 Subject: [PATCH 1/3] added file type to FormType Parser --- DataTypes.php | 20 ++++++++++---------- Parser/FormTypeParser.php | 1 + 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/DataTypes.php b/DataTypes.php index 7e02741..1df2190 100644 --- a/DataTypes.php +++ b/DataTypes.php @@ -18,27 +18,27 @@ namespace Nelmio\ApiDocBundle; */ class DataTypes { - const INTEGER = 'integer'; + const INTEGER = 'integer'; - const FLOAT = 'float'; + const FLOAT = 'float'; - const STRING = 'string'; + const STRING = 'string'; - const BOOLEAN = 'boolean'; + const BOOLEAN = 'boolean'; - const FILE = 'file'; + const FILE = 'file'; - const ENUM = 'choice'; + const ENUM = 'choice'; const COLLECTION = 'collection'; - const MODEL = 'model'; + const MODEL = 'model'; - const DATE = 'date'; + const DATE = 'date'; - const DATETIME = 'datetime'; + const DATETIME = 'datetime'; - const TIME = 'time'; + const TIME = 'time'; /** * Returns true if the supplied `actualType` value is considered a primitive type. Returns false, otherwise. diff --git a/Parser/FormTypeParser.php b/Parser/FormTypeParser.php index bddfa05..14a4028 100644 --- a/Parser/FormTypeParser.php +++ b/Parser/FormTypeParser.php @@ -46,6 +46,7 @@ class FormTypeParser implements ParserInterface 'textarea' => DataTypes::STRING, 'country' => DataTypes::STRING, 'choice' => DataTypes::ENUM, + 'file' => DataTypes::FILE, ); public function __construct(FormFactoryInterface $formFactory) From 210596eae94436f05c49077c226dde3e3e12c8e1 Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Tue, 17 Jun 2014 00:10:34 -0400 Subject: [PATCH 2/3] adding support for different parameter types --- Resources/views/layout.html.twig | 59 ++++++++++++++++++++++++++++++-- Resources/views/method.html.twig | 27 +++++++++++++-- 2 files changed, 81 insertions(+), 5 deletions(-) diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig index 2c964fe..b67a5c5 100644 --- a/Resources/views/layout.html.twig +++ b/Resources/views/layout.html.twig @@ -101,6 +101,39 @@ }); {% if enableSandbox %} + var setParameterType = function ($context,setType) { + // no 2nd argument, use default from parameters + if (typeof setType == "undefined") { + setType = $context.parent().attr("data-dataType"); + $context.val(setType); + } + + $context.parent().find('.value').remove(); + var placeholder = ""; + if ($context.parent().attr("data-dataType") != "" && typeof $context.parent().attr("data-dataType") != "undefined") { + placeholder += "[" + $context.parent().attr("data-dataType") + "] "; + } + if ($context.parent().attr("data-format") != "" && typeof $context.parent().attr("data-format") != "undefined") { + placeholder += $context.parent().attr("data-dataType"); + } + if ($context.parent().attr("data-description") != "" && typeof $context.parent().attr("data-description") != "undefined") { + placeholder += $context.parent().attr("data-description"); + } else { + placeholder += "Value"; + } + + switch(setType) { + case "boolean": + $('').insertAfter($context); + break; + case "file": + $('').insertAfter($context); + break; + default: + $('').insertAfter($context); + } + }; + var toggleButtonText = function ($btn) { if ($btn.text() === 'Default') { $btn.text('Raw'); @@ -369,8 +402,30 @@ e.preventDefault(); }); - $('.pane.sandbox').on('click', '.add', function() { - var html = $(this).parents('.pane').find('.tuple_template').html(); + + // sets the correct parameter type on load + $('.pane.sandbox .tuple_type').each(function() { + setParameterType($(this)); + }); + + + // handles parameter type change + $('.pane.sandbox').on('change', '.tuple_type', function() { + setParameterType($(this),$(this).val()); + }); + + + + $('.pane.sandbox').on('click', '.add_parameter', function() { + var html = $(this).parents('.pane').find('.parameters_tuple_template').html(); + + $(this).before(html); + + return false; + }); + + $('.pane.sandbox').on('click', '.add_header', function() { + var html = $(this).parents('.pane').find('.headers_tuple_template').html(); $(this).before(html); diff --git a/Resources/views/method.html.twig b/Resources/views/method.html.twig index b4b4ac2..8089805 100644 --- a/Resources/views/method.html.twig +++ b/Resources/views/method.html.twig @@ -228,14 +228,20 @@

Parameters

{% for name, infos in data.parameters %} {% if not infos.readonly %} -

+

= + -

{% endif %} {% endfor %} - + {% endif %} @@ -257,7 +263,7 @@ -

- +
@@ -282,10 +288,25 @@

= + -

+ + +

Request URL



From 1cd77e2f141ef8a0b09454d4c37f013edf9ee5fa Mon Sep 17 00:00:00 2001
From: Jonathan Chan 
Date: Tue, 17 Jun 2014 02:02:35 -0400
Subject: [PATCH 3/3] added support to properly handle file upload POST

---
 Resources/views/layout.html.twig | 56 +++++++++++++++++++++++++++++---
 Resources/views/method.html.twig |  2 +-
 2 files changed, 52 insertions(+), 6 deletions(-)

diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig
index b67a5c5..773d39e 100644
--- a/Resources/views/layout.html.twig
+++ b/Resources/views/layout.html.twig
@@ -254,6 +254,7 @@
                         method = $(this).attr('method'),
                         self = this,
                         params = {},
+                        formData = new FormData(),
                         headers = {},
                         content = $(this).find('textarea.content').val(),
                         result_container = $('.result', $(this).parent());
@@ -268,6 +269,7 @@
                     var requestFormatMethod = '{{ requestFormatMethod }}';
                     if (requestFormatMethod == 'format_param') {
                         params['_format'] = $('#request_format option:selected').text();
+                        formData.append('_format',$('#request_format option:selected').text());
                     } else if (requestFormatMethod == 'accept_header') {
                         headers['Accept'] = $('#request_format').val();
                     }
@@ -279,6 +281,37 @@
                         headers['Content-type'] = 'application/'+bodyFormat;
                     }
 
+                    var hasFileTypes = false;
+                    $('.parameters .tuple_type', $(this)).each(function() {
+                        if ($(this).val() == 'file') {
+                            hasFileTypes = true;
+                        }
+                    });
+
+                    if (hasFileTypes && method != 'POST') {
+                        alert("Sorry, you can only submit files via POST.");
+                        return false;
+                    }
+
+                    if (hasFileTypes) {
+                        // retrieve all the parameters to send for file upload
+                        $('.parameters .tuple', $(this)).each(function() {
+                            var key, value;
+
+                            key = $('.key', $(this)).val();
+                            if ($('.value', $(this)).attr('type') === 'file' ) {
+                                value = $('.value', $(this)).prop('files')[0];
+                            } else {
+                                value = $('.value', $(this)).val();
+                            }
+
+                            if (value) {
+                                formData.append(key,value);
+                            }
+                        });
+                    }
+
+
                     // retrieve all the parameters to send
                     $('.parameters .tuple', $(this)).each(function() {
                         var key, value;
@@ -291,6 +324,10 @@
                         }
                     });
 
+
+
+
+
                     // retrieve the additional headers to send
                     $('.headers .tuple', $(this)).each(function() {
                         var key, value;
@@ -312,7 +349,7 @@
                         }
                     };
 
-                    // disable all the fiels and buttons
+                    // disable all the fields and buttons
                     $('input, button', $(this)).attr('disabled', 'disabled');
 
                     // append the query authentication
@@ -340,9 +377,7 @@
                         body = params;
                     }
                     var data = content.length ? content : body;
-
-                    // and trigger the API call
-                    $.ajax({
+                    var ajaxOptions = {
                         url: endpoint + url,
                         type: method,
                         data: data,
@@ -361,7 +396,18 @@
                             // and enable them back
                             $('input:not(.content-type), button', $(self)).removeAttr('disabled');
                         }
-                    });
+                    };
+
+                    // overrides body format to send data properly
+                    if (hasFileTypes) {
+                        ajaxOptions.data = formData;
+                        ajaxOptions.processData = false;
+                        ajaxOptions.contentType = false;
+                        delete(ajaxOptions.headers);
+                    }
+
+                    // and trigger the API call
+                    $.ajax(ajaxOptions);
 
                     return false;
                 });
diff --git a/Resources/views/method.html.twig b/Resources/views/method.html.twig
index 8089805..448fba7 100644
--- a/Resources/views/method.html.twig
+++ b/Resources/views/method.html.twig
@@ -241,7 +241,7 @@
                                         

{% endif %} {% endfor %} - + {% endif %}