Skip to content

Commit

Permalink
fix @JsonTypeName with wrapped JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
ae-ae authored and frantuma committed Jul 12, 2022
1 parent 259355a commit 7c778d2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.annotation.JsonView;
Expand Down Expand Up @@ -1968,6 +1969,10 @@ protected Schema resolveWrapping(JavaType type, ModelConverterContext context, S
if (JsonTypeInfo.Id.CLASS.equals(id)) {
name = type.getRawClass().getName();
}
JsonTypeName typeName = type.getRawClass().getDeclaredAnnotation((JsonTypeName.class));
if (JsonTypeInfo.Id.NAME.equals(id) && typeName != null) {
name = typeName.value();
}
if(JsonTypeInfo.Id.NAME.equals(id) && name == null) {
name = type.getRawClass().getSimpleName();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.swagger.v3.core.resolving;

import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverterContextImpl;
import io.swagger.v3.core.jackson.ModelResolver;
import io.swagger.v3.core.matchers.SerializationMatchers;
import io.swagger.v3.core.resolving.resources.TestObject3699;
import org.testng.annotations.Test;

public class Ticket3699Test extends SwaggerTestBase {

@Test
public void test3699() {
final ModelResolver modelResolver = new ModelResolver(mapper());

ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
context.resolve(new AnnotatedType(TestObject3699.class));

SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "TestObject3699:\n" +
" type: object\n" +
" properties:\n" +
" CustomName:\n" +
" type: object\n" +
" properties:\n" +
" bar:\n" +
" type: string\n" +
" foo:\n" +
" type: array\n" +
" items:\n" +
" type: string\n");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.swagger.v3.core.resolving.resources;

import java.util.List;

@com.fasterxml.jackson.annotation.JsonTypeInfo(
use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME,
include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.WRAPPER_OBJECT)
@com.fasterxml.jackson.annotation.JsonTypeName("CustomName")
public class TestObject3699 {
public String bar;
public List<String> foo;
}

0 comments on commit 7c778d2

Please sign in to comment.