OpenAPITools / OpenAPITools/openapi-generator

[BUG] Using $ref for schema definition for server sent event (SEE) though spring-boot generator raise null pointer exception

Open
#17,271 1 comment 2 reactions 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
openapi-generator version

We are using gradle plugin version 7.1.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  description: apis
  version: 1.0.0
  title: debug-apis

paths:
  /stream:
    get:
      summary: Get the notifications stream
      operationId: getNotificationsStream
      responses:
        "200":
          $ref: "#/components/responses/NotificationStream"

components:
  schemas:
    Notification:
      type: object
      properties:
        message:
          type: string

    NotificationArray:
      type: array
      format: event-stream
      items:
        $ref: '#/components/schemas/Notification'

  responses:
    NotificationStream:
      description: notification stream
      content:
        text/event-stream:
          schema:
            $ref: '#/components/schemas/NotificationArray'
Generation Details

The configuration file used, you can make the generation work by changing serverSentEvents from true to false.

{
  "library": "spring-boot",
  "apiPackage": "com.example.api",
  "invokerPackage": "com.example.invoker",
  "modelPackage": "com.example.model",
  "hideGenerationTimestamp": "true",
  "generatePom": "false",
  "title": "debug-title",
  "interfaceOnly": "true",
  "serverSentEvents": "true",
  "delegatePattern": "true",
  "reactive": "true",
  "unhandledException": "true",
  "dateLibrary": "java8",
  "useSpringBoot3": "true"
}

Steps to reproduce

See https://github.com/OpenAPITools/openapi-generator/pull/17273 for test use case failing

Schemas leading to issue

The schema above produces

Exception: element cannot be mapped to a null key
at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1269)
at org.openapitools.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:1160)
at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:609)
at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:994)
at org.openapitools.generator.gradle.plugin.tasks.GenerateTask.doWork(GenerateTask.kt:925)
... 122 more
Caused by: java.lang.NullPointerException: element cannot be mapped to a null key
at org.openapitools.codegen.languages.SpringCodegen.fromOperation(SpringCodegen.java:1288)
at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1237)
... 126 more

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/pull/16543

Suggest a fix

If we place the schema definition directly in the response it works.

....
  responses:
    NotificationStream:
      description: notification stream
      content:
        text/event-stream:
          schema:
            type: array
            format: event-stream
            items:
              $ref: '#/components/schemas/Notification'

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

Start with SpringCodegen.fromOperation at the stack-trace location and compare the failing schema in the issue with the test use case referenced by PR #17273. Run the spring-boot generator with serverSentEvents enabled and the supplied OpenAPI document. Done means generation no longer raises the null-key exception for the referenced response schema.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring-boot
Domain
api, backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.