Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(compiler): cannot use fromJson on jsii imported struct (#4040)
## Summary So this is actually quite a substantial refactor to how we generate jsonschemas for structs. Previously anytime we encountered a user defined wing struct we generated a json schema file with a schema and fromJson methods. However this meant that we were not created schema files for structs defined outside of the wing code (I.E. JSII imported). With the new changes I am taking a lazy creation approach where we only generate schemas for structs if there are references that would require the schema to exist. This change also no longer generates a separate file for the schema, instead it creates an instance of a stdlib.std.StructSchema using the jsonschema inline. The other big change is the removal of the require statements and defs from schemas that were dependent on other schemas. As in take for example this Wing struct: ```js struct A { val: num; } struct B { data: str; } struct C extends A { b: B; } ``` The struct schema for `C` used to look like this: ```js { id: "/C", type: "object", properties: { ...require("./A.Struct.js")().jsonSchema().properties, b: { "$ref": "#/$defs/B" }, }, required: [ "b", ...require("./A.Struct.js")().jsonSchema().required, ], $defs: { "B": { type: "object", "properties": require("./B.Struct.js")().jsonSchema().properties }, ...require("./A.Struct.js")().jsonSchema().$defs, } } ``` Now it looks like this: ```js { id: "/C", type: "object", properties: { b: { type: "object", properties: { data: { type: "string" }, }, required: [ "data", ] }, val: { type: "number" }, }, required: [ "b", "val", ] } ``` ### Implementation Notes 1. Lazy creation of schemas, only emit struct schema files if there exists a reference to the struct type in the code. 2. Schemas no longer consist of require statements for dependent schemas. As in the schemas are now fully self contained. 3. Pre-Jsification parse of the ast to emit struct files for referenced schemas. Closes: #3943 Closes: #3790 ## 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) - [x] Docs updated (only required for features) - [x] 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