Skip to content

Commit 9e2593e

Browse files
committed
refs swagger-api/swagger-parser/issues/1757 - boolean schema support for OAS 3.1
1 parent 2c2980e commit 9e2593e

File tree

8 files changed

+136
-0
lines changed

8 files changed

+136
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public void serialize(
2929
Schema value, JsonGenerator jgen, SerializerProvider provider)
3030
throws IOException {
3131

32+
if (value.getBooleanSchemaValue() != null) {
33+
jgen.writeBoolean(value.getBooleanSchemaValue());
34+
return;
35+
}
3236
if (value.getExampleSetFlag() && value.getExample() == null) {
3337
jgen.writeStartObject();
3438
defaultSerializer.unwrappingSerializer(null).serialize(value, jgen, provider);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ public abstract class DateSchemaMixin {
1515

1616
@JsonIgnore
1717
public abstract Map<String, Object> getJsonSchema();
18+
19+
@JsonIgnore
20+
public abstract Boolean getBooleanSchemaValue();
1821
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public abstract class Schema31Mixin {
5959
@JsonIgnore
6060
public abstract Object getJsonSchemaImpl();
6161

62+
@JsonIgnore
63+
public abstract Boolean getBooleanSchemaValue();
64+
6265
public static class TypeSerializer extends JsonSerializer<Set<String>> {
6366

6467
@Override

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,7 @@ public abstract class SchemaMixin {
108108

109109
@JsonIgnore
110110
public abstract Object getConst();
111+
112+
@JsonIgnore
113+
public abstract Boolean getBooleanSchemaValue();
111114
}

modules/swagger-core/src/main/java/io/swagger/v3/core/util/ModelDeserializer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ private Schema deserializeObjectSchema(JsonNode node) {
125125
}
126126

127127
private Schema deserializeJsonSchema(JsonNode node) {
128+
if (node.isBoolean()) {
129+
return new Schema().booleanSchemaValue(node.booleanValue());
130+
}
128131
JsonNode additionalProperties = node.get("additionalProperties");
129132
JsonNode type = node.get("type");
130133
Schema schema = null;

modules/swagger-core/src/test/java/io/swagger/v3/core/deserialization/OpenAPI3_1DeserializationTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package io.swagger.v3.core.deserialization;
22

33
import io.swagger.v3.core.matchers.SerializationMatchers;
4+
import io.swagger.v3.core.util.Json;
5+
import io.swagger.v3.core.util.Json31;
46
import io.swagger.v3.core.util.ResourceUtils;
57
import io.swagger.v3.core.util.Yaml;
68
import io.swagger.v3.core.util.Yaml31;
9+
import io.swagger.v3.oas.models.Components;
710
import io.swagger.v3.oas.models.OpenAPI;
11+
import io.swagger.v3.oas.models.media.Schema;
812
import org.testng.annotations.Test;
913

1014
import java.io.IOException;
@@ -13,6 +17,7 @@
1317
import static org.testng.Assert.assertEquals;
1418
import static org.testng.Assert.assertFalse;
1519
import static org.testng.Assert.assertNotNull;
20+
import static org.testng.Assert.assertTrue;
1621

1722
public class OpenAPI3_1DeserializationTest {
1823

@@ -222,4 +227,29 @@ public void testRefDeserializationOnOAS31() throws IOException {
222227
assertEquals(openAPI.getPaths().get("/pets").getPost().getResponses().get("default").getHeaders().get("head").getDescription(), "ref header description");
223228

224229
}
230+
231+
@Test
232+
public void testBooleanSchemaDeserialization() throws Exception{
233+
OpenAPI openAPI = new OpenAPI()
234+
.openapi("3.1.0")
235+
.components(new Components().addSchemas("test", new Schema().booleanSchemaValue(true)));
236+
237+
String json = Json31.pretty(openAPI);
238+
String yaml = Yaml31.pretty(openAPI);
239+
OpenAPI oas = Json31.mapper().readValue(json, OpenAPI.class);
240+
assertTrue(Boolean.TRUE.equals(oas.getComponents().getSchemas().get("test").getBooleanSchemaValue()));
241+
Schema schema = Json31.mapper().readValue("true", Schema.class);
242+
assertTrue(Boolean.TRUE.equals(schema.getBooleanSchemaValue()));
243+
oas = Yaml31.mapper().readValue(yaml, OpenAPI.class);
244+
assertTrue(Boolean.TRUE.equals(oas.getComponents().getSchemas().get("test").getBooleanSchemaValue()));
245+
schema = Yaml31.mapper().readValue("true", Schema.class);
246+
assertTrue(Boolean.TRUE.equals(schema.getBooleanSchemaValue()));
247+
248+
json = Json.pretty(openAPI);
249+
yaml = Yaml.pretty(openAPI);
250+
oas = Json.mapper().readValue(json, OpenAPI.class);
251+
assertNull(oas.getComponents().getSchemas().get("test").getBooleanSchemaValue());
252+
oas = Yaml.mapper().readValue(yaml, OpenAPI.class);
253+
assertNull(oas.getComponents().getSchemas().get("test").getBooleanSchemaValue());
254+
}
225255
}

modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/OpenAPI3_1SerializationTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package io.swagger.v3.core.serialization;
22

33
import io.swagger.v3.core.matchers.SerializationMatchers;
4+
import io.swagger.v3.core.util.Json;
5+
import io.swagger.v3.core.util.Json31;
46
import io.swagger.v3.core.util.ResourceUtils;
7+
import io.swagger.v3.core.util.Yaml;
58
import io.swagger.v3.core.util.Yaml31;
69
import io.swagger.v3.oas.models.Components;
710
import io.swagger.v3.oas.models.OpenAPI;
@@ -1357,5 +1360,56 @@ public void testCallRefSerialization() {
13571360
"}");
13581361
}
13591362

1363+
@Test
1364+
public void testBooleanSchemaSerialization() {
1365+
OpenAPI openAPI = new OpenAPI()
1366+
.openapi("3.1.0")
1367+
.components(new Components().addSchemas("test", new Schema().booleanSchemaValue(true)));
1368+
1369+
System.out.println("--------- root ----------");
1370+
Json31.prettyPrint(openAPI);
1371+
assertEquals(Json31.pretty(openAPI), "{\n" +
1372+
" \"openapi\" : \"3.1.0\",\n" +
1373+
" \"components\" : {\n" +
1374+
" \"schemas\" : {\n" +
1375+
" \"test\" : true\n" +
1376+
" }\n" +
1377+
" }\n" +
1378+
"}");
1379+
System.out.println("--------- schema ----------");
1380+
Json31.prettyPrint(openAPI.getComponents().getSchemas().get("test"));
1381+
assertEquals(Json31.pretty(openAPI.getComponents().getSchemas().get("test")), "true");
1382+
System.out.println("--------- root YAML----------");
1383+
Yaml31.prettyPrint(openAPI);
1384+
assertEquals(Yaml31.pretty(openAPI), "openapi: 3.1.0\n" +
1385+
"components:\n" +
1386+
" schemas:\n" +
1387+
" test: true\n");
1388+
System.out.println("--------- schema YAML ----------");
1389+
Yaml31.prettyPrint(openAPI.getComponents().getSchemas().get("test"));
1390+
assertEquals(Yaml31.pretty(openAPI.getComponents().getSchemas().get("test")), "true\n");
1391+
System.out.println("--------- root 3.0 ----------");
1392+
Json.prettyPrint(openAPI);
1393+
assertEquals(Json.pretty(openAPI), "{\n" +
1394+
" \"openapi\" : \"3.1.0\",\n" +
1395+
" \"components\" : {\n" +
1396+
" \"schemas\" : {\n" +
1397+
" \"test\" : { }\n" +
1398+
" }\n" +
1399+
" }\n" +
1400+
"}");
1401+
System.out.println("--------- schema 3.0 ----------");
1402+
Json.prettyPrint(openAPI.getComponents().getSchemas().get("test"));
1403+
assertEquals(Json.pretty(openAPI.getComponents().getSchemas().get("test")), "{ }");
1404+
System.out.println("--------- root YAML 3.0 ----------");
1405+
Yaml.prettyPrint(openAPI);
1406+
assertEquals(Yaml.pretty(openAPI), "openapi: 3.1.0\n" +
1407+
"components:\n" +
1408+
" schemas:\n" +
1409+
" test: {}\n");
1410+
System.out.println("--------- schema YAML 3.0 ----------");
1411+
Yaml.prettyPrint(openAPI.getComponents().getSchemas().get("test"));
1412+
assertEquals(Yaml.pretty(openAPI.getComponents().getSchemas().get("test")), "{}\n");
1413+
}
13601414

13611415
}

modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/Schema.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@ public Schema specVersion(SpecVersion specVersion) {
245245
@OpenAPI31
246246
private List<T> examples;
247247

248+
/**
249+
* @since 2.2.2 (OpenAPI 3.1.0)
250+
*
251+
* when set, this represents a boolean schema value
252+
*/
253+
@OpenAPI31
254+
private Boolean booleanSchemaValue;
255+
248256
/**
249257
*
250258
* @since 2.2.0 (OpenAPI 3.1.0)
@@ -2124,5 +2132,33 @@ public Schema _const(Object _const) {
21242132
this._const = cast(_const);
21252133
return this;
21262134
}
2135+
2136+
/**
2137+
*
2138+
* @since 2.2.2 (OpenAPI 3.1.0)
2139+
*/
2140+
@OpenAPI31
2141+
public Boolean getBooleanSchemaValue() {
2142+
return booleanSchemaValue;
2143+
}
2144+
2145+
/**
2146+
*
2147+
* @since 2.2.2 (OpenAPI 3.1.0)
2148+
*/
2149+
@OpenAPI31
2150+
public void setBooleanSchemaValue(Boolean booleanSchemaValue) {
2151+
this.booleanSchemaValue = booleanSchemaValue;
2152+
}
2153+
2154+
/**
2155+
*
2156+
* @since 2.2.2 (OpenAPI 3.1.0)
2157+
*/
2158+
@OpenAPI31
2159+
public Schema booleanSchemaValue(Boolean booleanSchemaValue) {
2160+
this.booleanSchemaValue = booleanSchemaValue;
2161+
return this;
2162+
}
21272163
}
21282164

0 commit comments

Comments
 (0)