Skip to content

Commit

Permalink
fix(compiler): Json? can be provided where Json is expected (#6679)
Browse files Browse the repository at this point in the history
fixes: #6061 

## Checklist

- [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [ ] Description explains motivation and solution
- [ ] Tests added (always)
- [ ] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
tsuf239 authored Jun 14, 2024
1 parent 6f4236b commit 0b73a60
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
27 changes: 27 additions & 0 deletions examples/tests/invalid/json_is_not_nil.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
let innerOptional = (): Json? => {
return nil;
};

let outerJson = (x: Json) => {

};


let innerJson = (): Json => {
return {};
};

let outerOptional = (x: Json?) => {

};


// those are ok
outerJson(innerJson());
outerOptional(innerOptional());
outerOptional(innerJson());

// this is problematic as json? can be nil, and nil is not json
outerJson(innerOptional());
//^^^^^^^^^^^^^^^ Expected type to be "Json", but got "Json?" instead

4 changes: 2 additions & 2 deletions examples/tests/sdk_tests/resource/call.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ class MyResource {
}
pub inflight methodOptNum1(arg: num?): num? {
// TODO: need a way to convert from Json to num?
return unsafeCast(this.backend.call("methodOptNum1", Json [arg]));
return unsafeCast(this.backend.call("methodOptNum1", Json [arg ?? 0]));
}
pub inflight methodOptNum2(arg: num?): num? {
// TODO: need a way to convert from Json to num?
return unsafeCast(this.backend.call("methodOptNum2", Json [arg]));
return unsafeCast(this.backend.call("methodOptNum2", Json [arg ?? 0]));
}
pub inflight methodJson(arg: Json): Json {
return this.backend.call("methodJson", [arg]);
Expand Down
4 changes: 2 additions & 2 deletions examples/tests/sdk_tests/table/get.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test "get" {
table.get(NON_EXISTENT_KEY);
});

let var result: Json = table.tryGet(VALID_KEY);
assert(result.get(COLUMN_NAME) == COLUMN_VALUE);
let var result: Json? = table.tryGet(VALID_KEY);
assert(result?.get(COLUMN_NAME) == COLUMN_VALUE);
assert(table.tryGet(NON_EXISTENT_KEY) == nil);
}
7 changes: 5 additions & 2 deletions libs/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,6 @@ impl TypeRef {
Type::Inferred(..) => true,
Type::Array(v) => v.is_json_legal_value(),
Type::Map(v) => v.is_json_legal_value(),
Type::Optional(v) => v.is_json_legal_value(),
Type::Struct(ref s) => {
for (_, t) in s.fields(true) {
if !t.is_json_legal_value() {
Expand Down Expand Up @@ -3489,7 +3488,11 @@ new cloud.Function(@inflight("./handler.ts"), lifts: { bucket: ["put"] });
));
}

if matches!(**return_type.maybe_unwrap_option(), Type::Json(None) | Type::MutJson) {
if matches!(**return_type.maybe_unwrap_option(), Type::Json(None) | Type::MutJson)
&& !matches!(
**first_expected_type.maybe_unwrap_option(),
Type::Json(None) | Type::MutJson
) {
// known json data is statically known
hints.push(format!(
"use {first_expected_type}.fromJson() to convert dynamic Json\""
Expand Down
15 changes: 15 additions & 0 deletions tools/hangar/__snapshots__/invalid.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3135,6 +3135,21 @@ error: \\"Bucket\\" is not a legal JSON value



Tests 1 failed (1)
Snapshots 1 skipped
Test Files 1 failed (1)
Duration <DURATION>"
`;

exports[`json_is_not_nil.test.w 1`] = `
"error: Expected type to be \\"Json\\", but got \\"Json?\\" instead
--> ../../../examples/tests/invalid/json_is_not_nil.test.w:25:11
|
25 | outerJson(innerOptional());
| ^^^^^^^^^^^^^^^



Tests 1 failed (1)
Snapshots 1 skipped
Test Files 1 failed (1)
Expand Down

0 comments on commit 0b73a60

Please sign in to comment.