THE GAME HAS CHANGED

Introducing Infoblox Universal DDI ManagementTM

Watch the launch to discover the new era of management for critical network services. Watch Now

API & Integration, DevOps,NetOps,SecOps

Reply

Who maintains the openapi specs and what was used to generate JSON with single quotes?

New Member
Posts: 3
2523     0

Hello I am beginning the process of developing a client to consume the openapi's found at https://github.com/infobloxopen/infoblox-swagger-wapi/tree/master/infoblox-swagger-ui/dist/v2.11.

Unfortunately, all of the openapi files are not valid JSON, largely due to the fact that single quotes were used instead of double quotes.  I tried to contact the email address within the files (ie #/info/contact/email which resolves to vsethia@infoblox.com) but I get a 550 bounce saying there is no such address.

So I would like to know who is the current support contact for the openapi files, and most importantly, what program was used to generate the single-quoted JSON so that I can try to parse it reliably in that nonstandard format.

Re: Who maintains the openapi specs and what was used to generate JSON with single quotes?

New Member
Posts: 3
2524     0

In case anyone else stumbles across this post looking for a solution to the invalid json openapi specs, I have come up with a temporary workaround:

  1. create a small npm project with `npm init`
  2. execute `npm install jsonic` and `npm install patch-package`
  3. put this patch into patches/jsonic+1.0.1.patch
diff --git a/node_modules/jsonic/jsonic.js b/node_modules/jsonic/jsonic.js
index dee2347..1ce7982 100644
--- a/node_modules/jsonic/jsonic.js
+++ b/node_modules/jsonic/jsonic.js
@@ -896,7 +896,7 @@ var jsonic_parser = (function() {
             s0 = peg$parsearray();
             if (s0 === peg$FAILED) {
               s0 = peg$currPos;
-              if (input.substr(peg$currPos, 4) === peg$c24) {
+              if (input.substr(peg$currPos, 4).toLowerCase() === peg$c24) {
                 s1 = peg$c24;
                 peg$currPos += 4;
               } else {
@@ -919,7 +919,7 @@ var jsonic_parser = (function() {
               }
               if (s0 === peg$FAILED) {
                 s0 = peg$currPos;
-                if (input.substr(peg$currPos, 5) === peg$c27) {
+                if (input.substr(peg$currPos, 5).toLowerCase() === peg$c27) {
                   s1 = peg$c27;
                   peg$currPos += 5;
                 } else {
@@ -942,7 +942,7 @@ var jsonic_parser = (function() {
                 }
                 if (s0 === peg$FAILED) {
                   s0 = peg$currPos;
-                  if (input.substr(peg$currPos, 4) === peg$c30) {
+                  if (input.substr(peg$currPos, 4).toLowerCase() === peg$c30) {
                     s1 = peg$c30;
                     peg$currPos += 4;
                   } else {

4. execute `npx patch-package`

5. put this JavaScript into index.js (note you will have to adjust lines 6&7 to suit your environment)

fs = require('fs');
jsonic = require('jsonic');
fpath = require('path');
util = require('util');

v211 = '../infoblox-swagger-wapi/infoblox-swagger-ui/dist/v2.11';
v211_jsonic = v211+'_jsonic';

function parse_examples(obj) {
  if (typeof obj !== 'object' || obj === null) return;
  if (Array.isArray(obj)) {
    obj.forEach((e) => parse_examples(e));
  } else {
    Object.keys(obj).forEach((k) => {
      if (typeof obj[k] === 'object') parse_examples(obj[k]);
      else if (k === 'example' && typeof obj[k] === 'string' && (obj[k][0] === '{' ||  obj[k][0] === '[')) {
        console.error(`old: ${obj[k]}\n`);
        obj[k] = JSON.stringify(jsonic(obj[k]));
        console.error(`new: ${obj[k]}\n`);
      }
    });
  }
}

jsons = fs.readdirSync(v211).reduce((a, v)=>{p=fpath.parse(v);if(p.ext==='.json' && p.name!=='example')a.push(p.base);return a}, []);
jsons.forEach((j)=>{
  obj=jsonic(fs.readFileSync(fpath.join(v211,j), 'utf8'));
  console.error(`obj is ${typeof obj}`);
  parse_examples(obj);
  fs.writeFileSync(fpath.join(v211_jsonic, j), JSON.stringify(obj, null, 2));
});

5.execute `mkdir /path/to/v2.11_jsonic` (again, this depends on your environment)

6. execute `node index.js`

The original files will not be modified, valid JSON will be written to new openapi files; furthermore, any "example" property that looks like JSON will have its single quotes converted to slash-escaped double-quotes, in keeping with correct JSON.  The only thing I haven't bothered to address are the use of single quotes and correct quoting of JSON in "description" properties, but that's not going to prevent you from using the openapi specs.

 

Re: Who maintains the openapi specs and what was used to generate JSON with single quotes?

Adviser
Posts: 181
2524     0

Hi,

 

Thank you for finding this and providing a temporary workaround.

 

Vedant Sethia is not with the company anymore. I'll get the team to look into it and see what we can do to update the swagger-doc.

 

Regards,

Krishna

Re: Who maintains the openapi specs and what was used to generate JSON with single quotes?

New Member
Posts: 3
2524     0

Hello Krishna, this morning I discovered that the repo was recently updated so that the openapi spec files are now valid JSON.  Thanks to the maintainers for doing that :-)  However, there is a bit of an odd wrinkle remaining, which is that properties whose names are "example" and whose values are what appears to be stringified JSON, except there are single quotes being used, and there is an unexpected lower-case "u" character immediately before each single-quoted string.

 

So for example in https://github.com/infobloxopen/infoblox-swagger-wapi/blob/master/infoblox-swagger-ui/dist/v2.11/dhc... on line 86 it reads

                    "example": "[u'RELEASE_MATCHING_ID', u'NEVER_RELEASE', u'ONE_LEASE_PER_CLIENT']"

While this doesn't cause any harm per se, it is nevertheless preferable that such example fields that are intended to have stringified JSON in them, should be formatted in a way that they will display as valid JSON, so the above would properly read as

                    "example": "[\"RELEASE_MATCHING_ID\", \"NEVER_RELEASE\", \"ONE_LEASE_PER_CLIENT\"]"

Likewise, in some of the files there are also properties named "description" which occasionally have what appears to be JSON mixed in with plain text that incorrectly uses single quotes and/or trailing commas.  So for instance, line 21309 in dhcp.json currently reads as

            "description": "A list of networks belonging to the shared network Each individual list item must be specified as an object containing a '_ref' parameter to a network reference, for example:: [{ _ref: network/ZG5zLm5ldHdvcmskMTAuMwLvMTYvMA, }] if the reference of the wanted network is not known, it is possible to specify search parameters for the network instead in the following way:: [{ _ref: { 'network': '10.0.0.0/8', } }] note that in this case the search must match exactly one network for the assignment to be successful."

but should read as

            "description": "A list of networks belonging to the shared network Each individual list item must be specified as an object containing a \"_ref\" parameter to a network reference, for example:: [{ \"_ref\": \"network/ZG5zLm5ldHdvcmskMTAuMwLvMTYvMA\" }] if the reference of the wanted network is not known, it is possible to specify search parameters for the network instead in the following way:: [{ \"_ref\": { \"network\": \"10.0.0.0/8\" } }] note that in this case the search must match exactly one network for the assignment to be successful."

I think that the rule of thumb should be that single quotes should not be used within strings exept when you need an apostrophe in a plain text word.

 

Re: Who maintains the openapi specs and what was used to generate JSON with single quotes?

Superuser
Posts: 38
2524     0

Hi Jeffoire,

Thanks for your input. I have checked the script that generates the JSON files and there seems to be no issue with that. The lower case ‘u’ character in the files may be due to a typo while the files were generated. I have generated new JSON files now and I’ll push them to GitHub as well.
As for the single quotes/trailing commas, the script just stringifies the data it gets from NIOS. So the files have the same description as in NIOS. I’ll see what I can do about fixing the single quotes as they also need to be updated for the example fields.

Thanks,
Shukran

Shukran
Showing results for 
Search instead for 
Did you mean: 

Recommended for You