Skip to content

Commit 76d1fd4

Browse files
committed
added Schema#copy
1 parent 2f72d4e commit 76d1fd4

File tree

12 files changed

+97
-5
lines changed

12 files changed

+97
-5
lines changed

src/main/java/org/dynapi/openapispec/core/schema/AllOf.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@ protected JSONObject finalized() {
2727
return new JSONObject()
2828
.put("allOf", new JSONArray(subSchemas.stream().map(Schema::getOpenApiSpec).toList()));
2929
}
30+
31+
@Override
32+
public AllOf copy() {
33+
AllOf copy = new AllOf();
34+
copy.options.putAll(this.options);
35+
copy.subSchemas.addAll(this.subSchemas);
36+
return copy;
37+
}
3038
}

src/main/java/org/dynapi/openapispec/core/schema/AnyOf.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@ protected JSONObject finalized() {
2727
return new JSONObject()
2828
.put("anyOf", new JSONArray(subSchemas.stream().map(Schema::getOpenApiSpec).toList()));
2929
}
30+
31+
@Override
32+
public AnyOf copy() {
33+
AnyOf copy = new AnyOf();
34+
copy.options.putAll(this.options);
35+
copy.subSchemas.addAll(this.subSchemas);
36+
return copy;
37+
}
3038
}

src/main/java/org/dynapi/openapispec/core/schema/OneOf.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@ protected JSONObject finalized() {
2727
return new JSONObject()
2828
.put("oneOf", new JSONArray(subSchemas.stream().map(Schema::getOpenApiSpec).toList()));
2929
}
30+
31+
@Override
32+
public OneOf copy() {
33+
OneOf copy = new OneOf();
34+
copy.options.putAll(this.options);
35+
copy.subSchemas.addAll(this.subSchemas);
36+
return copy;
37+
}
3038
}

src/main/java/org/dynapi/openapispec/core/schema/Schema.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,9 @@ public JSONObject getOpenApiSpec() {
124124
}
125125

126126
protected abstract JSONObject finalized();
127+
128+
/**
129+
* creates a <b>not deep</b> copy of this schema
130+
*/
131+
public abstract Schema<THIS, TYPE> copy();
127132
}

src/main/java/org/dynapi/openapispec/core/schema/TAnyType.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,11 @@ protected JSONObject finalized() {
1111
return new JSONObject()
1212
.put("AnyValue", new JSONObject());
1313
}
14+
15+
@Override
16+
public TAnyType copy() {
17+
TAnyType copy = new TAnyType();
18+
copy.options.putAll(this.options);
19+
return copy;
20+
}
1421
}

src/main/java/org/dynapi/openapispec/core/schema/TArray.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.ArrayList;
88
import java.util.Arrays;
9+
import java.util.Collection;
910
import java.util.List;
1011

1112
@ToString(callSuper = true)
@@ -17,6 +18,10 @@ public TArray(Schema<?, ?>... types) {
1718
this.types.addAll(Arrays.asList(types));
1819
}
1920

21+
public TArray(Collection<Schema<?, ?>> types) {
22+
this.types.addAll(types);
23+
}
24+
2025
/**
2126
* @param size exact number of items in this array
2227
*/
@@ -65,18 +70,26 @@ public TArray addType(@NonNull Schema<?, ?> schema) {
6570

6671
@Override
6772
protected JSONObject finalized() {
68-
JSONObject finalizedItems;
73+
JSONObject items;
6974
if (types.isEmpty()) {
70-
finalizedItems = new JSONObject();
75+
items = new JSONObject();
7176
} else if (types.size() == 1) {
72-
finalizedItems = types.get(0).finalized();
77+
items = types.get(0).getOpenApiSpec();
7378
} else {
74-
finalizedItems = new JSONObject()
79+
items = new JSONObject()
7580
.put("oneOf", new JSONArray(types.stream().map(Schema::getOpenApiSpec).toList()));
7681
}
7782

7883
return new JSONObject()
7984
.put("type", "array")
80-
.put("items", finalizedItems);
85+
.put("items", items);
86+
}
87+
88+
@Override
89+
public TArray copy() {
90+
TArray copy = new TArray();
91+
copy.options.putAll(this.options);
92+
copy.types.addAll(this.types);
93+
return copy;
8194
}
8295
}

src/main/java/org/dynapi/openapispec/core/schema/TBoolean.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,11 @@ protected JSONObject finalized() {
1111
return new JSONObject()
1212
.put("type", "boolean");
1313
}
14+
15+
@Override
16+
public TBoolean copy() {
17+
TBoolean copy = new TBoolean();
18+
copy.options.putAll(this.options);
19+
return copy;
20+
}
1421
}

src/main/java/org/dynapi/openapispec/core/schema/TInteger.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ protected JSONObject finalized() {
5050
.put("type", "integer");
5151
}
5252

53+
@Override
54+
public TInteger copy() {
55+
TInteger copy = new TInteger();
56+
copy.options.putAll(this.options);
57+
return copy;
58+
}
59+
5360
public static class CommonFormats {
5461
public static final String FLOAT = "float";
5562
public static final String DOUBLE = "double";

src/main/java/org/dynapi/openapispec/core/schema/TNumber.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ protected JSONObject finalized() {
5050
.put("type", "number");
5151
}
5252

53+
@Override
54+
public TNumber copy() {
55+
TNumber copy = new TNumber();
56+
copy.options.putAll(this.options);
57+
return copy;
58+
}
59+
5360
public static class CommonFormats {
5461
public static final String INT32 = "int32";
5562
public static final String INT64 = "int64";

src/main/java/org/dynapi/openapispec/core/schema/TObject.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,12 @@ protected JSONObject finalized() {
7777
.put("type", "object")
7878
.put("properties", finalizedProperties);
7979
}
80+
81+
@Override
82+
public TObject copy() {
83+
TObject copy = new TObject();
84+
copy.options.putAll(this.options);
85+
copy.properties.putAll(this.properties);
86+
return copy;
87+
}
8088
}

0 commit comments

Comments
 (0)