Skip to content

[scala] client escapes terms which may be common in models, with no serialization fixes #6393

@jimschubert

Description

@jimschubert
Description

The Scala client codegen escapes terms which may be fairly common in response models (path, lazy, match, type). These are prefixed with an underscore with no serialization fix to account for the escaped value.

This is probably an issue in other generators as well.

Swagger-codegen version

2.2.3

Swagger declaration file content or url
{
    "swagger": "2.0",
    "info": {
        "version": "1.0.0",
        "title": "Reserved Words Issue",
        "description": "A sample of reserved words being escaped and breaking serialization",
        "termsOfService": "http://swagger.io/terms/",
        "contact": {
            "name": "Jim Schubert"
        },
        "license": {
            "name": "MIT"
        }
    },
    "host": "example.com",
    "basePath": "/api",
    "schemes": [
        "http"
    ],
    "consumes": [
        "application/json"
    ],
    "produces": [
        "application/json"
    ],
    "paths": {
        "/hi": {
            "get": {
                "description": "Returns all examples",
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "200": {
                        "description": "A list of examples.",
                        "schema": {
                            "type": "array",
                            "items": {
                                "$ref": "#/definitions/Example"
                            }
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "Example": {
            "type": "object",
            "required": [
                "id",
                "name",
                "path"
            ],
            "properties": {
                "id": {
                    "type": "integer",
                    "format": "int64"
                },
                "name": {
                    "type": "string"
                },
                "path": {
                    "type": "string"
                },
                "lazy": {
                    "type": "boolean"
                },
                "match": {
                    "type": "integer",
                    "format": "int64"
                },
                "type": {
                    "type": "string"
                }
            }
        }
    }
}
Command line used for generation
swagger-codegen generate -l scala -i swagger.json -o example
Steps to reproduce
  1. Save above spec as swagger.json
  2. Run above command to generate Scala client
  3. Inspect the model (cat example/src/main/scala/io/swagger/client/model/Example.scala):
package io.swagger.client.model


case class Example (
  id: Long,
  name: String,
  _path: String,
  _lazy: Option[Boolean],
  _match: Option[Long],
  _type: Option[String]
)
Related issues/PRs
Suggest a fix/enhancement

Scala allows these reserved words to be escaped in code. Model properties requiring escaping can easily be done like so:

case class Example (
  id: Long,
  name: String,
  `path`: String,
  `lazy`: Option[Boolean],
  `match`: Option[Long],
  `type`: Option[String]
)

Another option would be to annotate each escaped property with the unescaped property name, e.g. @JsonProperty("path"). However, this assumes only JSON serialization. This shouldn't be a huge issue, considering the ApiInvoker for the Scala generator assumes JSON serialization.

I'd imagine the escapes would be a better solution, especially for anyone who may only be generating models without API implementations.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions