Skip to content

Commit 375b2f5

Browse files
Jason Vossfrantuma
authored andcommitted
schema json object defaultValue
1 parent 2f81363 commit 375b2f5

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,10 +1520,15 @@ protected String resolveFormat(Annotated a, Annotation[] annotations, io.swagger
15201520
return null;
15211521
}
15221522

1523-
protected String resolveDefaultValue(Annotated a, Annotation[] annotations, io.swagger.v3.oas.annotations.media.Schema schema) {
1523+
protected Object resolveDefaultValue(Annotated a, Annotation[] annotations, io.swagger.v3.oas.annotations.media.Schema schema) {
15241524
if (schema != null) {
15251525
if (!schema.defaultValue().isEmpty()) {
1526-
return schema.defaultValue();
1526+
try {
1527+
ObjectMapper mapper = ObjectMapperFactory.buildStrictGenericObjectMapper();
1528+
return mapper.readTree(schema.defaultValue());
1529+
} catch (IOException e) {
1530+
return schema.defaultValue();
1531+
}
15271532
}
15281533
}
15291534
if (a == null) {
@@ -2005,8 +2010,8 @@ protected void resolveSchemaMembers(Schema schema, Annotated a, Annotation[] ann
20052010
if (StringUtils.isNotBlank(format) && StringUtils.isBlank(schema.getFormat())) {
20062011
schema.format(format);
20072012
}
2008-
String defaultValue = resolveDefaultValue(a, annotations, schemaAnnotation);
2009-
if (StringUtils.isNotBlank(defaultValue)) {
2013+
Object defaultValue = resolveDefaultValue(a, annotations, schemaAnnotation);
2014+
if (defaultValue != null) {
20102015
schema.setDefault(defaultValue);
20112016
}
20122017
Object example = resolveExample(a, annotations, schemaAnnotation);

modules/swagger-core/src/test/java/io/swagger/v3/core/converting/PojoTest.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,65 @@ public void setExampleJson(String exampleJson) {
482482

483483
}
484484

485+
/**
486+
* Test schema with json object defaultValue and example.
487+
*/
488+
@Test(description = "Shows how to provide model examples as json")
489+
public void testModelPropertyExampleDefaultJson() {
490+
491+
String json = "{\"ExampleJsonExtended\": {" +
492+
"\"type\":\"object\"," +
493+
"\"properties\":{" +
494+
"\"id\":{" +
495+
"\"type\":\"integer\"," +
496+
"\"format\":\"int32\"" +
497+
"}," +
498+
"\"idType\":{" +
499+
"\"type\":\"string\"" +
500+
"}," +
501+
"\"isActive\":{" +
502+
"\"type\":\"boolean\"" +
503+
"}" +
504+
"}," +
505+
"\"example\":{" +
506+
"\"id\":19877734," +
507+
"\"idType\":\"employee code\"," +
508+
"\"isActive\":false" +
509+
"}," +
510+
"\"default\":{" +
511+
"\"id\":19877734," +
512+
"\"idType\":\"employee code\"," +
513+
"\"isActive\":false" +
514+
"}" +
515+
"}," +
516+
"\"modelWithPropertyExampleDefaultJson\":{" +
517+
"\"type\":\"object\"," +
518+
"\"properties\":{" +
519+
"\"exampleJsonExtended\":{" +
520+
"\"$ref\":\"#/components/schemas/ExampleJsonExtended\"" +
521+
"}" +
522+
"}" +
523+
"}" +
524+
"}";
525+
SerializationMatchers.assertEqualsToJson(readAll(modelWithPropertyExampleDefaultJson.class), json);
526+
}
527+
528+
static class modelWithPropertyExampleDefaultJson {
529+
final String SAMPLE = "{\"id\": 19877734, \"idType\":\"employee code\", \"isActive\":false}";
530+
ExampleJsonExtended tester = new ExampleJsonExtended();
531+
532+
@Schema( example = SAMPLE, defaultValue = SAMPLE)
533+
private ExampleJsonExtended exampleJsonExtended;
534+
535+
public ExampleJsonExtended getExampleJsonExtended() {
536+
return exampleJsonExtended;
537+
}
538+
539+
public void setExampleJsonExtended(ExampleJsonExtended exampleJsonExtended) {
540+
this.exampleJsonExtended = exampleJsonExtended;
541+
}
542+
}
543+
485544
@Test(description = "Shows how to provide model examples as json")
486545
public void testModelPropertyStringExampleJson() {
487546

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

525584
}
526585

586+
static class ExampleJsonExtended {
587+
private int id;
588+
private String idType;
589+
private boolean isActive;
590+
591+
public int getId() {
592+
return id;
593+
}
594+
595+
public void setId(int id) {
596+
this.id = id;
597+
}
598+
599+
public String getIdType() {
600+
return idType;
601+
}
602+
603+
public void setIdType(String idType) {
604+
this.idType = idType;
605+
}
606+
607+
public boolean getIsActive() {
608+
return isActive;
609+
}
610+
611+
public void setIsActive(boolean isActive) {
612+
this.isActive = isActive;
613+
}
614+
615+
}
616+
527617
@Test(description = "Shows how to provide an example array")
528618
public void testExampleArray() {
529619

0 commit comments

Comments
 (0)