Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Swift5 Generator Null Enum Errors #20724

Open
5 of 6 tasks
benkgoldberg opened this issue Feb 25, 2025 · 0 comments
Open
5 of 6 tasks

[BUG] Swift5 Generator Null Enum Errors #20724

benkgoldberg opened this issue Feb 25, 2025 · 0 comments

Comments

@benkgoldberg
Copy link

benkgoldberg commented Feb 25, 2025

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 {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant