Skip to content

Commit 5041e87

Browse files
committed
Merge branch 'main' into improv_ref
2 parents e5ac1af + 12a6aea commit 5041e87

35 files changed

+1786
-32
lines changed

jac-cloud/.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ repos:
77
- id: check-json
88
- id: trailing-whitespace
99
- repo: https://github.com/psf/black
10-
rev: 24.4.2
10+
rev: 24.8.0
1111
hooks:
1212
- id: black
1313
- repo: https://github.com/PyCQA/flake8

jac-cloud/jac_cloud/core/context.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from bson import ObjectId
99

1010
from fastapi import Request
11+
from fastapi.responses import ORJSONResponse
1112

1213
from jaclang.runtimelib.context import ExecutionContext
1314

@@ -46,6 +47,7 @@ class JaseciContext(ExecutionContext):
4647

4748
mem: MongoDB
4849
reports: list
50+
status: int
4951
system_root: NodeAnchor
5052
root: NodeAnchor
5153
entry_node: NodeAnchor
@@ -68,6 +70,7 @@ def create(request: Request, entry: NodeAnchor | None = None) -> "JaseciContext"
6870
ctx.request = request
6971
ctx.mem = MongoDB()
7072
ctx.reports = []
73+
ctx.status = 200
7174

7275
if not isinstance(system_root := ctx.mem.find_by_id(SUPER_ROOT), NodeAnchor):
7376
system_root = NodeAnchor(
@@ -130,9 +133,9 @@ def get_root() -> Root: # type: ignore[override]
130133
"""Get current root."""
131134
return cast(Root, JaseciContext.get().root.architype)
132135

133-
def response(self, returns: list[Any], status: int = 200) -> ContextResponse:
136+
def response(self, returns: list[Any]) -> ORJSONResponse:
134137
"""Return serialized version of reports."""
135-
resp: ContextResponse = {"status": status, "returns": returns}
138+
resp: ContextResponse = {"status": self.status, "returns": returns}
136139

137140
if self.reports:
138141
for key, val in enumerate(self.reports):
@@ -145,7 +148,7 @@ def response(self, returns: list[Any], status: int = 200) -> ContextResponse:
145148
if not SHOW_ENDPOINT_RETURNS:
146149
resp.pop("returns")
147150

148-
return resp
151+
return ORJSONResponse(resp, status_code=self.status)
149152

150153
def clean_response(
151154
self, key: str | int, val: Any, obj: list | dict # noqa: ANN401

jac-cloud/jac_cloud/plugin/jaseci.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def api_entry(
163163
if jctx.validate_access():
164164
wlk.spawn_call(jctx.entry_node)
165165
jctx.close()
166-
return ORJSONResponse(jctx.response(wlk.returns))
166+
return jctx.response(wlk.returns)
167167
else:
168168
jctx.close()
169169
raise HTTPException(

jac-cloud/jac_cloud/tests/openapi_specs.json

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4356,6 +4356,118 @@
43564356
}
43574357
}
43584358
}
4359+
},
4360+
"/walker/custom_status_code": {
4361+
"post": {
4362+
"tags": [
4363+
"walker",
4364+
"walker"
4365+
],
4366+
"summary": "/custom_status_code",
4367+
"operationId": "api_root_walker_custom_status_code_post",
4368+
"requestBody": {
4369+
"content": {
4370+
"application/json": {
4371+
"schema": {
4372+
"$ref": "#/components/schemas/custom_status_code_body_model"
4373+
}
4374+
}
4375+
},
4376+
"required": true
4377+
},
4378+
"responses": {
4379+
"200": {
4380+
"description": "Successful Response",
4381+
"content": {
4382+
"application/json": {
4383+
"schema": {
4384+
"$ref": "#/components/schemas/ContextResponse"
4385+
}
4386+
}
4387+
}
4388+
},
4389+
"422": {
4390+
"description": "Validation Error",
4391+
"content": {
4392+
"application/json": {
4393+
"schema": {
4394+
"$ref": "#/components/schemas/HTTPValidationError"
4395+
}
4396+
}
4397+
}
4398+
}
4399+
},
4400+
"security": [
4401+
{
4402+
"HTTPBearer": []
4403+
}
4404+
]
4405+
}
4406+
},
4407+
"/walker/custom_status_code/{node}": {
4408+
"post": {
4409+
"tags": [
4410+
"walker",
4411+
"walker"
4412+
],
4413+
"summary": "/custom_status_code/{node}",
4414+
"operationId": "api_entry_walker_custom_status_code__node__post",
4415+
"security": [
4416+
{
4417+
"HTTPBearer": []
4418+
}
4419+
],
4420+
"parameters": [
4421+
{
4422+
"name": "node",
4423+
"in": "path",
4424+
"required": true,
4425+
"schema": {
4426+
"anyOf": [
4427+
{
4428+
"type": "string"
4429+
},
4430+
{
4431+
"type": "null"
4432+
}
4433+
],
4434+
"title": "Node"
4435+
}
4436+
}
4437+
],
4438+
"requestBody": {
4439+
"required": true,
4440+
"content": {
4441+
"application/json": {
4442+
"schema": {
4443+
"$ref": "#/components/schemas/custom_status_code_body_model"
4444+
}
4445+
}
4446+
}
4447+
},
4448+
"responses": {
4449+
"200": {
4450+
"description": "Successful Response",
4451+
"content": {
4452+
"application/json": {
4453+
"schema": {
4454+
"$ref": "#/components/schemas/ContextResponse"
4455+
}
4456+
}
4457+
}
4458+
},
4459+
"422": {
4460+
"description": "Validation Error",
4461+
"content": {
4462+
"application/json": {
4463+
"schema": {
4464+
"$ref": "#/components/schemas/HTTPValidationError"
4465+
}
4466+
}
4467+
}
4468+
}
4469+
}
4470+
}
43594471
}
43604472
},
43614473
"components": {
@@ -4761,6 +4873,19 @@
47614873
],
47624874
"title": "combination2_body_model"
47634875
},
4876+
"custom_status_code_body_model": {
4877+
"properties": {
4878+
"status": {
4879+
"type": "integer",
4880+
"title": "Status"
4881+
}
4882+
},
4883+
"type": "object",
4884+
"required": [
4885+
"status"
4886+
],
4887+
"title": "custom_status_code_body_model"
4888+
},
47644889
"disallow_other_root_access_body_model": {
47654890
"properties": {
47664891
"root_id": {

jac-cloud/jac_cloud/tests/simple_graph.jac

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ walker manual_create_nested_node {
243243
here.__jac__.apply();
244244

245245
# simulate no auto save
246-
jsrc = JaseciContext.get().mem;
246+
jsrc = Jac.get_context().mem;
247247
jsrc.__mem__.clear();
248248
jsrc.__gc__.clear();
249249

@@ -269,7 +269,7 @@ walker manual_update_nested_node {
269269
nested.__jac__.apply();
270270

271271
# simulate no auto save
272-
jsrc = JaseciContext.get().mem;
272+
jsrc = Jac.get_context().mem;
273273
jsrc.__mem__.clear();
274274
jsrc.__gc__.clear();
275275

@@ -285,7 +285,7 @@ walker manual_detach_nested_node {
285285
nested.__jac__.apply();
286286

287287
# simulate no auto save
288-
jsrc = JaseciContext.get().mem;
288+
jsrc = Jac.get_context().mem;
289289
jsrc.__mem__.clear();
290290
jsrc.__gc__.clear();
291291

@@ -310,7 +310,7 @@ walker manual_delete_nested_node {
310310
nested.__jac__.apply();
311311

312312
# simulate no auto save
313-
jsrc = JaseciContext.get().mem;
313+
jsrc = Jac.get_context().mem;
314314
jsrc.__mem__.clear();
315315
jsrc.__gc__.clear();
316316

@@ -334,7 +334,7 @@ walker manual_delete_nested_edge {
334334
nested_edge.__jac__.apply();
335335

336336
# simulate no auto save
337-
jsrc = JaseciContext.get().mem;
337+
jsrc = Jac.get_context().mem;
338338
jsrc.__mem__.clear();
339339
jsrc.__gc__.clear();
340340

@@ -475,4 +475,12 @@ walker post_with_body_and_file {
475475
class __specs__ {
476476
has auth: bool = False;
477477
}
478+
}
479+
480+
walker custom_status_code {
481+
has status: int;
482+
483+
can enter with `root entry {
484+
Jac.get_context().status = self.status;
485+
}
478486
}

jac-cloud/jac_cloud/tests/test_simple_graph.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,82 @@ async def nested_count_should_be(self, node: int, edge: int) -> None:
505505
),
506506
)
507507

508+
async def trigger_custom_status_code(self) -> None:
509+
"""Test custom status code."""
510+
for acceptable_code in [200, 201, 202, 203, 205, 206, 207, 208, 226]:
511+
res = self.post_api("custom_status_code", {"status": acceptable_code})
512+
self.assertEqual(acceptable_code, res["status"])
513+
self.assertEqual([None], res["returns"])
514+
515+
for error_code in [
516+
400,
517+
401,
518+
402,
519+
403,
520+
404,
521+
405,
522+
406,
523+
407,
524+
408,
525+
409,
526+
410,
527+
411,
528+
412,
529+
413,
530+
414,
531+
415,
532+
416,
533+
417,
534+
418,
535+
421,
536+
422,
537+
423,
538+
424,
539+
425,
540+
426,
541+
428,
542+
429,
543+
431,
544+
451,
545+
500,
546+
501,
547+
502,
548+
503,
549+
504,
550+
505,
551+
506,
552+
507,
553+
508,
554+
510,
555+
511,
556+
]:
557+
self.assertEqual(
558+
error_code,
559+
self.post_api(
560+
"custom_status_code", {"status": error_code}, expect_error=True
561+
),
562+
)
563+
564+
for invalid_code in [
565+
100,
566+
101,
567+
102,
568+
103,
569+
204,
570+
300,
571+
301,
572+
302,
573+
303,
574+
304,
575+
305,
576+
306,
577+
307,
578+
308,
579+
]:
580+
self.assertRaises(
581+
Exception, self.post_api, "custom_status_code", {"status": invalid_code}
582+
)
583+
508584
async def test_all_features(self) -> None:
509585
"""Test Full Features."""
510586
self.trigger_openapi_specs_test()
@@ -576,3 +652,9 @@ async def test_all_features(self) -> None:
576652
self.trigger_access_validation_test(
577653
give_access_to_full_graph=True, via_all=True
578654
)
655+
656+
###################################################
657+
# CUSTOM STATUS #
658+
###################################################
659+
660+
await self.trigger_custom_status_code()

jac/examples/reference/visit_statements.jac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
walker Visitor {
22
can travel with `root entry {
33
visit [-->] else {
4-
visitroot;
4+
visit root;
55
disengage;
66
}
77
}

jac/jaclang/compiler/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3589,7 +3589,7 @@ def match_case_block(self, kid: list[ast.AstNode]) -> ast.MatchCase:
35893589
match_case_block: KW_CASE pattern_seq (KW_IF expression)? COLON statement_list
35903590
"""
35913591
pattern = kid[1]
3592-
guard = kid[3] if len(kid) > 4 else None
3592+
guard = kid[3] if isinstance(kid[3], ast.Expr) else None
35933593
stmts = [i for i in kid if isinstance(i, ast.CodeBlockStmt)]
35943594
if isinstance(pattern, ast.MatchPattern) and isinstance(
35953595
guard, (ast.Expr, type(None))

jac/jaclang/compiler/passes/main/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .registry_pass import RegistryPass # noqa: I100
1414

1515

16-
pass_schedule = py_code_gen
16+
pass_schedule = py_code_gen # type: ignore[has-type]
1717

1818
__all__ = [
1919
"SubNodeTabPass",

0 commit comments

Comments
 (0)