swagger-api / swagger-api/swagger-codegen

[Swift4] DefaultValue variable used in mustache template appears to not exist?

Open
#8,341 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

It doesn't appear that the defaultValue variable is being generated when generating models with the language swift4.

Swagger-codegen version

2.4.0

Swagger declaration file content or url
definitions: {
  Order: {
    type: "object",
    properties: {
      id: {
        type: "integer",
        format: "int64"
      },
      petId: {
        type: "integer",
        format: "int64"
      },
      quantity: {
        type: "integer",
        format: "int32"
      },
      shipDate: {
        type: "string",
        format: "date-time"
      },
      status: {
        type: "string",
        description: "Order Status",
        enum: [
          "placed",
          "approved",
          "delivered"
        ]
      },
      complete: {
        type: "boolean",
        default: false
      }
    },
    xml: {
      name: "Order"
    }
  },
...
Command line used for generation
java -jar swagger-codegen-cli.jar generate \
-i https://petstore.swagger.io/v2/swagger.json \
-l swift4 -o /Users/.../.../.../.../.../SwaggerDemo -Dmodels
Steps to reproduce
  1. Run the above command to produce swift models based off of the https://petstore.swagger.io/v2/swagger.json Open Api Specification.
  2. Inspect the Order.swift model
  3. The Order.swift model contains a class definition that looks like this:
public struct Order: Codable {

    public enum Status: String, Codable { 
        case placed = "placed"
        case approved = "approved"
        case delivered = "delivered"
    }
    public var _id: Int64?
    public var petId: Int64?
    public var quantity: Int?
    public var shipDate: Date?
    /** Order Status */
    public var status: Status?
    public var complete: Bool?

The complete Boolean property is defined with a default value, but the default value is not set:

      complete: {
        type: "boolean",
        default: false
      }
Related issues/PRs
Suggest a fix/enhancement

When I debug with -DdebugModels

java -DdebugModels -jar swagger-codegen-cli.jar generate \
  -i petstorecomplete.json \
  -l swift4

I see this output for the OrderClass:

{
      "baseName" : "complete",
      "getter" : "getComplete",
      "setter" : "setComplete",
      "datatype" : "Bool",
      "datatypeWithEnum" : "Bool",
      "name" : "complete",
      "defaultValueWithParam" : " = data.complete;",
      "baseType" : "Bool",
      "jsonSchema" : "{\n  \"type\" : \"boolean\",\n  \"default\" : false\n}",
      "exclusiveMinimum" : false,
      "exclusiveMaximum" : false,
      "hasMore" : false,
      "required" : false,
      "secondaryParam" : false,
      "hasMoreNonReadOnly" : false,
      "isPrimitiveType" : true,
      "isContainer" : false,
      "isNotContainer" : true,
      "isString" : false,
      "isNumeric" : false,
      "isInteger" : false,
      "isLong" : false,
      "isNumber" : false,
      "isFloat" : false,
      "isDouble" : false,
      "isByteArray" : false,
      "isBinary" : false,
      "isFile" : false,
      "isBoolean" : true,
      "isDate" : false,
      "isDateTime" : false,
      "isUuid" : false,
      "isListContainer" : false,
      "isMapContainer" : false,
      "isEnum" : false,
      "isReadOnly" : false,
      "vendorExtensions" : {
        "x-swift-optional-scalar" : true
      },
      "hasValidation" : false,
      "isInherited" : false,
      "nameInCamelCase" : "Complete",
      "isXmlAttribute" : false,
      "isXmlWrapped" : false
    } ],

But the defaultValue parameter used in the mustache file doesn't seem to exist at all:

  {{/description}}public var {{name}}: {{{datatype}}}{{^required}}?{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{#objcCompatible}}{{#vendorExtensions.x-swift-optional-scalar}}

My only guess is that the defaultValue variable is no longer used or it's not being generated for some reason?

Thank you for any insight!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated Order.swift model and the Swift4 Mustache template containing the defaultValue reference. Reproduce generation with the documented swagger-codegen command and inspect the debugModels output for the complete property. Done means the generated Swift model applies the schema's false default value, with generation output matching the intended template behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.