Nswag: Optional parameter not being respected from specification

Created on 20 Feb 2017  Â·  11Comments  Â·  Source: RicoSuter/NSwag

Howdy!

I have a config generated by Swashbuckle, and read by NSwag Studio to create a client.

Generated Json;

"parameters": [
          {
            "name": "begin",
            "in": "query",
            "required": false,
            "type": "string",
            "format": "date-time"
          },
          {
            "name": "end",
            "in": "query",
            "required": false,
            "type": "string",
            "format": "date-time"
          }
        ]

However, the right pane with Swagger spec shows parameters with 'required' omitted;

"parameters": [
          {
            "type": "string",
            "name": "begin",
            "in": "query",
            "format": "date-time"
          },
          {
            "type": "string",
            "name": "end",
            "in": "query",
            "format": "date-time"
          }
        ]

Both toolsets are configured for the 2.0 spec

done enhancement

Most helpful comment

Maybe we should add the option "GenerateOptionalParameters" (like in CSharp) which reorders parameters and adds "?" to the optional ones...

All 11 comments

"required" is removed because it is not needed (default value is "false"). See http://swagger.io/specification/#parameterObject

required: Otherwise, the property MAY be included and its default value is false.

Ah, cool beans. So what would be the step to get Studio to reflect this as an optional parameter in a Typescript client generator? I have enabled the "Mark optional properties with ?" option and at the moment have to explicitly provide undefined for each property I don't wish to supply.

The option "Mark optional properties with ?" is only for DTO classes/interfaces... I think for TypeScript there isn't currently the option to generate default parameters.

Maybe we should add the option "GenerateOptionalParameters" (like in CSharp) which reorders parameters and adds "?" to the optional ones...

As the language stands, I would say there were two approaches; using ? syntax on an optional parameter, or defining default parameters. The inherently optional nature of query parameters, the ? syntax would be my vote.

I think we should go with ? because it does not need additional procedural code. Just FYI: As per the spec, query parameters can be required.

Hi @RSuter, I just ran into this issue with an optional boolean. I just wanted to bump this and see if there were any updates since it's been over a year. Thanks.

Have you tried the GenerateOptionalParameters?

image

@RSuter oh, thank you good sir, that's exactly what I was missing. I really appreciate the quick reply, thanks again.

Can we close this issue?

I would say so, this functionality is covered.

On Aug 16, 2018, at 3:05 AM, Rico Suter notifications@github.com wrote:

Can we close this issue?

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

Was this page helpful?
0 / 5 - 0 ratings