Search code examples
node.jspropertiesswaggermongoose-schema

How do I determine the order of "properties" with swagger-autogen in node.js?


For some reason, swagger-autogen is reversing the order of my properties in my swagger.json output file. For example, I have a simple 'user' mongoose schema set up with only 'username' and 'password' properties. When I run swagger-autogen, my PUT swagger-docs populate with the password first and the username last. I need username first and password last. I have to manually fix it in my swagger.json file every time I run swagger-autogen. Is there a way to determine properties order in my swagger.js or possibly on my controllers functions so I don't have to alter it every time?


Solution

  • swagger-autogen doesn't have an option to determine the order of parameters. In your case, you have two options:

    Option 1: Try reordering your mongoose schema.

    Option 2: Manually declare the parameter in the endpoint that contains the put method as follows:

    
    app.put('/somepath', (req, res) => {
    
        /*  #swagger.parameters['body'] = {
                in: 'body',
                required: true,
                schema: {
                    username: "user",
                    password: "1234"
                }
        } */
    
        ...
    })
    

    See more about swagger-autogen parameters here

    If none of the options work, send us a link to a sample project or post here some pieces of your code that reproduces this problem. That way it's easier to help you.

    disclaimer:
    working for swagger v1, v2, but not for v3