-
Notifications
You must be signed in to change notification settings - Fork 6k
Description
Description
Using the default options, the client does not serialize enum properties if they are the default enum value. If the property is marked as required, this means that the resulting JSON does not actually satisfy the specification.
This only occurs for enums which are defined as their own object. If the enum is defined inline, the property is generated as MyEnumType?, but for the ones defined as top level definitions, it is just MyEnumType
Setting the -DoptionalEmitDefaultValues=true parameter will change this, but will affect all properties.
Swagger-codegen version
2.2.3
Swagger declaration file content or url
swagger: "2.0"
info:
description: "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters."
version: "1.0.0"
title: "Swagger Petstore"
termsOfService: "http://swagger.io/terms/"
contact:
email: "apiteam@swagger.io"
license:
name: "Apache 2.0"
url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "petstore.swagger.io"
basePath: "/v2"
tags:
- name: "pet"
description: "Everything about your Pets"
externalDocs:
description: "Find out more"
url: "http://swagger.io"
- name: "store"
description: "Access to Petstore orders"
- name: "user"
description: "Operations about user"
externalDocs:
description: "Find out more about our store"
url: "http://swagger.io"
schemes:
- "http"
paths:
/pet:
post:
tags:
- "pet"
summary: "Add a new pet to the store"
description: ""
operationId: "addPet"
consumes:
- "application/json"
- "application/xml"
produces:
- "application/xml"
- "application/json"
parameters:
- in: "body"
name: "body"
description: "Pet object that needs to be added to the store"
required: true
schema:
$ref: "#/definitions/Pet"
responses:
405:
description: "Invalid input"
definitions:
PetStatus:
type: "string"
description: "pet status in the store"
enum:
- "available"
- "pending"
- "sold"
Pet:
type: "object"
required:
- "name"
- "status"
properties:
name:
type: "string"
example: "doggie"
status:
$ref: "#/definitions/PetStatus"Command line used for generation
java -jar swagger-codegen-cli-2.2.3.jar generate -i ./swagger.json -l csharp
where swagger.json is the result of downloading the JSON file from the online swagger editor for the above YAML
Steps to reproduce
-
Generate the client
-
Run the following code snippet against the generated code:
new Pet("Kitty McKittyface", PetStatus.Available).ToJson();
The result will be missing the status:
{ "name": "Kitty McKittyface" } -
Alternately, the following serializes correctly since the PetStatus value is not the default:
new Pet("Kitty McKittyface", PetStatus.Sold).ToJson();
Related issues/PRs
#4262 is somewhat related, but doesn't cover this exact scenario
Suggest a fix/enhancement
From looking through the other issues, it seems like Nullable types for required properties is a bit of a sticking point, so would it make sense to ignore the optionalEmitDefaultValues argument for required parameters (even beyond enums?) and also set EmitDefaultValue=true for required properties?