question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG][KOTLIN] NullPointerException generating model documentation for enum value named `null`

See original GitHub issue

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 generating a client for a 3rd party API I do not own. They are using nullable enum properties in their API, with null as a possible enum value.

The Kotlin generator is able to generate the model successfully, but the generator fails afterwards when generating the model documentation.

Resulting Model:

/**
 * Example
 *
 * Example API
 *
 * The version of the OpenAPI document: 1.0
 * 
 *
 * Please note:
 * This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * Do not edit this file manually.
 */

@file:Suppress(
    "ArrayInDataClass",
    "EnumEntryName",
    "RemoveRedundantQualifierName",
    "UnusedImport"
)

package org.openapitools.client.models


import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*

/**
 * An object
 *
 * @param id 
 * @param description The description.
 * @param color The color.
 */
@Serializable
data class Model (

    @SerialName(value = "id") @Required val id: kotlin.String,

    /* The description. */
    @SerialName(value = "description") @Required val description: kotlin.String?,

    /* The color. */
    @SerialName(value = "color") @Required val color: Model.Color?

) : kotlin.collections.HashMap<String, kotlin.String>() {

    /**
     * The color.
     *
     * Values: red,blue,green,`null`
     */
    @Serializable
    enum class Color(val value: kotlin.String) {
        @SerialName(value = "red") red("red"),
        @SerialName(value = "blue") blue("blue"),
        @SerialName(value = "green") green("green"),
        @SerialName(value = "null") `null`("null"); // nullable enum value handled here
    }
}

NullPointerException:

[main] INFO  o.o.codegen.DefaultGenerator - Generating with dryRun=false
[main] INFO  o.o.c.ignore.CodegenIgnoreProcessor - No .openapi-generator-ignore file found.
[main] INFO  o.o.codegen.DefaultGenerator - OpenAPI Generator: kotlin (client)
[main] INFO  o.o.codegen.DefaultGenerator - Generator 'kotlin' is considered stable.
[main] INFO  o.o.c.l.AbstractKotlinCodegen - Environment variable KOTLIN_POST_PROCESS_FILE not defined so the Kotlin code may not be properly formatted. To define it, try 'export KOTLIN_POST_PROCESS_FILE="/usr/local/bin/ktlint -F"' (Linux/Mac)
[main] INFO  o.o.c.l.AbstractKotlinCodegen - NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).
[main] INFO  o.o.codegen.TemplateManager - writing file /output/src/commonMain/kotlin/org/openapitools/client/models/Model.kt
Exception in thread "main" java.lang.RuntimeException: Could not generate model 'Model'
	at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:532)
	at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:888)
	at org.openapitools.codegen.cmd.Generate.execute(Generate.java:441)
	at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.NullPointerException: Null context for variable '.' on line 14
	at com.samskivert.mustache.Template.getValueIn(Template.java:288)
	at com.samskivert.mustache.Template.getValue(Template.java:224)
	at com.samskivert.mustache.Template.getValueOrDefault(Template.java:279)
	at com.samskivert.mustache.Mustache$VariableSegment.execute(Mustache.java:802)
	at com.samskivert.mustache.Mustache$BlockSegment.executeSegs(Mustache.java:845)
	at com.samskivert.mustache.Mustache$SectionSegment.execute(Mustache.java:866)
	at com.samskivert.mustache.Mustache$BlockSegment.executeSegs(Mustache.java:845)
	at com.samskivert.mustache.Mustache$SectionSegment.execute(Mustache.java:881)
	at com.samskivert.mustache.Mustache$BlockSegment.executeSegs(Mustache.java:845)
	at com.samskivert.mustache.Mustache$SectionSegment.execute(Mustache.java:870)
	at com.samskivert.mustache.Mustache$BlockSegment.executeSegs(Mustache.java:845)
	at com.samskivert.mustache.Mustache$SectionSegment.execute(Mustache.java:866)
	at com.samskivert.mustache.Template.executeSegs(Template.java:157)
	at com.samskivert.mustache.Mustache$IncludedTemplateSegment.execute(Mustache.java:774)
	at com.samskivert.mustache.Mustache$BlockSegment.executeSegs(Mustache.java:845)
	at com.samskivert.mustache.Mustache$InvertedSegment.execute(Mustache.java:910)
	at com.samskivert.mustache.Mustache$BlockSegment.executeSegs(Mustache.java:845)
	at com.samskivert.mustache.Mustache$SectionSegment.execute(Mustache.java:881)
	at com.samskivert.mustache.Mustache$BlockSegment.executeSegs(Mustache.java:845)
	at com.samskivert.mustache.Mustache$SectionSegment.execute(Mustache.java:866)
	at com.samskivert.mustache.Template.executeSegs(Template.java:157)
	at com.samskivert.mustache.Template.execute(Template.java:134)
	at com.samskivert.mustache.Template.execute(Template.java:125)
	at org.openapitools.codegen.templating.MustacheEngineAdapter.compileTemplate(MustacheEngineAdapter.java:65)
	at org.openapitools.codegen.TemplateManager.write(TemplateManager.java:163)
	at org.openapitools.codegen.DefaultGenerator.processTemplateToFile(DefaultGenerator.java:1034)
	at org.openapitools.codegen.DefaultGenerator.processTemplateToFile(DefaultGenerator.java:1021)
	at org.openapitools.codegen.DefaultGenerator.generateModelDocumentation(DefaultGenerator.java:375)
	at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:529)
	... 4 more
