quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

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

Open BadMagic100 opened this issue 2 years ago • 8 comments

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

BadMagic100 avatar Jul 12 '23 02:07 BadMagic100

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 avatar Jul 20 '23 15:07 ulazdins-afs

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?

ulazdins-afs avatar Jul 20 '23 15:07 ulazdins-afs

The problem is caused by boolean constants.

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

timbunce avatar Aug 14 '23 11:08 timbunce

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

DanielBretzigheimer avatar Aug 16 '23 17:08 DanielBretzigheimer

@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

DanielBretzigheimer avatar Aug 17 '23 18:08 DanielBretzigheimer

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 :)

ulazdins-afs avatar Aug 18 '23 13:08 ulazdins-afs

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

black-puppydog avatar Oct 25 '23 11:10 black-puppydog

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

patrickocoffeyo avatar Aug 13 '24 20:08 patrickocoffeyo