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

Issue 4041 schema json object defaultValue #4063

Merged
merged 2 commits into from
Dec 10, 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 @@ -1520,10 +1520,15 @@ protected String resolveFormat(Annotated a, Annotation[] annotations, io.swagger
return null;
}

protected String resolveDefaultValue(Annotated a, Annotation[] annotations, io.swagger.v3.oas.annotations.media.Schema schema) {
protected Object resolveDefaultValue(Annotated a, Annotation[] annotations, io.swagger.v3.oas.annotations.media.Schema schema) {
if (schema != null) {
if (!schema.defaultValue().isEmpty()) {
return schema.defaultValue();
try {
ObjectMapper mapper = ObjectMapperFactory.buildStrictGenericObjectMapper();
return mapper.readTree(schema.defaultValue());
} catch (IOException e) {
return schema.defaultValue();
}
}
}
if (a == null) {
Expand Down Expand Up @@ -2005,8 +2010,8 @@ protected void resolveSchemaMembers(Schema schema, Annotated a, Annotation[] ann
if (StringUtils.isNotBlank(format) && StringUtils.isBlank(schema.getFormat())) {
schema.format(format);
}
String defaultValue = resolveDefaultValue(a, annotations, schemaAnnotation);
if (StringUtils.isNotBlank(defaultValue)) {
Object defaultValue = resolveDefaultValue(a, annotations, schemaAnnotation);
if (defaultValue != null) {
schema.setDefault(defaultValue);
}
Object example = resolveExample(a, annotations, schemaAnnotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,65 @@ public void setExampleJson(String exampleJson) {

}

/**
* Test schema with json object defaultValue and example.
*/
@Test(description = "Shows how to provide model examples as json")
public void testModelPropertyExampleDefaultJson() {

String json = "{\"ExampleJsonExtended\": {" +
"\"type\":\"object\"," +
"\"properties\":{" +
"\"id\":{" +
"\"type\":\"integer\"," +
"\"format\":\"int32\"" +
"}," +
"\"idType\":{" +
"\"type\":\"string\"" +
"}," +
"\"isActive\":{" +
"\"type\":\"boolean\"" +
"}" +
"}," +
"\"example\":{" +
"\"id\":19877734," +
"\"idType\":\"employee code\"," +
"\"isActive\":false" +
"}," +
"\"default\":{" +
"\"id\":19877734," +
"\"idType\":\"employee code\"," +
"\"isActive\":false" +
"}" +
"}," +
"\"modelWithPropertyExampleDefaultJson\":{" +
"\"type\":\"object\"," +
"\"properties\":{" +
"\"exampleJsonExtended\":{" +
"\"$ref\":\"#/components/schemas/ExampleJsonExtended\"" +
"}" +
"}" +
"}" +
"}";
SerializationMatchers.assertEqualsToJson(readAll(modelWithPropertyExampleDefaultJson.class), json);
}

static class modelWithPropertyExampleDefaultJson {
final String SAMPLE = "{\"id\": 19877734, \"idType\":\"employee code\", \"isActive\":false}";
ExampleJsonExtended tester = new ExampleJsonExtended();

@Schema( example = SAMPLE, defaultValue = SAMPLE)
private ExampleJsonExtended exampleJsonExtended;

public ExampleJsonExtended getExampleJsonExtended() {
return exampleJsonExtended;
}

public void setExampleJsonExtended(ExampleJsonExtended exampleJsonExtended) {
this.exampleJsonExtended = exampleJsonExtended;
}
}

@Test(description = "Shows how to provide model examples as json")
public void testModelPropertyStringExampleJson() {

Expand Down Expand Up @@ -524,6 +583,37 @@ public void setId(String id) {

}

static class ExampleJsonExtended {
private int id;
private String idType;
private boolean isActive;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getIdType() {
return idType;
}

public void setIdType(String idType) {
this.idType = idType;
}

public boolean getIsActive() {
return isActive;
}

public void setIsActive(boolean isActive) {
this.isActive = isActive;
}

}

@Test(description = "Shows how to provide an example array")
public void testExampleArray() {

Expand Down