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

Fix @JsonUnwrapped does not copy required properties: Resolves #3879 #3880

Merged
merged 3 commits into from
Apr 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}