OpenAPITools / OpenAPITools/openapi-generator

[BUG] Swift5 Generator Null Enum Errors

Open
#20,724 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I'm using this generator to generate Swift5 code for my frontend. The generator creates a NullEnum type that is unused and produces the Swift compiler error 'NullEnum' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized.

For context, I generate my YAMLs using drf-spectacular, which I'm using for the API on my server. drf-spectacular creates the NullEnum type and includes it on all enum fields where null=True is set in the Django model (see the example below for clarification).

openapi-generator version

I'm using openapi-generator-cli 7.11.0, commit hash a7240ee.

OpenAPI declaration file content or url

Here's the part of the YAML file that produces the null enum. In the Swift generated code, any enum that can be null will just be marked as optional—the NullEnum type is never used and does not need to be generated.

NullEnum:
  enum:
  - null
Generation Details

I typically use the generator installed with Homebrew, but using the latest OpenAPI Generator version (downloaded using the "build the project locally" instructions here reproduced the error).

Steps to reproduce
  1. Create an OpenAPI.yaml file using OpenAPI 3.0.3.
  2. Create a file name OpenAPI.yaml with the contents pasted below. I am using drf-spectacular to generate my schemas, and there are several instances where I have this pattern of the three enums (some enum, BlankEnum, and NullEnum).
openapi: 3.0.3
info:
  title: API
  version: 1.0.0
  description: API for mobile apps
paths:
  /api/schema/:
    get:
      operationId: api_schema_retrieve
      description: ''
      responses:
        '200':
          content:
            application/yaml:
              schema:
                type: object
                additionalProperties: {}
          description: ''
components:
  schemas:
    Arm:
      type: object
      properties:
        side:
          nullable: true
          oneOf:
          - $ref: '#/components/schemas/SideEnum'
          - $ref: '#/components/schemas/BlankEnum'
          - $ref: '#/components/schemas/NullEnum'
    SideEnum:
      enum:
      - Left
      - Right
      type: string
      description: |-
        * `Left` - Left
        * `Right` - Right
    NullEnum:
      enum:
      - null
    BlankEnum:
      enum:
      - ''
  1. Run openapi-generator generate -i path/to/yaml/OpenAPI.yaml -g swift5 -o /path/to/output/TestAPI in the command line.

Looking at the generated code, the NullEnum type is defined but never used. Here is the definition you should see:

public enum NullEnum: String, Codable, CaseIterable {
}

This type generates the Swift compiler error, 'NullEnum' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized.

I can solve the error by commenting this code out or deleting the file, but it seems like we don't need to generate NullEnum at all.

Related issues/PRs

I couldn't find a similar issue with the Swift5 generator.

Suggest a fix

Not 100% sure where this NullEnum enum generation occurs, but NullEnum may not be needed for Swift code to function. I just comment it out every time I generate the schema. These errors also go away if NullEnum does not declare raw type String or conform to Codable. Here's an updated NullEnum definition that will compile:

public enum NullEnum: CaseIterable {
}

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

Reproduce the issue with the provided OpenAPI.yaml and the openapi-generator generate -i path/to/yaml/OpenAPI.yaml -g swift5 command. Start at the Swift5 generator's enum generation path and inspect the generated NullEnum definition; done means the sample output compiles without the reported error and existing generator checks remain valid.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.