glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL
https://app.quicktype.io
Apache License 2.0
12.34k stars 1.07k forks source link

CLI type generation fails - s.codePointAt is not a function #2333

Open BadMagic100 opened 1 year ago

BadMagic100 commented 1 year ago

I generate types as a pre-build step for one of my libraries. On the latest release, type generation has begun to fail unexpectedly with no change to input. See the error message occuring in my build workflow here: https://github.com/hpackage/HPackage.Net/actions/runs/5503071564/jobs/10081743010#step:7:15

Last known successful run was July 2nd on release 23.0.49

ulazdins-afs commented 1 year ago

Here is a JSON Schema file that will produce the error:

{
  "anyOf": [
    {
      "$ref": "#/definitions/ConditionCheckResponseValid"
    },
    {
      "$ref": "#/definitions/ConditionCheckResponseInvalid"
    }
  ],
  "definitions": {
    "ConditionCheckResponseValid": {
      "type": "object",
      "properties": {
        "valid": {
          "type": "boolean",
          "const": true
        }
      },
      "additionalProperties": false,
      "required": ["valid"]
    },
    "ConditionCheckResponseInvalid": {
      "type": "object",
      "properties": {
        "valid": {
          "type": "boolean",
          "const": false
        },
        "reason": {
          "type": "string"
        }
      },
      "additionalProperties": false,
      "required": ["reason", "valid"]
    }
  },
  "$schema": "http://json-schema.org/draft-07/schema#"
}

And you get this error:

quicktype ConditionCheckResponseDTO.json  -s schema -l swift --just-types
Error: s.codePointAt is not a function.

A slightly different version of JSON Schema with basically the same information work as expected:

{
  "anyOf": [
    {
      "$ref": "#/definitions/ConditionCheckResponseValid"
    },
    {
      "$ref": "#/definitions/ConditionCheckResponseInvalid"
    }
  ],
  "definitions": {
    "ConditionCheckResponseValid": {
      "type": "object",
      "properties": {
        "valid": {
          "type": "boolean",
          "enum": [true]
        }
      },
      "additionalProperties": false,
      "required": ["valid"]
    },
    "ConditionCheckResponseInvalid": {
      "type": "object",
      "properties": {
        "valid": {
          "type": "boolean",
          "enum": [false]
        },
        "reason": {
          "type": "string"
        }
      },
      "additionalProperties": false,
      "required": ["reason", "valid"]
    }
  },
  "$schema": "http://json-schema.org/draft-07/schema#"
}
quicktype ConditionCheckResponseDTO.json  -s schema -l swift --just-types

// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse the JSON, add this file to your project and do:
//
//   let conditionCheckResponseDTO = try ConditionCheckResponseDTO(json)

import Foundation

// MARK: - ConditionCheckResponseDTO
struct ConditionCheckResponseDTO {
    let valid: Bool
    let reason: String?
}
ulazdins-afs commented 1 year ago

Broken in https://github.com/quicktype/quicktype/pull/2314

The problem is caused by boolean constants. In that case a boolean gets passed to function splitIntoWords(s: string): WordInName[] where a string is expected causing the "Error: s.codePointAt is not a function." error.

https://github.com/DanielBretzigheimer 👋 You seem to be the author of the PR - do you have an idea how to fix this bug?

timbunce commented 1 year ago

The problem is caused by boolean constants.

Integer constants (in Go) are also broken by #2314. /cc @DanielBretzigheimer

DanielBretzigheimer commented 1 year ago

Apologies for my delayed response, I will look into it in the following days and respond here.

DanielBretzigheimer commented 1 year ago

@timbunce @ulazdins-afs @BadMagic100 could you please verify if my PR resolves your issues? I tried the two samples and an integer constant and they should work now. If you get another error, please provide the JSON Schema and I will take a look at it. Thanks for your patience, and I apologize for the problems caused by my PR

ulazdins-afs commented 1 year ago

No problem and thank you for fixing! I don't have any more examples, so If it works for you, then I think it's fine. The idea that number/bool constants won't be converted to JSONSchema constants seems sub-optimal, but it's better than crashing :)

black-puppydog commented 11 months ago

For me the PR resolves the issue, thank you for the fix! 🙂

patrickocoffeyo commented 2 months ago

Are there any plans to include a fix for this in an upcoming release? I am running into the same issue.