Skip to content

Discriminated Union Cases with Special Characters Need Better Sanitization #59

@houstonhaynes

Description

@houstonhaynes

When OpenAPI enum values contain special characters (like @, /, -, .), Hawaii generates F# discriminated union cases that require backticks or have invalid identifiers. Better sanitization with [<CompiledName>] attributes would improve code quality.

OpenAPI Source

# From Cloudflare Vectorize OpenAPI spec
vectorizeindex-preset:
  type: string
  enum:
    - "@cf/baai/bge-small-en-v1.5"      # Special characters: @, /, -, .
    - "@cf/baai/bge-base-en-v1.5"
    - "openai/text-embedding-ada-002"
    - "cohere/embed-multilingual-v2.0"

Current Hawaii Output

// From CloudflareFS: src/Management/CloudFlare.Management.Vectorize/Types.fs (line 40)
[<Fable.Core.StringEnum; RequireQualifiedAccess>]
type ``vectorizeindex-preset`` =
    | [<CompiledName "@cf/baai/bge-small-en-v1.5">] ``@cfBaaiBgeSmallEnV1Numeric_5``
    | [<CompiledName "@cf/baai/bge-base-en-v1.5">] ``@cfBaaiBgeBaseEnV1Numeric_5``
    | [<CompiledName "openai/text-embedding-ada-002">] OpenaiTextEmbeddingAdaNumeric_002
    member this.Format() =
        match this with
        | ``@cfBaaiBgeSmallEnV1Numeric_5`` -> "@cf/baai/bge-small-en-v1.5"
        | ``@cfBaaiBgeBaseEnV1Numeric_5`` -> "@cf/baai/bge-base-en-v1.5"
        // ...

Problems

  1. Backticks required: Cases starting with @ need backticks (`@cfBaaiBge...`)
  2. Inconsistent naming: Some cases have backticks, others don't
  3. Poor readability: Numeric_5 suffix is unclear (represents .5 from version number)
  4. Pattern matching awkward: Users must remember which cases need backticks

Proposed Improvement

[<Fable.Core.StringEnum; RequireQualifiedAccess>]
type ``vectorizeindex-preset`` =
    | [<CompiledName "@cf/baai/bge-small-en-v1.5">] CfBaaiBgeSmallEnV15
    | [<CompiledName "@cf/baai/bge-base-en-v1.5">] CfBaaiBgeBaseEnV15
    | [<CompiledName "@cf/baai/bge-large-en-v1.5">] CfBaaiBgeLargeEnV15
    | [<CompiledName "openai/text-embedding-ada-002">] OpenAiTextEmbeddingAda002
    | [<CompiledName "cohere/embed-multilingual-v2.0">] CohereEmbedMultilingualV20
    member this.Format() =
        match this with
        | CfBaaiBgeSmallEnV15 -> "@cf/baai/bge-small-en-v1.5"
        | CfBaaiBgeBaseEnV15 -> "@cf/baai/bge-base-en-v1.5"
        // ...

Benefits

// Clean pattern matching - no backticks needed
match preset with
| CfBaaiBgeSmallEnV15 -> "Small model"
| CfBaaiBgeBaseEnV15 -> "Base model"
| OpenAiTextEmbeddingAda002 -> "OpenAI model"

// JSON serialization still works correctly:
// "@cf/baai/bge-small-en-v1.5"

Sanitization Rules

  1. Remove leading special characters (@, -, etc.)
  2. Convert /, -, . to word boundaries for PascalCase
  3. Convert version numbers intelligently: v1.5V15, not V1Numeric_5
  4. Always use [<CompiledName>] to preserve original JSON value
  5. Ensure all cases use consistent naming (all with or all without backticks)

CloudflareFS Workaround

We accept the generated backticks and work with them in pattern matching. The Format() member method provides a way to get the original string value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions