Skip to content

Commit

Permalink
schema json object defaultValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Voss authored and frantuma committed Dec 10, 2021
1 parent 2f81363 commit 375b2f5
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
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

0 comments on commit 375b2f5

Please sign in to comment.