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

Nullable enums cause NPE #357

Open
xehmer opened this issue Jan 31, 2025 · 4 comments · May be fixed by #360
Open

Nullable enums cause NPE #357

xehmer opened this issue Jan 31, 2025 · 4 comments · May be fixed by #360

Comments

@xehmer
Copy link

xehmer commented Jan 31, 2025

Hello,
I was trying to generate the client and model code for the German Brightsky API: https://brightsky.dev/docs/

Their OpenAPI Spec works flawlessly in the Swagger Editor, but when trying to generate the code with fabrikt, it simply fails due to

Caused by: java.lang.NullPointerException: Cannot invoke "Object.toString()" because "it" is null
	at com.cjbooms.fabrikt.util.KaizenParserExtensions.getEnumValues(KaizenParserExtensions.kt:94)
	at com.cjbooms.fabrikt.model.KotlinTypeInfo$Companion.from(KotlinTypeInfo.kt:96)
	at com.cjbooms.fabrikt.model.PropertyInfo$Field.<init>(PropertyInfo.kt:170)
	at com.cjbooms.fabrikt.model.PropertyInfo$Companion.getInLinedProperties(PropertyInfo.kt:127)
	at com.cjbooms.fabrikt.model.PropertyInfo$Companion.topLevelProperties(PropertyInfo.kt:57)
	at com.cjbooms.fabrikt.generators.model.ModelGenerator.createModels(ModelGenerator.kt:179)
	at com.cjbooms.fabrikt.generators.model.ModelGenerator.generate(ModelGenerator.kt:162)
	at com.cjbooms.fabrikt.cli.CodeGenerator.models(CodeGenerator.kt:65)
	at com.cjbooms.fabrikt.cli.CodeGenerator.generateClient(CodeGenerator.kt:55)
	at com.cjbooms.fabrikt.cli.CodeGenerator.generateCode(CodeGenerator.kt:36)
	at com.cjbooms.fabrikt.cli.CodeGenerator.generate(CodeGenerator.kt:32)
	at com.cjbooms.fabrikt.cli.CodeGen.generate(CodeGen.kt:84)
	at com.cjbooms.fabrikt.cli.CodeGen.main(CodeGen.kt:22)
	at ch.acanda.gradle.fabrikt.generator.FabriktGeneratorKt.generate(FabriktGenerator.kt:18)
	... 119 more

When I removed all null values from all the enums in the YAML it worked.

A quick debugging session showed that the issue is there:

this.hasEnums() -> this.enums.map { it.toString() }.filterNot { it.isBlank() }

So that toString() call there needs to use the correct extension function for Any?, and not the member method of Any. I don't know if that is enough, though.

BUT: Even after removing all nullable enums, the generated client code (for OkHTTP) did not compile still. It tried to provide a List<Int> to a function that only accepts List<String>. That is the case for all the source_id parameters in the API spec.

PS: I was using a Gradle build with the latest version 1.11.0 of the plugin, which in turn uses fabrikt 20.0.0.

@cjbooms
Copy link
Owner

cjbooms commented Jan 31, 2025

Can you provide a minimal API spec that causes the issue(s). I might get a chance to look at this this weekend

@xehmer
Copy link
Author

xehmer commented Jan 31, 2025

Yeah I can try to fiddle something together later this evening, at least with the enum issue.

@xehmer
Copy link
Author

xehmer commented Jan 31, 2025

This is what ChatGPT gave me, with only minor tweaking on my part to make both errors occur (nullable enum causing the whole generation to fail, and the List<Int> not being supported as query parameter):

openapi: 3.0.3
info:
  title: Nullable Enum Demo API
  version: 1.0.0
paths:
  /status:
    get:
      summary: Get the status of a resource
      operationId: getStatus
      parameters:
        - name: values
          in: query
          description: A comma-separated list of integer values
          required: false
          schema:
            type: array
            items:
              type: integer
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/DemoResponse'
components:
  schemas:
    DemoResponse:
      type: object
      properties:
        status:
          type: string
          nullable: true
          enum: [ "active", "inactive", "pending", null ]

According to the OpenAPI documentation/specification it's essential that both nullable: true as well as adding null to the enum list are given (see here).

@xehmer xehmer changed the title Nullable enums cause NPE Nullable enums cause NPE / List<Int> not supported as query parameter Jan 31, 2025
@xehmer xehmer changed the title Nullable enums cause NPE / List<Int> not supported as query parameter Nullable enums cause NPE + List<Int> not supported as query parameter Jan 31, 2025
@xehmer
Copy link
Author

xehmer commented Jan 31, 2025

Ah I just saw #237, so the integer list query param story is apparently already known.

@xehmer xehmer changed the title Nullable enums cause NPE + List<Int> not supported as query parameter Nullable enums cause NPE Jan 31, 2025
@xehmer xehmer linked a pull request Feb 3, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants