Skip to content

Commit

Permalink
Remove support for non-safe REST methods
Browse files Browse the repository at this point in the history
Takes up on #927 and uses a clear error message instead of letting the compiler output indirect errors.

Closes #927.
  • Loading branch information
s-ludwig committed Feb 6, 2024
1 parent 7de62ab commit 3640f86
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
3 changes: 3 additions & 0 deletions examples/rest-collections/source/api.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ module api;
import vibe.web.rest;

interface ForumAPI {
@safe:
// base path /threads/
Collection!ThreadAPI threads();
}

interface ThreadAPI {
@safe:
// define the index parameters used to identify the collection items
struct CollectionIndices {
string _thread_name;
Expand All @@ -27,6 +29,7 @@ interface ThreadAPI {
}

interface PostAPI {
@safe:
// define the index parameters used to identify the collection items
struct CollectionIndices {
string _thread_name;
Expand Down
1 change: 1 addition & 0 deletions examples/rest-js/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import vibe.http.common : HTTPMethod;

// Defines a simple RESTful API
interface ITest {
@safe:
// GET /compute_sum?a=...&b=...
@method(HTTPMethod.GET)
float computeSum(float a, float b);
Expand Down
3 changes: 3 additions & 0 deletions tests/restclient/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import vibe.vibe;

interface ITestAPI
{
@safe:
@property ISub sub();

@method(HTTPMethod.POST) @path("other/path")
Expand All @@ -22,6 +23,7 @@ interface ITestAPI
}

interface ISub {
@safe:
int get(int id);
}

Expand Down Expand Up @@ -49,6 +51,7 @@ class SubAPI : ISub {

interface ITestAPICors
{
@safe:
string getFoo();
string setFoo();
string addFoo();
Expand Down
4 changes: 4 additions & 0 deletions tests/restcollections/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ module app;
import vibe.vibe;

interface API {
@safe:
Collection!ItemAPI items();
}

interface ItemAPI {
@safe:
struct CollectionIndices {
string _item;
}
Expand All @@ -17,6 +19,7 @@ interface ItemAPI {
}

interface SubItemAPI {
@safe:
struct CollectionIndices {
string _item;
int _index;
Expand All @@ -29,6 +32,7 @@ interface SubItemAPI {
}

interface ItemManagerAPI {
@safe:
@property string databaseURL();
}

Expand Down
26 changes: 10 additions & 16 deletions web/vibe/web/rest.d
Original file line number Diff line number Diff line change
Expand Up @@ -1562,8 +1562,8 @@ private HTTPServerRequestDelegate jsonMethodHandler(alias Func, size_t ridx, T)(
v = fromRestString!(PT, SerPolicyType)(*pv);
} else static if (sparam.kind == ParameterKind.attributed) {
static if (!__traits(compiles, () @safe { computeAttributedParameterCtx!(CFunc, pname)(inst, req, res); } ()))
pragma(msg, "Non-@safe @before evaluators are deprecated - annotate evaluator function for parameter "~pname~" of "~T.stringof~"."~Method~" as @safe.");
v = () @trusted { return computeAttributedParameterCtx!(CFunc, pname)(inst, req, res); } ();
static assert(false, "`@before` evaluator for REST interface method `" ~ fullyQualifiedName!T ~ "." ~ Method ~ "` must be marked `@safe`.");
v = computeAttributedParameterCtx!(CFunc, pname)(inst, req, res);
} else static if (sparam.kind == ParameterKind.internal) {
if (auto pv = fieldname in req.params)
v = fromRestString!(PT, DefaultPolicy)(urlDecode(*pv));
Expand Down Expand Up @@ -1628,28 +1628,25 @@ private HTTPServerRequestDelegate jsonMethodHandler(alias Func, size_t ridx, T)(
import vibe.internal.meta.funcattr;

static if (!__traits(compiles, () @safe { __traits(getMember, inst, Method)(params); }))
pragma(msg, "Non-@safe methods are deprecated in REST interfaces - Mark " ~
T.stringof ~ "." ~ Method ~ " as @safe.");
static assert(false, "REST interface method `" ~ fullyQualifiedName!T ~ "." ~ Method ~ "` must be marked `@safe`.");

static if (is(RT == void)) {
// TODO: remove after deprecation period
() @trusted { __traits(getMember, inst, Method)(params); } ();
__traits(getMember, inst, Method)(params);
returnHeaders();
res.writeBody(cast(ubyte[])null);
} else static if (isInputStream!RT) {
returnHeaders();
auto ret = () @trusted {
return evaluateOutputModifiers!CFunc(
__traits(getMember, inst, Method)(params), req, res); } ();
auto ret = evaluateOutputModifiers!CFunc(
__traits(getMember, inst, Method)(params), req, res);
res.headers["Content-Type"] = "application/octet-stream";
ret.pipe(res.bodyWriter);
} else {
// TODO: remove after deprecation period
static if (!__traits(compiles, () @safe { evaluateOutputModifiers!Func(RT.init, req, res); } ()))
pragma(msg, "Non-@safe @after evaluators are deprecated - annotate @after evaluator function for " ~
T.stringof ~ "." ~ Method ~ " as @safe.");
static assert(false, "`@after` evaluator for REST interface method `" ~ fullyQualifiedName!T ~ "." ~ Method ~ "` must be marked `@safe`.");

auto ret = () @trusted {
auto ret = () /*@trusted*/ {
return evaluateOutputModifiers!CFunc(
__traits(getMember, inst, Method)(params), req, res); } ();
returnHeaders();
Expand All @@ -1670,12 +1667,9 @@ private HTTPServerRequestDelegate jsonMethodHandler(alias Func, size_t ridx, T)(
serializer.serialize!(SerPolicyT!(RestInterface!T.I).PolicyTemplate)(serialized_output, ret);
}))
{
pragma(msg, "Non-@safe serialization of REST return types deprecated - ensure that " ~
RT.stringof~" is safely serializable.");
static assert(false, "Serialization of return type `"~RT.stringof~"` of REST interface method `" ~ fullyQualifiedName!T ~ "." ~ Method ~ "` must be `@safe`.");
}
() @trusted {
serializer.serialize!(SerPolicyT!(RestInterface!T.I).PolicyTemplate)(serialized_output, ret);
}();
serializer.serialize!(SerPolicyT!(RestInterface!T.I).PolicyTemplate)(serialized_output, ret);
res.writeBody(serialized_output.data, serializer.contentType);
}
res.statusCode = HTTPStatus.notAcceptable; // will trigger RestException on the client side
Expand Down

0 comments on commit 3640f86

Please sign in to comment.