cdklabs / json2jsii Goto Github PK
View Code? Open in Web Editor NEWGenerates jsii-compatible structs from JSON schemas
License: Apache License 2.0
Generates jsii-compatible structs from JSON schemas
License: Apache License 2.0
Currently code generation for some schemas is failing due to ref
not starting with #/definitions/
. Here is where the error is being thrown: https://github.com/cdklabs/json2jsii/blob/main/src/type-generator.ts#L514-L518
Here are some examples of schemas that are failing with this,
Currently code generation is failing for properties of type array
if items in a schema definition are either not present or their type is not object. Here are few examples of it,
Here is where the error is being thrown: https://github.com/cdklabs/json2jsii/blob/main/src/type-generator.ts#L536-L542
When referencing the value of a generated union type, such as k8s.Quantity
, the value is an any
type. This can lead to unexpected output that does not meet the schemas being rendered at runtime, due to the lack of type checking.
For example, given a k8s.EnvVar like:
const cpu = Quantity.fromNumber(2)
{
name: "GOMAXPROCS",
// EnvVar.value is of type 'string | undefined'
// cpu.value is of type 'any', which fulfills 'undefined'
value: cpu.value,
},
Because type checking is skipped for an any
, and cpu
was created from a number, when this value is rendered to JSON it is still a number, and thus the rendered JSON is invalid:
// Actual, invalid result:
{
"name": "GOMAXPROCS",
"value": 2
}
// Desired, valid result:
{
"name": "GOMAXPROCS",
"value": "2"
}
In order to provide useful type data, instead of any
, I propose that the type of the union's value should be a union of the possible input types.
With this change, the example from above now will fail type checking, and provide a useful error:
// This code now fails to compile
const cpu = Quantity.fromNumber(2)
{
name: "GOMAXPROCS",
// EnvVar.value is of type 'string | undefined'
// cpu.value is of type 'string | number', which cannot fulfill the expected type
value: cpu.value,
},
// This code compiles and results in valid JSON that meets the schemas
const cpu = Quantity.fromNumber(2)
{
name: "GOMAXPROCS",
value: cpu.value.toString(),
},
Right now only primitive types are supported as unions.
What would support for interfaces look like?
json2jsii/src/type-generator.ts
Lines 576 to 578 in b6091c8
json2jsii/src/type-generator.ts
Lines 350 to 352 in b6091c8
Could this tool be extended to auto-generate AWS CDK constructs for CloudFormation 3rd party resources?
Examples:
Currently code generation fails if there is a nested local reference present. Here is where the error is thrown: https://github.com/cdklabs/json2jsii/blob/main/src/type-generator.ts#L543-L557
Here is an example of schema that fails with this: https://github.com/vinayak-kukreja/SchemaValidation/blob/main/chart-schemas/alertmanager.json#L153-L227
Could you please document the limitations of this tool as far as what subset of JSON Schema it can understand?
I tried running it against a complex JSON Schema, only to get very surprising useless results. I am thinking that it is probably just too weird for this tool to understand.
Was trying to apply the generator on this schema: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/github-workflow.json
Here are some schemas that this fails for,
Given the following snippet from a JSON Schema:
{
"shell": {
"anyOf": [
{
"type": "string"
},
{
"type": "string",
"enum": ["bash", "pwsh", "python", "sh", "cmd", "powershell"]
}
]
}
}
Will produce invalid code, where fromString
is duplicated:
export class Shell {
public static fromString(value: string): Shell {
return new Shell(value);
}
public static fromString(value: string): Shell {
return new Shell(value);
}
private constructor(public readonly value: string | string) {
}
}
According to https://github.com/aws/json2jsii/blob/d39695dcdef3a6cee0723b28d0d85e1868a56200/src/type-generator.ts#L289-L291 underscores are not supported in property names.
What is the reasoning for this? Can this check be removed such that one can omit json/yaml with underscores in names?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.