@@ -40,6 +40,16 @@ class schema
40
40
root_schema *root_;
41
41
json default_value_ = nullptr ;
42
42
43
+ protected:
44
+ virtual std::shared_ptr<schema> make_for_default_ (
45
+ std::shared_ptr<::schema> & /* sch */ ,
46
+ root_schema * /* root */ ,
47
+ std::vector<nlohmann::json_uri> & /* uris */ ,
48
+ nlohmann::json & /* default_value */ ) const
49
+ {
50
+ return nullptr ;
51
+ };
52
+
43
53
public:
44
54
virtual ~schema () = default ;
45
55
@@ -92,6 +102,21 @@ class schema_ref : public schema
92
102
return default_value_;
93
103
}
94
104
105
+ protected:
106
+ virtual std::shared_ptr<schema> make_for_default_ (
107
+ std::shared_ptr<::schema> &sch,
108
+ root_schema *root,
109
+ std::vector<nlohmann::json_uri> &uris,
110
+ nlohmann::json &default_value) const override
111
+ {
112
+ // create a new reference schema using the original reference (which will be resolved later)
113
+ // to store this overloaded default value #209
114
+ auto result = std::make_shared<schema_ref>(uris[0 ].to_string (), root);
115
+ result->set_target (sch, true );
116
+ result->set_default_value (default_value);
117
+ return result;
118
+ };
119
+
95
120
public:
96
121
schema_ref (const std::string &id, root_schema *root)
97
122
: schema(root), id_(id) {}
@@ -509,6 +534,18 @@ class type_schema : public schema
509
534
}
510
535
}
511
536
537
+ protected:
538
+ virtual std::shared_ptr<schema> make_for_default_ (
539
+ std::shared_ptr<::schema> & /* sch */ ,
540
+ root_schema * /* root */ ,
541
+ std::vector<nlohmann::json_uri> & /* uris */ ,
542
+ nlohmann::json &default_value) const override
543
+ {
544
+ auto result = std::make_shared<type_schema>(*this );
545
+ result->set_default_value (default_value);
546
+ return result;
547
+ };
548
+
512
549
public:
513
550
type_schema (json &sch,
514
551
root_schema *root,
@@ -1289,16 +1326,8 @@ std::shared_ptr<schema> schema::make(json &schema,
1289
1326
attr = schema.find (" default" );
1290
1327
if (attr != schema.end ()) {
1291
1328
// copy the referenced schema depending on the underlying type and modify the default value
1292
- if (dynamic_cast <schema_ref *>(sch.get ())) {
1293
- // create a new reference schema use the original reference (which will be resolved later)
1294
- // to store this overloaed default value #209
1295
- auto overloaded_ref_sch = std::make_shared<schema_ref>(uris[0 ].to_string (), root);
1296
- overloaded_ref_sch->set_target (sch, true );
1297
- overloaded_ref_sch->set_default_value (attr.value ());
1298
- sch = overloaded_ref_sch;
1299
- } else if (auto *type_sch = dynamic_cast <type_schema *>(sch.get ())) {
1300
- sch = std::make_shared<type_schema>(*type_sch);
1301
- sch->set_default_value (attr.value ());
1329
+ if (auto new_sch = sch->make_for_default_ (sch, root, uris, attr.value ())) {
1330
+ sch = new_sch;
1302
1331
}
1303
1332
schema.erase (attr);
1304
1333
}
0 commit comments