Home > OS >  Swagger shows 'string' instead of body example
Swagger shows 'string' instead of body example

Time:12-11

I am trying to set a default json body for the following endpoint:

const loginJsonSchema = {
    'type': 'object',
    'properties': {
        'username': {
            'type': 'string',
            'example': 'my_user' // this property is ignored
        },
        'password': {
            'type': 'string',
            'example': 'my_password' // this property is ignored
        },
    },
    'required': ['username', 'password'],
}


const paths = {
    "/auth/login": {
        "post": {
            "tags": [
                "auth"
            ],
            "summary": "Login",
            "description": "",
            "operationId": "auth-login",
            "consumes": [
                "application/json"
            ],
            "parameters": [
                {
                    "name": "Credentials",
                    "in": "body",
                    "description": "Your credentials",
                    "required": true,
                    "schema": {
                        loginJsonSchema
                    }
                }
            ],
            "responses": {
                "200": {
                    "description": "successful operation",
                    "schema": loginSuccessResponseSchema,
                },
            },
        }
    },

Instead of the example values, I get the default body as 'string':

enter image description here

I expected the following default body:

{
  "username": "my_user",
  "password": "my_password"
}

CodePudding user response:

You need to remove the extra { } from schema like this:

const paths = {
"/auth/login": {
    "post": {
        "tags": [
            "auth"
        ],
        "summary": "Login",
        "description": "",
        "operationId": "auth-login",
        "consumes": [
            "application/json"
        ],
        "parameters": [
            {
                "name": "Credentials",
                "in": "body",
                "description": "Your credentials",
                "required": true,
                "schema": loginJsonSchema
            }
        ],
        "responses": {
            "200": {
                "description": "successful operation",
                "schema": loginSuccessResponseSchema,
            },
        },
    }
},

See here: https://jsfiddle.net/5gfpy1qr/

  • Related