openapi-generator version

5.4.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Example
  version: '1.0'
  description: Example API
  contact:
    name: craigberry1
servers:
  - url: 'http://localhost:3000'
paths:
  '/models/{id}':
    parameters:
      - schema:
          type: string
        name: id
        in: path
        required: true
    get:
      summary: Your GET endpoint
      tags: []
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Model'
      operationId: get-models-id
      description: Gets a model by unique identifier
      parameters:
        - schema:
            type: string
          in: query
          name: id
          description: The unique model identifier
          required: true
components:
  schemas:
    Model:
      title: Model
      type: object
      additionalProperties: true
      description: An object
      properties:
        id:
          type: string
        description:
          type: string
          nullable: true
          description: The description.
        color:
          type: string
          nullable: true
          enum:
            - red
            - blue
            - green
            - null
          description: The color.
      required:
        - id
        - description
        - color
Generation Details
openapi-generator generate -i example.yaml -g kotlin --library multiplatform
Steps to reproduce
  1. Create model with enum property and null as a possible value for the enum.
  2. Generate kotlin/multiplatform client for the API.
  3. Observe NullPointerException after processing the model.
Related issues/PRs

I have not found related or open PR.

Suggest a fix

I’m guessing this has to do with parsing the null as a field name in the documentation. But I am not familiar with the doc generation to suggest a fix. FWIW this issue does not appear in the Java generators.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Marek00Malikcommented, Sep 21, 2022

Hey guys. I cannot guarantee but will try to jump on it maybe after the 3th of October.

1reaction
dvankleycommented, Sep 19, 2022

Encountered the same issue in 6.1.0. Can be worked around by deleting the null enum value in the list.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin: Accessing Enum Value generates nullpointerexception
So the super constructor (i.e. Product 's constructor) is called first, and then the discount member is initialized.
Read more >
Openapi enum null values - SmartBear Community
I want to have enums that can have null values. ... the specifications here https://swagger.io/docs/specification/data-models/enums/.
Read more >
enum — Support for enumerations — Python 3.11.1 ...
The enum class being called. value. The name of the new Enum to create. names. The names/values of the members for the new...
Read more >
Guide to EnumSet - Baeldung
In this tutorial, we'll explore the EnumSet collection from the java.util package and discuss its peculiarities.
Read more >
Enum.ToString Method (System) - Microsoft Learn
Otherwise, the return value is the string representation of the numeric value of this instance. For more information about formatting enumeration values, see ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found