OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Swift] Number without specified type doesn't set x-swift-optional-scalar to true

Open
#6,614 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Swift Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

I need to generate obj-c accessible models and there is a problem in interoperability with some nullable types which cannot be represented in obj-c. Swift5ClientCodegen does support that but there is a problem if in OpenAPI declaration is missing format and only specify type. Then it's generated into Double? but postProcessModelProperty function does not trigger x-swift-optional-scalar to true. It can definitely be fixed on declaration side but client Codegen should expected it, too.

    @Override
    public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
        super.postProcessModelProperty(model, property);

        boolean isSwiftScalarType = property.isInteger || property.isLong || property.isFloat
                || property.isDouble || property.isBoolean;
        if ((!property.required || property.isNullable) && isSwiftScalarType) {
            // Optional scalar types like Int?, Int64?, Float?, Double?, and Bool?
            // do not translate to Objective-C. So we want to flag those
            // properties in case we want to put special code in the templates
            // which provide Objective-C compatibility.
            property.vendorExtensions.put("x-swift-optional-scalar", true);
        }
    }
openapi-generator version

4.3.1

OpenAPI declaration file content or url
    "GpsCoordinatesTO": {
      "type": "object",
      "properties": {
        "gpsX": {
          "type": "number",
          "format": "double"
        },
        "gpsY": {
          "type": "number"
        }
      },
      "title": "GpsCoordinatesTO"
    }

-->

Command line used for generation

openapi-generator generate -i swagger.json -g swift5 --additional-properties=library=urlsession,objcCompatible=true -o swift/ --skip-validate-spec

Steps to reproduce

Generate client from declaration which includes snippet above.

Suggest a fix

Update postProcessModelProperty function to handle only number because it's generated into Swift as Double? which cannot be represented in obj-c.

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 at Swift5ClientCodegen.postProcessModelProperty and reproduce the issue with the provided OpenAPI declaration and swift5 generation command. Confirm that a number without a format generates as Double? and that x-swift-optional-scalar is set, then verify the resulting Objective-C-compatible output with the relevant generator checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, swift
Domain
api, devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.