Skip to content

Commit

Permalink
Resolves #3879
Browse files Browse the repository at this point in the history
Fix @JsonUnwrapped does not copy required properties
  • Loading branch information
twobiers authored and frantuma committed Apr 18, 2021
1 parent ea4db2c commit 56d233b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context

boolean isPrimitive = false;
Schema model = null;
List<String> requiredProps = new ArrayList<>();

if (annotatedType == null) {
return null;
Expand Down Expand Up @@ -644,7 +645,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
.ctxAnnotations(null)
.jsonUnwrappedHandler(null)
.resolveAsRef(false);
handleUnwrapped(props, context.resolve(t), uw.prefix(), uw.suffix());
handleUnwrapped(props, context.resolve(t), uw.prefix(), uw.suffix(), requiredProps);
return null;
} else {
return new Schema();
Expand Down Expand Up @@ -709,6 +710,9 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
}
if (modelProps.size() > 0) {
model.setProperties(modelProps);
for(String propName : requiredProps) {
addRequiredItem(model, propName);
}
}

/**
Expand Down Expand Up @@ -1021,10 +1025,14 @@ protected boolean ignore(final Annotated member, final XmlAccessorType xmlAccess
return false;
}

private void handleUnwrapped(List<Schema> props, Schema innerModel, String prefix, String suffix) {
private void handleUnwrapped(List<Schema> props, Schema innerModel, String prefix, String suffix, List<String> requiredProps) {
if (StringUtils.isBlank(suffix) && StringUtils.isBlank(prefix)) {
if (innerModel.getProperties() != null) {
props.addAll(innerModel.getProperties().values());
if(innerModel.getRequired() != null) {
requiredProps.addAll(innerModel.getRequired());
}

}
} else {
if (prefix == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.swagger.v3.core.resolving;

import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.matchers.SerializationMatchers;
import io.swagger.v3.core.resolving.resources.JacksonUnwrappedRequiredProperty;
import org.testng.annotations.Test;

public class JacksonJsonUnwrappedTest {

@Test(description = "test the @JsonUnwrapped behaviour when required Properties")
public void jacksonJsonUnwrappedTest() {

SerializationMatchers
.assertEqualsToYaml(ModelConverters.getInstance().read(
JacksonUnwrappedRequiredProperty.class), "InnerTypeRequired:\n" +
" required:\n" +
" - name\n" +
" type: object\n" +
" properties:\n" +
" foo:\n" +
" type: integer\n" +
" format: int32\n" +
" name:\n" +
" type: string\n" +
"JacksonUnwrappedRequiredProperty:\n" +
" required:\n" +
" - name\n" +
" type: object\n" +
" properties:\n" +
" foo:\n" +
" type: integer\n" +
" format: int32\n" +
" name:\n" +
" type: string\n");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.swagger.v3.core.resolving.resources;

import io.swagger.v3.oas.annotations.media.Schema;

public class InnerTypeRequired {
public int foo;
@Schema(required = true)
public String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.swagger.v3.core.resolving.resources;

import com.fasterxml.jackson.annotation.JsonUnwrapped;

public class JacksonUnwrappedRequiredProperty {
@JsonUnwrapped private final InnerTypeRequired innerType;

public JacksonUnwrappedRequiredProperty(InnerTypeRequired innerType) {
this.innerType = innerType;
}

public InnerTypeRequired getInnerType() {
return innerType;
}
}

0 comments on commit 56d233b

Please sign in to comment.