biocad / openapi3

OpenAPI 3.0 data model
BSD 3-Clause "New" or "Revised" License
39 stars 54 forks source link

Invalid schema with types with primes (`'`) #57

Open brprice opened 2 years ago

brprice commented 2 years ago

When generically deriving a schema for a type whose name contains a ' we (might) generate a malformed spec. (Such a type may come from being defined to have such a name, or mentioning a promoted constructor (with DataKinds).) If the openapi3 generates a named schema object, it will be named the same as the type (applied to parameters, with spaces replaced by underscores). Unfortunately, the spec https://swagger.io/specification/#components-object says this is invalid:

All the fixed fields declared above are objects that MUST use keys that match the regular expression: ^[a-zA-Z0-9\.\-_]+$

Both the online swagger editor and openapi-generator-cli validate reject the schema as invalid.

For example, consider the program

{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeApplications #-}

module Main where

import qualified Data.ByteString.Lazy.Char8 as B
import Data.OpenApi
import Data.OpenApi.Declare
import Data.OpenApi.Internal.Utils
import Data.Proxy
import GHC.Generics

data A = A {x :: T'}
 deriving Generic

data T' = T'
  deriving Generic

instance ToSchema A
instance ToSchema T'

main :: IO ()
main = B.putStrLn $ encodePretty $  execDeclare (declareSchemaRef $ Proxy @A) mempty

This prints

{
    "A": {
        "properties": {
            "x": {
                "$ref": "#/components/schemas/T'"
            }
        },
        "required": [
            "x"
        ],
        "type": "object"
    },
    "T'": {
        "enum": [
            "T'"
        ],
        "type": "string"
    }
}

Notice that the map keys include the character '.

The solutions I can see are: