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

Class with cyclic reference and non property id results in NPE #3453

Closed
xardif opened this issue Feb 21, 2020 · 4 comments
Closed

Class with cyclic reference and non property id results in NPE #3453

xardif opened this issue Feb 21, 2020 · 4 comments

Comments

@xardif
Copy link

xardif commented Feb 21, 2020

Creating class as below and returning it from an API results in exception (at the bottom of the issue).

@JsonIdentityInfo( generator = ObjectIdGenerators.UUIDGenerator.class )
public class ModelWithJsonIdentityCyclicUUID
{
    public ModelWithJsonIdentityCyclicUUID parent;

}

I have tried using @JsonManagedReference, but this does not result in anything.
The only workaround is to migrate to ObjectIdGenerators.PropertyGenerator and create additional property in model as below:

@JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class ModelWithJsonIdentityCyclicProperty
{
    public String id;
    public ModelWithJsonIdentityCyclicProperty parent;

}

The above classes can be used to extend test cases in io.swagger.v3.jaxrs2.resources.JsonIdentityCyclicResource.

The exception:

java.lang.NullPointerException
	at io.swagger.v3.core.jackson.ModelResolver$GeneratorWrapper.process(ModelResolver.java:1116)
	at io.swagger.v3.core.jackson.ModelResolver$GeneratorWrapper.access$100(ModelResolver.java:981)
	at io.swagger.v3.core.jackson.ModelResolver$GeneratorWrapper$3.processAsProperty(ModelResolver.java:1051)
	at io.swagger.v3.core.jackson.ModelResolver$GeneratorWrapper.processJsonIdentity(ModelResolver.java:1098)
	at io.swagger.v3.core.jackson.ModelResolver.resolve(ModelResolver.java:295)
	at io.swagger.v3.core.converter.ModelConverterContextImpl.resolve(ModelConverterContextImpl.java:90)
	at io.swagger.v3.core.converter.ModelConverters.resolveAsResolvedSchema(ModelConverters.java:112)
	at io.swagger.v3.core.util.ParameterProcessor.applyAnnotations(ParameterProcessor.java:62)
	at io.swagger.v3.jaxrs2.DefaultParameterExtension.extractParameters(DefaultParameterExtension.java:116)
	at io.swagger.v3.jaxrs2.Reader.getParameters(Reader.java:1282)
	at io.swagger.v3.jaxrs2.Reader.read(Reader.java:514)
	at io.swagger.v3.jaxrs2.Reader.read(Reader.java:121)
	at io.swagger.v3.jaxrs2.JsonIdentityTest.testJsonIdentityCyclic(JsonIdentityTest.java:23)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
	at org.testng.TestRunner.privateRun(TestRunner.java:648)
	at org.testng.TestRunner.run(TestRunner.java:505)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
	at org.testng.SuiteRunner.run(SuiteRunner.java:364)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
	at org.testng.TestNG.runSuites(TestNG.java:1049)
	at org.testng.TestNG.run(TestNG.java:1017)
	at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
	at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110)
@xardif
Copy link
Author

xardif commented Apr 2, 2020

There is also another workaround - using com.fasterxml.jackson.annotation.ObjectIdGenerators.StringIdGenerator somehow bypasses mechanism that causes exception and works flawlessly.

So the sample class looks as below:

@JsonIdentityInfo( generator = ObjectIdGenerators.StringIdGenerator.class )
public class ModelWithJsonIdentityCyclicUUIDString
{
    public ModelWithJsonIdentityCyclicUUIDString parent;

}

@daniel-schel
Copy link
Contributor

I have created a fix for this issue in the following PR: #3908

Unfortunately, I had found this issue later and also opened my own: #3907

@daniel-schel
Copy link
Contributor

daniel-schel commented Mar 9, 2021

@xardif: The ObjectIdGenerators.StringIdGenerator works, because it is not taken into consideration yet, which is another bug from my point of view.

@frantuma
Copy link
Member

Closing as fixed by #3908

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

No branches or pull requests

3 participants