openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open craigberry1 opened this issue 2 years ago • 4 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] 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.

craigberry1 avatar Feb 16 '22 16:02 craigberry1

what is the status of this issue?

Marek00Malik avatar Jul 12 '22 06:07 Marek00Malik

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

dvankley avatar Sep 19 '22 23:09 dvankley

@Marek00Malik I don't think anyone has started working on a fix. Would you have time to contribute a fix?

wing328 avatar Sep 20 '22 03:09 wing328

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

Marek00Malik avatar Sep 21 '22 09:09 Marek00Malik

Any progress? 👀

I got the same issue when I tried to generate my own client for GitHub API (I know there is existing GitHub java library, but I am just using GitHub OpenAPI spec to practice using this openapi-generator)

CXwudi avatar Nov 28 '22 17:11 CXwudi

Encountered the same issue. Any update?

drachim-dev avatar Feb 08 '23 12:02 drachim-dev

Would anyone like to sponsor @Marek00Malik to come up with a fix with a higher priority?

wing328 avatar Feb 08 '23 13:02 wing328

no longer seeing the same issue with the latest v7.0.0 so closing this.

wing328 avatar Sep 04 '23 15:09 wing328