Skip to content

Commit 7c778d2

Browse files
ae-aefrantuma
authored andcommitted
fix @JsonTypeName with wrapped JSON
1 parent 259355a commit 7c778d2

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
77
import com.fasterxml.jackson.annotation.JsonProperty;
88
import com.fasterxml.jackson.annotation.JsonTypeInfo;
9+
import com.fasterxml.jackson.annotation.JsonTypeName;
910
import com.fasterxml.jackson.annotation.JsonUnwrapped;
1011
import com.fasterxml.jackson.annotation.JsonValue;
1112
import com.fasterxml.jackson.annotation.JsonView;
@@ -1968,6 +1969,10 @@ protected Schema resolveWrapping(JavaType type, ModelConverterContext context, S
19681969
if (JsonTypeInfo.Id.CLASS.equals(id)) {
19691970
name = type.getRawClass().getName();
19701971
}
1972+
JsonTypeName typeName = type.getRawClass().getDeclaredAnnotation((JsonTypeName.class));
1973+
if (JsonTypeInfo.Id.NAME.equals(id) && typeName != null) {
1974+
name = typeName.value();
1975+
}
19711976
if(JsonTypeInfo.Id.NAME.equals(id) && name == null) {
19721977
name = type.getRawClass().getSimpleName();
19731978
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.swagger.v3.core.resolving;
2+
3+
import io.swagger.v3.core.converter.AnnotatedType;
4+
import io.swagger.v3.core.converter.ModelConverterContextImpl;
5+
import io.swagger.v3.core.jackson.ModelResolver;
6+
import io.swagger.v3.core.matchers.SerializationMatchers;
7+
import io.swagger.v3.core.resolving.resources.TestObject3699;
8+
import org.testng.annotations.Test;
9+
10+
public class Ticket3699Test extends SwaggerTestBase {
11+
12+
@Test
13+
public void test3699() {
14+
final ModelResolver modelResolver = new ModelResolver(mapper());
15+
16+
ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
17+
context.resolve(new AnnotatedType(TestObject3699.class));
18+
19+
SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "TestObject3699:\n" +
20+
" type: object\n" +
21+
" properties:\n" +
22+
" CustomName:\n" +
23+
" type: object\n" +
24+
" properties:\n" +
25+
" bar:\n" +
26+
" type: string\n" +
27+
" foo:\n" +
28+
" type: array\n" +
29+
" items:\n" +
30+
" type: string\n");
31+
}
32+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.swagger.v3.core.resolving.resources;
2+
3+
import java.util.List;
4+
5+
@com.fasterxml.jackson.annotation.JsonTypeInfo(
6+
use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME,
7+
include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.WRAPPER_OBJECT)
8+
@com.fasterxml.jackson.annotation.JsonTypeName("CustomName")
9+
public class TestObject3699 {
10+
public String bar;
11+
public List<String> foo;
12+
}

0 commit comments

Comments
 (0)