Search code examples
node.jsswaggerswagger-uijoihapi-swagger

Swagger UI seems to not handle optional keys with POST json body


Packages :

hapi-swagger: 9.0.1
joi: 13.0.2

Context :

I use the swagger-ui with a swagger.json file generated by hapi-swagger.

I use for hapi-swagger the jsonEditor option set to true. Which means that i don't enter myself the body in a single text area but i use directly the generated inputs of the UI.

Issue :

Only the key "name" is required in the payload, the other ones are optional by default if i refer to the Joi doc.

Actually the Swagger-UI sends :

curl -X POST
--header 'Content-Type: application/json' 
--header 'Accept: application/json' 
-d '{
 "name":"fzef",

 "idsUser": [],

 "idsUsergroup":[]
}'

Instead i want that the Swagger-UI to send a request like :

curl -X POST 
--header 'Content-Type: application/json' 
--header 'Accept: application/json' 
-d '{
 "name":"fzef",
 }'

Swagger UI

enter image description here

Joi Schema

Joi.object().keys({

 request: Joi.object().keys({

     name: Joi.string().required(),

     idsUser: Joi.array().items(Joi.string()),

     idsUsergroup: Joi.array().items(Joi.string()),
   }),
  }),
 });

Swagger.json

...
"paths": {
  "/addCompany": {
    "post": {
      "operationId": "postAddcompany",

       "parameters": [{
          "type": "string",
          "default": "1",
          "pattern": "/^v[0-9]+$/",
          "name": "apiVersion",
          "in": "path",
          "required": true
        },
        {
          "in": "body",
          "name": "body",
          "schema": {
            "$ref": "#/definitions/Model 208"
           }
         }
       ],

      "tags": ["api", "CompanyCommandsAPIPart"],

      "responses": {
        "default": {
          "schema": {
            "type": "string"
          },
         "description": "Successful"
        },
      }
    }
  }
}

"definitions": {
  ...
  "Model 208": {
    "type": "object",

    "properties": {
      "name": {
        "type": "string"
      },

      "idsUser": {
         "$ref": "#/definitions/Model 13",
        "type": "array",
        "x-alternatives": [{
          "$ref": "#/x-alt-definitions/idsFunctionality",
          "type": "array"
        }, {
          "type": "string"
        }]
      },

      "idsUsergroup": {
         "$ref": "#/definitions/Model 13",
         "type": "array",
        "x-alternatives": [{
          "$ref": "#/x-alt-definitions/idsFunctionality",
          "type": "array"
        }, {
          "type": "string"
        }]
      },
    },

    "required": ["name"]
  },
  ...
}

What can i do to get this request body ?

Do i need to precise a joi method in order that the hapi-swagger parser add a parameter like 'optional' to the swagger.json ?

I found the same issue for the query of a GET method but found no solution:

https://github.com/swagger-api/swagger-editor/issues/684


Solution

  • The JSON Editor component provides the "Properties" and "Edit JSON" buttons to customize the JSON payload, as you can in the component demo here: http://jeremydorn.com/json-editor/. However, Swagger UI 2.x (the version used by hapi-swagger at the time of writing) initializes the JSON Editor with disable_properties: true and disable_edit_json: true so that these buttons are hidden, and the UI does not expose any configuration options to change the JSON Editor options. There is an open issue in the hapi-editor repository on GitHub: https://github.com/glennjones/hapi-swagger/issues/332.

    A possible workaround is to tweak the Swagger UI code. Assuming your Swagger UI's index.html uses unminified swagger-ui.js, find the following lines in <hapi-swagger>/public/swaggerui/swagger-ui.js:

    disable_properties:true,
    disable_edit_json:true,
    

    and replace them with:

    disable_properties:false,
    disable_edit_json:false,
    

    Now the JSON Editor will have the "Object Properties" button. Click this button to select the properties to be displayed in the form editor and included in the request body. Unselected properties will not be sent in the request body.

    Swagger UI - JSON Editor - excluding optional properties