From 181738dd27eca5f373368296946bf551d0875586 Mon Sep 17 00:00:00 2001 From: Chris Rybicki Date: Thu, 10 Oct 2024 11:39:18 -0400 Subject: [PATCH] fix(compiler): unexpected identifier 'description' (#7194) Fixes a bug with how we generate properties, a regression from https://github.com/winglang/wing/pull/7190 ## Checklist - [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [x] Description explains motivation and solution - [x] 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)*. --- packages/@winglang/wingc/src/json_schema_generator.rs | 2 +- tests/valid/struct_from_json.test.w | 4 ++++ .../valid/struct_from_json.test.w_compile_tf-aws.md | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/@winglang/wingc/src/json_schema_generator.rs b/packages/@winglang/wingc/src/json_schema_generator.rs index 1efcf86cf89..417d5c9d2b5 100644 --- a/packages/@winglang/wingc/src/json_schema_generator.rs +++ b/packages/@winglang/wingc/src/json_schema_generator.rs @@ -88,7 +88,7 @@ impl JsonSchemaGenerator { code.append("type:\"object\","); code.append(format!( - "patternProperties: {{\".*\":{}}}", + "patternProperties: {{\".*\":{}}},", self.get_struct_schema_field(t, None) )); diff --git a/tests/valid/struct_from_json.test.w b/tests/valid/struct_from_json.test.w index e87fff49f16..d223893e6b2 100644 --- a/tests/valid/struct_from_json.test.w +++ b/tests/valid/struct_from_json.test.w @@ -345,3 +345,7 @@ test "unsafe flight" { // Check that imported files can use .fromJson on structs defined within them new otherExternalStructs.UsesStructInImportedFile(); + +let resp = cloud.ApiResponse.fromJson({ status: 200, body: "ok"}); +expect.equal(resp.status, 200); +expect.equal(resp.body, "ok"); diff --git a/tools/hangar/__snapshots__/test_corpus/valid/struct_from_json.test.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/struct_from_json.test.w_compile_tf-aws.md index cb28daf530a..5eca7b2987b 100644 --- a/tools/hangar/__snapshots__/test_corpus/valid/struct_from_json.test.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/valid/struct_from_json.test.w_compile_tf-aws.md @@ -202,6 +202,7 @@ class $Root extends $stdlib.std.Resource { const Foosible = $stdlib.std.Struct._createJsonSchema({$id:"/Foosible",type:"object",properties:{f:{type:"string"},},required:[],description:""}); const MyStruct = $stdlib.std.Struct._createJsonSchema({$id:"/MyStruct",type:"object",properties:{color:{type:"string",enum:["red", "green", "blue"],description:"color docs\n@example Color.red"},m1:{type:"object",properties:{val:{type:"number",description:"val docs"},},required:["val",],description:"m1 docs"},m2:{type:"object",properties:{val:{type:"string"},},required:["val",],description:"m2 docs"},},required:["color","m1","m2",],description:"MyStruct docs\n@foo bar"}); const Student = $stdlib.std.Struct._createJsonSchema({$id:"/Student",type:"object",properties:{additionalData:{type:["object","string","boolean","number","array"]},advisor:{type:"object",properties:{dob:{type:"object",properties:{day:{type:"number"},month:{type:"number"},year:{type:"number"},},required:["day","month","year",],description:""},employeeID:{type:"string"},firstName:{type:"string"},lastName:{type:"string"},},required:["dob","employeeID","firstName","lastName",],description:""},coursesTaken:{type:"array",items:{type:"object",properties:{course:{type:"object",properties:{credits:{type:"number"},name:{type:"string"},},required:["credits","name",],description:""},dateTaken:{type:"object",properties:{day:{type:"number"},month:{type:"number"},year:{type:"number"},},required:["day","month","year",],description:""},grade:{type:"string"},},required:["course","dateTaken","grade",],description:""}},dob:{type:"object",properties:{day:{type:"number"},month:{type:"number"},year:{type:"number"},},required:["day","month","year",],description:""},enrolled:{type:"boolean"},enrolledCourses:{type:"array",uniqueItems:true,items:{type:"object",properties:{credits:{type:"number"},name:{type:"string"},},required:["credits","name",],description:""}},firstName:{type:"string"},lastName:{type:"string"},schoolId:{type:"string"},},required:["dob","enrolled","firstName","lastName","schoolId",],description:""}); + const cloud_ApiResponse = $stdlib.std.Struct._createJsonSchema({$id:"/ApiResponse",type:"object",properties:{body:{type:"string",description:"The response\'s body.\n@default - no body\n@stability experimental"},headers:{type:"object",patternProperties: {".*":{type:"string"}},description:"The response\'s headers.\n@default {}\n@stability experimental",},status:{type:"number",description:"The response\'s status code.\n@default 200\n@stability experimental"},},required:[],description:"Shape of a response from a inflight handler.\n@stability experimental"}); const cloud_CounterProps = $stdlib.std.Struct._createJsonSchema({$id:"/CounterProps",type:"object",properties:{initial:{type:"number",description:"The initial value of the counter.\n@default 0\n@stability experimental"},},required:[],description:"Options for `Counter`.\n@stability experimental"}); const externalStructs_MyOtherStruct = $stdlib.std.Struct._createJsonSchema({$id:"/MyOtherStruct",type:"object",properties:{data:{type:"object",properties:{val:{type:"number",description:"val docs"},},required:["val",],description:"MyStruct docs in subdir"},},required:["data",],description:""}); $helpers.nodeof(this).root.$preflightTypesMap = $preflightTypesMap; @@ -487,6 +488,9 @@ class $Root extends $stdlib.std.Resource { $macros.__Struct_fromJson(false, Student, ({"obviously": "not a student"}), { unsafe: true }); globalThis.$ClassFactory.new("@winglang/sdk.std.Test", std.Test, this, "test:unsafe flight", new $Closure5(this, "$Closure5")); globalThis.$ClassFactory.new("examples-valid.subdir.UsesStructInImportedFile", otherExternalStructs.UsesStructInImportedFile, this, "UsesStructInImportedFile"); + const resp = $macros.__Struct_fromJson(false, cloud_ApiResponse, ({"status": 200, "body": "ok"})); + (expect.Util.equal(resp.status, 200)); + (expect.Util.equal(resp.body, "ok")); } } const $APP = $PlatformManager.createApp({ outdir: $outdir, name: "struct_from_json.test", rootConstruct: $Root, isTestEnvironment: $wing_is_test, entrypointDir: process.env['WING_SOURCE_DIR'], rootId: process.env['WING_ROOT_ID'] });