Giter Club home page Giter Club logo

json2jsii's People

Contributors

aws-cdk-automation avatar campionfellin avatar cdklabs-automation avatar chriscbr avatar dependabot[bot] avatar eladb avatar github-actions[bot] avatar heckler1 avatar iliapolo avatar mergify[bot] avatar mrarnoldpalmer avatar mrgrain avatar njlynch avatar otaviomacedo avatar rassie avatar rix0rrr avatar romainmuller avatar vinayak-kukreja avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

json2jsii's Issues

Code generation fails if `ref` does not start with `#/definitions/`

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,

Code generation fails if `items` are not present or items is not of `object` type

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

Union types accept "any" value

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(),
},

`additionalProperties` && `properties` && `enum` fail when type is defined as array

Duplicate static `from*` helper methods are produced

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) {
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.