yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong replacing string in variable server

ymhuang0808 opened this issue · comments

OpenAPI/Swagger provides Variable Server/Server Templating. In SwagGen, it also provides the feature. However, I found the replacing string may be wrong. Therefore, I would like to make sure that if it replaces the URL string with "variable value", instead of "variable name"

Actual

The line of code is:

url = url.replacingOccurrences(of: "{\({{variable.name}})}", with: {{variable.name}})

The generated code is:

public static func test(space: String = "main", version: String = "v1") -> String {
    var url = "https://test.petstore.com/{version}/{space}"
    url = url.replacingOccurrences(of: "{\(space)}", with: space)
    url = url.replacingOccurrences(of: "{\(version)}", with: version)
    return url
}

Expected

The line of code should look like:

url = url.replacingOccurrences(of: "{{{variable.name}}}", with: {{variable.name}})

The generated code should be:

public static func test(space: String = "main", version: String = "v1") -> String {
    var url = "https://test.petstore.com/{version}/{space}"
    url = url.replacingOccurrences(of: "{space}", with: space)
    url = url.replacingOccurrences(of: "{version}", with: version)
    return url
}

If the issue is confirmed, my teammate @marcuswu0814 would like to contribute it.

Yes, good catch! Feel free to submit a PR!

Hi @yonaskolb, currently I faced some problem when using Stencil, is it possible to escape { and } in this case:

// Stencil file
url = url.replacingOccurrences(of: "{{{variable.name}}}", with: {{variable.name}})

expected:

url = url.replacingOccurrences(of: "{version}", with: {{variable.name}})

Actual:

url = url.replacingOccurrences(of: "", with: {{variable.name}})

I can't found any document of escape character in Stencil web.

Yeah I've had this issue in the past. I've solved it before by doing something like this

"{" + "{{variable.name}}" + "}"

Hi @yonaskolb, thanks for your suggestion, the PR is done. 🚀