jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/352716 )

Change subject: Assert valid transformations between formats in the http api
......................................................................


Assert valid transformations between formats in the http api

Change-Id: Ib3baa70d90b6554bebe857478b22d1f168b58ed6
---
M lib/api/routes.js
1 file changed, 16 insertions(+), 16 deletions(-)

Approvals:
  Subramanya Sastry: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/api/routes.js b/lib/api/routes.js
index 289f007..580a5ab 100644
--- a/lib/api/routes.js
+++ b/lib/api/routes.js
@@ -53,12 +53,20 @@
                next();
        };
 
-       var supportedFormats = new Set(['pagebundle', 'html', 'wikitext']);
-
        var errorEncoding = new Map(Object.entries({
                'pagebundle': 'json',
                'html': 'html',
                'wikitext': 'plain',
+       }));
+
+       // NOTE: wt2wt here is not a transformation, it just fetches the page
+       // wikitext and is limited to GET requests.  The endpoint is used in
+       // roundtrip testing.
+
+       var validTransforms = new Map(Object.entries({
+               'wikitext': ['wikitext', 'html', 'pagebundle'],
+               'html': ['wikitext'],
+               'pagebundle': ['wikitext', 'pagebundle'],
        }));
 
        routes.v3Middle = function(req, res, next) {
@@ -79,15 +87,11 @@
                        format: req.params.format,
                }, req.body);
 
-               if (!supportedFormats.has(opts.format)) {
-                       res.locals.errorEnc = 'plain';
-                       return errOut(res, 'Invalid format: ' + opts.from + 
'/to/' + opts.format);
-               } else {
-                       res.locals.errorEnc = errorEncoding.get(opts.format);
-               }
+               res.locals.errorEnc = errorEncoding.get(opts.format) || 'plain';
 
-               if (!supportedFormats.has(opts.from)) {
-                       return errOut(res, 'Invalid format: ' + opts.from + 
'/to/' + opts.format);
+               var transforms = validTransforms.get(opts.from);
+               if (transforms === undefined || 
!transforms.includes(opts.format)) {
+                       return errOut(res, 'Invalid transform: ' + opts.from + 
'/to/' + opts.format);
                }
 
                var iwp = parsoidConfig.reverseMwApiMap.get(req.params.domain);
@@ -774,7 +778,7 @@
                var env = res.locals.env;
                var p;
                if (opts.from === 'wikitext') {
-                       // No use case for this yet
+                       // No use case for this yet ... we only accept it with 
GET above.
                        if (opts.format === 'wikitext') {
                                return apiUtils.fatalRequest(env, 'Invalid 
format', 400);
                        }
@@ -792,7 +796,7 @@
                                return apiUtils.fatalRequest(env, 'No title or 
wikitext was provided.', 400);
                        }
                        p = wt2html(req, res, wikitext);
-               } else {  // from html/pagebundle
+               } else {  // from pagebundle
                        if (opts.format === 'wikitext') {
                                // html is required for serialization
                                if (opts.html === undefined) {
@@ -803,10 +807,6 @@
                                        opts.html : (opts.html.body || '');
                                p = html2wt(req, res, html);
                        } else {
-                               // No use case for this yet
-                               if (opts.from === 'html' || opts.to === 'html') 
{
-                                       return apiUtils.fatalRequest(env, 
'Invalid to or from', 400);
-                               }
                                p = pb2pb(req, res);
                        }
                }

-- 
To view, visit https://gerrit.wikimedia.org/r/352716
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ib3baa70d90b6554bebe857478b22d1f168b58ed6
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Arlolra <abrea...@wikimedia.org>
Gerrit-Reviewer: Subramanya Sastry <ssas...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to