Skip to content

Commit 683bbcd

Browse files
committed
provide examples field to set example value for a field.
1 parent a8dc686 commit 683bbcd

File tree

5 files changed

+175
-117
lines changed

5 files changed

+175
-117
lines changed

core/src/main/java/org/everit/json/schema/Schema.java

Lines changed: 138 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,22 @@
11
package org.everit.json.schema;
22

3-
import static java.util.Collections.unmodifiableMap;
3+
import org.everit.json.schema.internal.JSONPrinter;
4+
import org.json.JSONWriter;
45

56
import java.io.StringWriter;
67
import java.util.HashMap;
8+
import java.util.List;
79
import java.util.Map;
810
import java.util.Objects;
911

10-
import org.everit.json.schema.internal.JSONPrinter;
11-
import org.json.JSONWriter;
12+
import static java.util.Collections.unmodifiableMap;
1213

1314
/**
1415
* Superclass of all other schema validator classes of this package.
1516
*/
1617
public abstract class Schema {
1718

18-
/**
19-
* Abstract builder class for the builder classes of {@code Schema} subclasses. This builder is
20-
* used to load the generic properties of all types of schemas like {@code title} or
21-
* {@code description}.
22-
*
23-
* @param <S>
24-
* the type of the schema being built by the builder subclass.
25-
*/
26-
public abstract static class Builder<S extends Schema> {
27-
28-
private String title;
29-
30-
private String description;
31-
32-
private String id;
33-
34-
private SchemaLocation schemaLocation;
35-
36-
private Object defaultValue;
37-
38-
private Boolean nullable = null;
39-
40-
private Boolean readOnly = null;
41-
42-
private Boolean writeOnly = null;
43-
44-
public Map<String, Object> unprocessedProperties = new HashMap<>(0);
45-
46-
public Builder<S> title(String title) {
47-
this.title = title;
48-
return this;
49-
}
50-
51-
public Builder<S> description(String description) {
52-
this.description = description;
53-
return this;
54-
}
55-
56-
public Builder<S> id(String id) {
57-
this.id = id;
58-
return this;
59-
}
60-
61-
/**
62-
* @deprecated Use {@link #schemaLocation(SchemaLocation)} instead.
63-
*/
64-
@Deprecated
65-
public Builder<S> schemaLocation(String schemaLocation) {
66-
return schemaLocation(SchemaLocation.parseURI(schemaLocation));
67-
}
68-
69-
public Builder<S> schemaLocation(SchemaLocation location) {
70-
this.schemaLocation = location;
71-
return this;
72-
}
73-
74-
public Builder<S> defaultValue(Object defaultValue) {
75-
this.defaultValue = defaultValue;
76-
return this;
77-
}
78-
79-
public Builder<S> nullable(Boolean nullable) {
80-
this.nullable = nullable;
81-
return this;
82-
}
83-
84-
public Builder<S> readOnly(Boolean readOnly) {
85-
this.readOnly = readOnly;
86-
return this;
87-
}
88-
89-
public Builder<S> writeOnly(Boolean writeOnly) {
90-
this.writeOnly = writeOnly;
91-
return this;
92-
}
93-
94-
public Builder<S> unprocessedProperties(Map<String, Object> unprocessedProperties) {
95-
this.unprocessedProperties = unprocessedProperties;
96-
return this;
97-
}
98-
99-
public abstract S build();
100-
101-
}
19+
private final String examples;
10220

10321
private final String title;
10422

@@ -113,14 +31,6 @@ public Builder<S> unprocessedProperties(Map<String, Object> unprocessedPropertie
11331

11432
private final Object defaultValue;
11533

116-
private final Boolean nullable;
117-
118-
private final Boolean readOnly;
119-
120-
private final Boolean writeOnly;
121-
122-
private final Map<String, Object> unprocessedProperties;
123-
12434
/**
12535
* Constructor.
12636
*
@@ -134,12 +44,43 @@ protected Schema(Builder<?> builder) {
13444
this.schemaLocation = builder.schemaLocation == null ? null : builder.schemaLocation.toString();
13545
this.location = builder.schemaLocation;
13646
this.defaultValue = builder.defaultValue;
47+
this.examples = builder.examples;
13748
this.nullable = builder.nullable;
13849
this.readOnly = builder.readOnly;
13950
this.writeOnly = builder.writeOnly;
14051
this.unprocessedProperties = new HashMap<>(builder.unprocessedProperties);
14152
}
14253

54+
private final Boolean nullable;
55+
56+
private final Boolean readOnly;
57+
58+
private final Boolean writeOnly;
59+
60+
private final Map<String, Object> unprocessedProperties;
61+
62+
@Override
63+
public boolean equals(Object o) {
64+
if (this == o) {
65+
return true;
66+
}
67+
if (o instanceof Schema) {
68+
Schema schema = (Schema) o;
69+
return schema.canEqual(this) &&
70+
Objects.equals(title, schema.title) &&
71+
Objects.equals(defaultValue, schema.defaultValue) &&
72+
Objects.equals(examples, schema.examples) &&
73+
Objects.equals(description, schema.description) &&
74+
Objects.equals(id, schema.id) &&
75+
Objects.equals(nullable, schema.nullable) &&
76+
Objects.equals(readOnly, schema.readOnly) &&
77+
Objects.equals(writeOnly, schema.writeOnly) &&
78+
Objects.equals(unprocessedProperties, schema.unprocessedProperties);
79+
} else {
80+
return false;
81+
}
82+
}
83+
14384
/**
14485
* Performs the schema validation.
14586
*
@@ -199,29 +140,13 @@ public boolean definesProperty(String field) {
199140
}
200141

201142
@Override
202-
public boolean equals(Object o) {
203-
if (this == o) {
204-
return true;
205-
}
206-
if (o instanceof Schema) {
207-
Schema schema = (Schema) o;
208-
return schema.canEqual(this) &&
209-
Objects.equals(title, schema.title) &&
210-
Objects.equals(defaultValue, schema.defaultValue) &&
211-
Objects.equals(description, schema.description) &&
212-
Objects.equals(id, schema.id) &&
213-
Objects.equals(nullable, schema.nullable) &&
214-
Objects.equals(readOnly, schema.readOnly) &&
215-
Objects.equals(writeOnly, schema.writeOnly) &&
216-
Objects.equals(unprocessedProperties, schema.unprocessedProperties);
217-
} else {
218-
return false;
219-
}
143+
public int hashCode() {
144+
return Objects.hash(title, description, id, defaultValue, examples, nullable, readOnly, writeOnly,
145+
unprocessedProperties);
220146
}
221147

222-
@Override
223-
public int hashCode() {
224-
return Objects.hash(title, description, id, defaultValue, nullable, readOnly, writeOnly, unprocessedProperties);
148+
public String getExamples() {
149+
return this.examples;
225150
}
226151

227152
public String getTitle() {
@@ -248,6 +173,103 @@ public Object getDefaultValue() {
248173
return this.defaultValue;
249174
}
250175

176+
/**
177+
* Abstract builder class for the builder classes of {@code Schema} subclasses. This builder is
178+
* used to load the generic properties of all types of schemas like {@code title} or
179+
* {@code description}.
180+
*
181+
* @param <S>
182+
* the type of the schema being built by the builder subclass.
183+
*/
184+
public abstract static class Builder<S extends Schema> {
185+
186+
private String title;
187+
188+
private String description;
189+
190+
private String id;
191+
192+
private SchemaLocation schemaLocation;
193+
194+
private Object defaultValue;
195+
196+
private String examples;
197+
198+
private Boolean nullable = null;
199+
200+
private Boolean readOnly = null;
201+
202+
private Boolean writeOnly = null;
203+
204+
public Map<String, Object> unprocessedProperties = new HashMap<>(0);
205+
206+
public Builder<S> title(String title) {
207+
this.title = title;
208+
return this;
209+
}
210+
211+
public Builder<S> description(String description) {
212+
this.description = description;
213+
return this;
214+
}
215+
216+
public Builder<S> id(String id) {
217+
this.id = id;
218+
return this;
219+
}
220+
221+
/**
222+
* @deprecated Use {@link #schemaLocation(SchemaLocation)} instead.
223+
*/
224+
@Deprecated
225+
public Builder<S> schemaLocation(String schemaLocation) {
226+
return schemaLocation(SchemaLocation.parseURI(schemaLocation));
227+
}
228+
229+
public Builder<S> schemaLocation(SchemaLocation location) {
230+
this.schemaLocation = location;
231+
return this;
232+
}
233+
234+
public Builder<S> defaultValue(Object defaultValue) {
235+
this.defaultValue = defaultValue;
236+
return this;
237+
}
238+
239+
public Builder<S> examples(List<String> examples) {
240+
StringBuilder exampleBuilder = new StringBuilder("[");
241+
for (String example : examples) {
242+
exampleBuilder.append(example);
243+
}
244+
exampleBuilder.append("]");
245+
this.examples = examples.toString();
246+
return this;
247+
}
248+
249+
public Builder<S> nullable(Boolean nullable) {
250+
this.nullable = nullable;
251+
return this;
252+
}
253+
254+
public Builder<S> readOnly(Boolean readOnly) {
255+
this.readOnly = readOnly;
256+
return this;
257+
}
258+
259+
public Builder<S> writeOnly(Boolean writeOnly) {
260+
this.writeOnly = writeOnly;
261+
return this;
262+
}
263+
264+
public Builder<S> unprocessedProperties(Map<String, Object> unprocessedProperties) {
265+
this.unprocessedProperties = unprocessedProperties;
266+
return this;
267+
}
268+
269+
public abstract S build();
270+
271+
}
272+
251273
public boolean hasDefaultValue() {
252274
return this.defaultValue != null;
253275
}

core/src/main/java/org/everit/json/schema/ToStringVisitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class ToStringVisitor extends Visitor {
3636
writer.ifPresent("description", schema.getDescription());
3737
writer.ifPresent("nullable", schema.isNullable());
3838
writer.ifPresent("default", schema.getDefaultValue());
39+
writer.ifPresent("examples", schema.getExamples());
3940
writer.ifPresent("readOnly", schema.isReadOnly());
4041
writer.ifPresent("writeOnly", schema.isWriteOnly());
4142
super.visitSchema(schema);

core/src/main/java/org/everit/json/schema/loader/SchemaLoader.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.net.URI;
1313
import java.net.URISyntaxException;
14+
import java.util.ArrayList;
1415
import java.util.Collection;
1516
import java.util.HashMap;
1617
import java.util.List;
@@ -432,6 +433,9 @@ private AdjacentSchemaExtractionState loadCommonSchemaProperties(Schema.Builder
432433
consumedKeys.maybe(config.specVersion.idKeyword()).map(JsonValue::requireString).ifPresent(builder::id);
433434
consumedKeys.maybe("title").map(JsonValue::requireString).ifPresent(builder::title);
434435
consumedKeys.maybe("description").map(JsonValue::requireString).ifPresent(builder::description);
436+
if(consumedKeys.maybe("examples").isPresent()) {
437+
setExamples(builder, consumedKeys);
438+
}
435439
if (ls.specVersion() == DRAFT_7) {
436440
consumedKeys.maybe("readOnly").map(JsonValue::requireBoolean).ifPresent(builder::readOnly);
437441
consumedKeys.maybe("writeOnly").map(JsonValue::requireBoolean).ifPresent(builder::writeOnly);
@@ -448,6 +452,15 @@ private AdjacentSchemaExtractionState loadCommonSchemaProperties(Schema.Builder
448452
return state.reduce(new ExtractionResult(consumedKeys.collect(), emptyList()));
449453
}
450454

455+
private void setExamples(Schema.Builder builder, KeyConsumer consumedKeys) {
456+
JsonArray jsonArray = consumedKeys.maybe("examples").get().requireArray();
457+
List<String> examples= new ArrayList<>();
458+
for(int index = 0; index < jsonArray.length();index++){
459+
examples.add(jsonArray.at(index).requireString());
460+
}
461+
builder.examples(examples);
462+
}
463+
451464
/**
452465
* Populates a {@code Schema.Builder} instance from the {@code schemaJson} schema definition.
453466
*

0 commit comments

Comments
 (0)