Skip to content

[Java][SSE] Replacing ApiResponse reference by the corresponding ApiResponse object #17273

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

Closed

Conversation

axel7083
Copy link

@axel7083 axel7083 commented Dec 1, 2023

Fixes #17271

This PR is adding a test case that is currently failing in the current context.

The problems come from the following line which is directly getting the content of the ApiResponse, when it can and is allowed to be a reference. Thus leading to a Null Pointer Exception

This can be fixed, by replacing the ApiResponse reference by their corresponding ApiResponse in the processOperation method.

We cannot fetch the ApiResponse object from its reference in the method fromOperation, therefore we need to it before.

@axel7083
Copy link
Author

axel7083 commented Dec 1, 2023

CC @wing328

@axel7083 axel7083 changed the title test: adding test case failing when it should not [Java][SSE] adding test case failing when it should not Dec 1, 2023
@axel7083 axel7083 changed the title [Java][SSE] adding test case failing when it should not [Java][SSE] Replacing ApiResponse reference by the corresponding ApiResponse object Dec 1, 2023
operation.getResponses().put(key, ModelUtils.getReferencedApiResponse(openAPI, apiResponse));
}
}

Copy link
Member

@wing328 wing328 Dec 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@axel7083 thanks for the PR. Should the fix be done in the default codegen to get the referenced response component/schema correctly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, I think I might have fixed it in the latest master.

Can you please give it a try with the latest master?

Copy link
Author

@axel7083 axel7083 Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, sadly I get the same error.

  Exception: Cannot invoke "io.swagger.v3.oas.models.media.Content.get(Object)" because the return value of "io.swagger.v3.oas.models.responses.ApiResponse.getContent()" is null

	at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1340)
	at org.openapitools.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:1234)
	at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:608)
	at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:1065)
	at org.openapitools.codegen.java.spring.SpringCodegenTest.testSSEOperationSupport(SpringCodegenTest.java:3235)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:577)
	at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:135)
	at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:673)
	at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:220)
	at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
	at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:945)
	at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:193)
	at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
	at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at org.testng.TestRunner.privateRun(TestRunner.java:808)
	at org.testng.TestRunner.run(TestRunner.java:603)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:429)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:423)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:383)
	at org.testng.SuiteRunner.run(SuiteRunner.java:326)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1249)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
	at org.testng.TestNG.runSuites(TestNG.java:1092)
	at org.testng.TestNG.run(TestNG.java:1060)
	at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
	at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:105)
Caused by: java.lang.NullPointerException: Cannot invoke "io.swagger.v3.oas.models.media.Content.get(Object)" because the return value of "io.swagger.v3.oas.models.responses.ApiResponse.getContent()" is null
	at org.openapitools.codegen.languages.SpringCodegen.lambda$fromOperation$16(SpringCodegen.java:1280)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
	at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1845)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
	at org.openapitools.codegen.languages.SpringCodegen.fromOperation(SpringCodegen.java:1282)
	at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1308)
	... 29 more

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did a test with the following using the latest master

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g spring -i https://raw.githubusercontent.com/OpenAPITools/openapi-generator/2129b759a31bc1b22e84728053f5fed7fe137990/modules/openapi-generator/src/test/resources/3_0/sse.yaml -o /tmp/spring -c /tmp/config.json

and the output compiles without issues

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

happy to reopen this PR if needed. closing it for the time being

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot reopen

@axel7083 axel7083 force-pushed the 17271-adding-failing-case branch from 2129b75 to a51a0eb Compare December 4, 2023 09:32
@wing328 wing328 closed this Dec 13, 2023
@axel7083
Copy link
Author

@wing328 sorry for the late response, I am reopening since I cannot understand one major issue, which is the difference of output between the direct java command, and the tests method testSSEOperationSupport with the provided configuration.

Scenarios

You need to use the configurations from the section bellow to reproduce on master (17/12/2023)

When directly compiling the .jar and using with the sse.yaml file, we get the expected output.

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g spring -i modules/openapi-generator/src/test/resources/3_0/sse.yaml -o ./tmp/spring -c ./tmp/config.json

However, when using running the test method, we get the error I tried to fix in the PR. The problems seems to be, when running the test, the content of the operation do not seems to be normalised/format properly, the $ref is not replace by the corresponding content (as it should?).

mvn -Dorg.openapitools.codegen.java.spring.SpringCodegenTest#testSSEOperationSupport test

Here some interesting stuff I fund while comparing both with debug mode. When the generate method is called we can see a very distinct difference between the openAPI objects

image

The image does not show the full object, but focus on the difference, the $ref on the java method is already replaced by its corresponding value, while on the test method, is still a non-null $ref.

Configuration file

./tmp/config.json

{
  "library": "spring-boot",
  "interfaceOnly": "false",
  "serverSentEvents": "true",
  "delegatePattern": "true",
  "reactive": "true"
}

modules/openapi-generator/src/test/resources/3_0/sse.yaml

openapi: 3.0.1
info:
  title: SSE test
  description: SSE test cases
  version: 1.0.0
servers:
  - url: 'https'
tags:
  - name: sse
paths:
  /path/variant4:
    post:
      operationId: sseVariant4
      tags:
        - sse
      responses:
        '200':
          $ref: '#/components/responses/EventTypeResponse'
components:
  schemas:
    EventType:
      type: object
      properties:
        attribute1:
          type: string
  responses:
    EventTypeResponse:
      description: acknowledged
      content:
        text/event-stream:
          schema:
            type: array
            format: event-stream
            items:
              $ref: '#/components/schemas/EventType'

kgeilmann pushed a commit to kgeilmann/openapi-generator that referenced this pull request Feb 10, 2025
…nd instantiate the generator.

This fixes the different behavior between Test and CLI seen in OpenAPITools#17273 (comment)
kgeilmann added a commit to kgeilmann/openapi-generator that referenced this pull request Feb 14, 2025
…nd instantiate the generator.

This fixes the different behavior between Test and CLI seen in OpenAPITools#17273 (comment)
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 this pull request may close these issues.

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