From 52217545455908c584132212606a67f28c018307 Mon Sep 17 00:00:00 2001 From: James Bodkin Date: Fri, 14 Jun 2024 15:50:35 +0100 Subject: [PATCH] Ignore @JsonBackReference when resolving the model --- .../v3/core/jackson/ModelResolver.java | 3 +- .../core/resolving/JsonBackReferenceTest.java | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/JsonBackReferenceTest.java diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java index 73437bb660..1b76512f88 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java @@ -1,5 +1,6 @@ package io.swagger.v3.core.jackson; +import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonIdentityReference; import com.fasterxml.jackson.annotation.JsonIgnore; @@ -1178,7 +1179,7 @@ protected boolean ignore(final Annotated member, final XmlAccessorType xmlAccess if (propertiesToIgnore.contains(propName)) { return true; } - if (member.hasAnnotation(JsonIgnore.class) && member.getAnnotation(JsonIgnore.class).value()) { + if ((member.hasAnnotation(JsonIgnore.class) && member.getAnnotation(JsonIgnore.class).value()) || member.hasAnnotation(JsonBackReference.class)) { return true; } if (hasHiddenAnnotation(member)) { diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/JsonBackReferenceTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/JsonBackReferenceTest.java new file mode 100644 index 0000000000..16e8bbec54 --- /dev/null +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/JsonBackReferenceTest.java @@ -0,0 +1,43 @@ +package io.swagger.v3.core.resolving; + +import com.fasterxml.jackson.annotation.JsonBackReference; +import io.swagger.v3.core.converter.ModelConverters; +import io.swagger.v3.oas.models.media.Schema; +import org.testng.annotations.Test; + +import java.util.List; +import java.util.Map; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; + +public class JsonBackReferenceTest { + + @Test(description = "it should ignore a reference field") + public void testReferenceField() { + final Map models = ModelConverters.getInstance().readAll(Parent.class); + + final Schema parent = models.get("Parent"); + assertNotNull(parent); + assertEquals(parent.getProperties().size(), 1); + + final Schema child = models.get("Child"); + assertNotNull(child); + assertNull(child.getProperties()); + } + + static class Parent { + + public List children = null; + + } + + static class Child { + + @JsonBackReference + public Parent parent = null; + + } + +}