Skip to content

Commit

Permalink
uri.resolve(relative,empty-base) -> relative
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 30, 2024
1 parent 3a96492 commit 09b300d
Show file tree
Hide file tree
Showing 22 changed files with 207 additions and 82 deletions.
6 changes: 3 additions & 3 deletions doc/ref/jsonschema/jsonschema.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ the [JSON Schema Test Suite](https://github.com/json-schema-org/JSON-Schema-Test

[Three ways of validating](#eg1)
[Format validation](#eg2)
[Using a URIResolver to resolve references to schemas defined in external files](#eg3)
[Using a ResolveURI to resolve references to schemas defined in external files](#eg3)
[Validate before decoding JSON into C++ class objects](#eg4)
[Default values](#eg5)

Expand Down Expand Up @@ -349,7 +349,7 @@ Output:

<div id="eg3"/>

#### Using a URIResolver to resolve references to schemas defined in external files
#### Using a ResolveURI to resolve references to schemas defined in external files

In this example, the main schema defines a reference using the `$ref` property to a
second schema, defined in an external file `name-defs.json`,
Expand All @@ -374,7 +374,7 @@ second schema, defined in an external file `name-defs.json`,
```

jsoncons needs to know how to turn a URI reference to `name-defs.json` into a JSON Schema document,
and for that it needs you to provide a `URIResolver`.
and for that it needs you to provide a `ResolveURI`.

```cpp
#include <jsoncons/json.hpp>
Expand Down
24 changes: 12 additions & 12 deletions doc/ref/jsonschema/make_json_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,36 @@ template <typename Json>
json_schema<Json> make_json_schema(Json sch, (since 0.175.0)
evaluation_options options = evaluation_options{});

template <typename Json,class URIResolver>
template <typename Json,class ResolveURI>
json_schema<Json> make_json_schema(const Json& sch, (until 0.175.0)
const URIResolver& resolver,
const ResolveURI& resolve,
evaluation_options options = evaluation_options{}); (2)

template <typename Json,class URIResolver>
template <typename Json,class ResolveURI>
json_schema<Json> make_json_schema(Json sch, (since 0.175.0)
const URIResolver& resolver,
const ResolveURI& resolve,
evaluation_options options = evaluation_options{});

template <typename Json,class URIResolver>
template <typename Json>
json_schema<Json> make_json_schema(const Json& sch, (until 0.175.0)
const std::string& retrieval_uri,
evaluation_options options = evaluation_options{});
(3)
template <typename Json,class URIResolver>
template <typename Json>
json_schema<Json> make_json_schema(Json sch, (since 0.175.0)
const std::string& retrieval_uri,
evaluation_options options = evaluation_options{});

template <typename Json>
template <typename Json,class ResolveURI>
json_schema<Json> make_json_schema(const Json& sch, (until 0.175.0)
const std::string& retrieval_uri,
const URIResolver& resolver,
const ResolveURI& resolve,
evaluation_options options = evaluation_options{});
(4)
template <typename Json>
template <typename Json,class ResolveURI>
json_schema<Json> make_json_schema(Json sch, (since 0.175.0)
const std::string& retrieval_uri,
const URIResolver& resolver,
const ResolveURI& resolve,
evaluation_options options = evaluation_options{});
```
Expand All @@ -54,8 +54,8 @@ Returns a [json_schema<Json>](json_schema.md) that represents a compiled JSON Sc
<td>JSON Schema</td>
</tr>
<tr>
<td>resolver</td>
<td>A function object with the signature of <code>resolver</code> being equivalent to
<td>resolve</td>
<td>A function object with the signature of <code>resolve</code> being equivalent to
<pre>
Json fun(const jsoncons::uri& uri)
</pre>
Expand Down
8 changes: 4 additions & 4 deletions doc/ref/jsonschema/make_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ template <typename Json>
std::shared_ptr<json_schema<Json>> make_schema(const Json& schema,
const std::string& retrieval_uri); (2) (since 0.173.0)

template <typename Json,class URIResolver>
template <typename Json,class ResolveURI>
std::shared_ptr<json_schema<Json>> make_schema(const Json& schema,
const std::string& retrieval_uri, const URIResolver& resolver); (3) (since 0.173.0)
const std::string& retrieval_uri, const ResolveURI& resolver); (3) (since 0.173.0)

template <typename Json,class URIResolver>
template <typename Json,class ResolveURI>
std::shared_ptr<json_schema<Json>> make_schema(const Json& schema,
const URIResolver& resolver); (4)
const ResolveURI& resolver); (4)
```
Returns a `shared_ptr` to a `json_schema<Json>`.
Expand Down
50 changes: 30 additions & 20 deletions include/jsoncons/uri.hpp → include/jsoncons/utility/uri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

// See https://github.com/danielaparker/jsoncons for latest version

#ifndef JSONCONS_JSONSCHEMA_URI_HPP
#define JSONCONS_JSONSCHEMA_URI_HPP
#ifndef JSONCONS_UTILITY_URI_HPP
#define JSONCONS_UTILITY_URI_HPP

#include <string> // std::string
#include <algorithm>
Expand All @@ -16,17 +16,18 @@
#include <jsoncons/detail/write_number.hpp>
#include <iostream>

namespace jsoncons {
namespace jsoncons { namespace utility {

enum class uri_errc
enum class uri_errc
{
success = 0,
invalid_uri = 1,
invalid_characters_in_path = 2
};


class uri_error_category_impl
: public std::error_category
: public std::error_category
{
public:
const char* name() const noexcept override
Expand All @@ -39,34 +40,38 @@ namespace jsoncons {
{
case uri_errc::invalid_uri:
return "Invalid URI";
case uri_errc::invalid_characters_in_path:
return "Invalid characters in path";
default:
return "Unknown uri error";
}
}
};

inline
const std::error_category& uri_error_category()
const std::error_category& uri_error_category()
{
static uri_error_category_impl instance;
return instance;
static uri_error_category_impl instance;
return instance;
}
inline
std::error_code make_error_code(uri_errc result)

inline
std::error_code make_error_code(uri_errc result)
{
return std::error_code(static_cast<int>(result),uri_error_category());
return std::error_code(static_cast<int>(result), uri_error_category());
}
}

} // namespace utility
} // namespace jsoncons

namespace std {
template<>
struct is_error_code_enum<jsoncons::uri_errc> : public true_type
struct is_error_code_enum<jsoncons::utility::uri_errc> : public true_type
{
};
}
} // namespace std

namespace jsoncons {
namespace jsoncons { namespace utility {

struct uri_fragment_part_t
{
Expand Down Expand Up @@ -687,9 +692,9 @@ namespace jsoncons {
default:
if (!(is_pchar(c,s.data()+i, s.size() - i) || c == '/'))
{
ec = uri_errc::invalid_uri;
ec = uri_errc::invalid_characters_in_path;
return uri{};
}
}
break;
}
break;
Expand Down Expand Up @@ -808,10 +813,11 @@ namespace jsoncons {
static std::string merge_paths(const uri& base, const uri& relative)
{
std::string result;

if (base.encoded_path().empty())
if (!base.encoded_authority().empty() && base.encoded_path().empty())
{
result = "/";
result.append(relative.encoded_path().data(), relative.encoded_path().length());
}
else
{
Expand Down Expand Up @@ -1146,6 +1152,10 @@ namespace jsoncons {
}
};

} // namespace utility

using uri = utility::uri;

} // namespace jsoncons

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define JSONCONS_JSONSCHEMA_COMMON_COMPILATION_CONTEXT_HPP

#include <jsoncons/config/jsoncons_config.hpp>
#include <jsoncons/uri.hpp>
#include <jsoncons/utility/uri.hpp>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
#include <jsoncons_ext/jsonschema/jsonschema_error.hpp>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define JSONCONS_JSONSCHEMA_COMMON_EVALUATION_CONTEXT_HPP

#include <jsoncons/config/jsoncons_config.hpp>
#include <jsoncons/uri.hpp>
#include <jsoncons/utility/uri.hpp>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
#include <jsoncons_ext/jsonschema/jsonschema_error.hpp>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define JSONCONS_JSONSCHEMA_COMMON_FORMAT_VALIDATOR_HPP

#include <jsoncons/config/jsoncons_config.hpp>
#include <jsoncons/uri.hpp>
#include <jsoncons/utility/uri.hpp>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
#include <jsoncons_ext/jsonschema/common/validator.hpp>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#define JSONCONS_JSONSCHEMA_COMMON_KEYWORD_VALIDATORS_HPP

#include <jsoncons/config/jsoncons_config.hpp>
#include <jsoncons/uri.hpp>
#include <jsoncons/utility/uri.hpp>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
#include <jsoncons_ext/jsonschema/common/format_validator.hpp>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define JSONCONS_JSONSCHEMA_COMMON_SCHEMA_VALIDATORS_HPP

#include <jsoncons/config/jsoncons_config.hpp>
#include <jsoncons/uri.hpp>
#include <jsoncons/utility/uri.hpp>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonschema/common/evaluation_context.hpp>
#include <jsoncons_ext/jsonschema/common/keyword_validators.hpp>
Expand Down
6 changes: 3 additions & 3 deletions include/jsoncons_ext/jsonschema/common/uri_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define JSONCONS_JSONSCHEMA_COMMON_SCHEMA_IDENTIFIER_HPP

#include <jsoncons/config/jsoncons_config.hpp>
#include <jsoncons/uri.hpp>
#include <jsoncons/utility/uri.hpp>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
#include <jsoncons_ext/jsonschema/jsonschema_error.hpp>
Expand Down Expand Up @@ -118,7 +118,7 @@ namespace jsonschema {
jsoncons::jsonpointer::json_pointer pointer(std::string(uri_.encoded_fragment()));
pointer /= field;

jsoncons::uri new_uri(uri_, uri_fragment_part, pointer.to_string());
jsoncons::uri new_uri(uri_, utility::uri_fragment_part, pointer.to_string());

return uri_wrapper(std::move(new_uri));
}
Expand All @@ -131,7 +131,7 @@ namespace jsonschema {
jsoncons::jsonpointer::json_pointer pointer(std::string(uri_.encoded_fragment()));
pointer /= index;

jsoncons::uri new_uri(uri_, uri_fragment_part, pointer.to_string());
jsoncons::uri new_uri(uri_, utility::uri_fragment_part, pointer.to_string());

return uri_wrapper(std::move(new_uri));
}
Expand Down
2 changes: 1 addition & 1 deletion include/jsoncons_ext/jsonschema/common/validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define JSONCONS_JSONSCHEMA_COMMON_VALIDATOR_HPP

#include <jsoncons/config/jsoncons_config.hpp>
#include <jsoncons/uri.hpp>
#include <jsoncons/utility/uri.hpp>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonschema/common/evaluation_context.hpp>
#include <jsoncons_ext/jsonschema/jsonschema_error.hpp>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#ifndef JSONCONS_JSONSCHEMA_DRAFT201909_SCHEMA_BUILDER_201909_HPP
#define JSONCONS_JSONSCHEMA_DRAFT201909_SCHEMA_BUILDER_201909_HPP

#include <jsoncons/uri.hpp>
#include <jsoncons/utility/uri.hpp>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
#include <jsoncons_ext/jsonschema/common/compilation_context.hpp>
Expand Down Expand Up @@ -524,7 +524,7 @@ namespace draft201909 {
JSONCONS_THROW(schema_error("Invalid $anchor " + anchor));
}
auto uri = !new_uris.empty() ? new_uris.back().uri() : jsoncons::uri{"#"};
jsoncons::uri new_uri(uri, uri_fragment_part, anchor);
jsoncons::uri new_uri(uri, utility::uri_fragment_part, anchor);
uri_wrapper identifier{ new_uri };
if (std::find(new_uris.begin(), new_uris.end(), identifier) == new_uris.end())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#ifndef JSONCONS_JSONSCHEMA_DRAFT202012_SCHEMA_BUILDER_202012_HPP
#define JSONCONS_JSONSCHEMA_DRAFT202012_SCHEMA_BUILDER_202012_HPP

#include <jsoncons/uri.hpp>
#include <jsoncons/utility/uri.hpp>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
#include <jsoncons_ext/jsonschema/common/compilation_context.hpp>
Expand Down Expand Up @@ -252,7 +252,7 @@ namespace draft202012 {
if (it != sch.object_range().end())
{
std::string value = it->value().template as<std::string>();
jsoncons::uri new_uri(context.get_base_uri(), uri_fragment_part, value);
jsoncons::uri new_uri(context.get_base_uri(), utility::uri_fragment_part, value);
dynamic_anchor = jsoncons::optional<jsoncons::uri>(new_uri);
local_anchor_dict.emplace(value, context.get_base_uri());
}
Expand Down Expand Up @@ -579,7 +579,7 @@ namespace draft202012 {
JSONCONS_THROW(schema_error("Invalid $anchor " + anchor));
}
auto uri = !new_uris.empty() ? new_uris.back().uri() : jsoncons::uri{"#"};
jsoncons::uri new_uri(uri, uri_fragment_part, anchor);
jsoncons::uri new_uri(uri, utility::uri_fragment_part, anchor);
uri_wrapper identifier{ new_uri };
if (std::find(new_uris.begin(), new_uris.end(), identifier) == new_uris.end())
{
Expand All @@ -595,7 +595,7 @@ namespace draft202012 {
JSONCONS_THROW(schema_error("Invalid $dynamicAnchor " + anchor));
}
auto uri = !new_uris.empty() ? new_uris.back().uri() : jsoncons::uri{"#"};
jsoncons::uri new_uri(uri, uri_fragment_part, anchor);
jsoncons::uri new_uri(uri, utility::uri_fragment_part, anchor);
uri_wrapper identifier{ new_uri };
if (std::find(new_uris.begin(), new_uris.end(), identifier) == new_uris.end())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#ifndef JSONCONS_JSONSCHEMA_DRAFT4_SCHEMA_BUILDER_4_HPP
#define JSONCONS_JSONSCHEMA_DRAFT4_SCHEMA_BUILDER_4_HPP

#include <jsoncons/uri.hpp>
#include <jsoncons/utility/uri.hpp>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
#include <jsoncons_ext/jsonschema/common/compilation_context.hpp>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#ifndef JSONCONS_JSONSCHEMA_DRAFT6_SCHEMA_BUILDER_6_HPP
#define JSONCONS_JSONSCHEMA_DRAFT6_SCHEMA_BUILDER_6_HPP

#include <jsoncons/uri.hpp>
#include <jsoncons/utility/uri.hpp>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
#include <jsoncons_ext/jsonschema/common/compilation_context.hpp>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#ifndef JSONCONS_JSONSCHEMA_DRAFT7_SCHEMA_BUILDER_7_HPP
#define JSONCONS_JSONSCHEMA_DRAFT7_SCHEMA_BUILDER_7_HPP

#include <jsoncons/uri.hpp>
#include <jsoncons/utility/uri.hpp>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
#include <jsoncons_ext/jsonschema/common/compilation_context.hpp>
Expand Down
Loading

0 comments on commit 09b300d

Please sign in to comment.