Skip to content

ToSchema (Map k v) instance can ignore the required ToSchema k instance #123

Description

@isomorpheme

Currently, the instance is this:

instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (Map k v) where
  declareNamedSchema _ = case toJSONKey :: ToJSONKeyFunction k of
      ToJSONKeyText  _ _ -> declareObjectMapSchema
      ToJSONKeyValue _ _ -> declareNamedSchema (Proxy :: Proxy [(k, v)])
    where
      declareObjectMapSchema = do
        schema <- declareSchemaRef (Proxy :: Proxy v)
        return $ unnamed $ mempty
          & type_ ?~ OpenApiObject
          & additionalProperties ?~ AdditionalPropertiesSchema schema

so when toJSONKey returns a ToJSONKeyText, the resulting schema only uses the schema for v to define additionalProperties, but it doesn't do anything with to schema for k. This means that if k's schema & FromJSONKey instance do restrict what keys are valid, this is silently ignored and the generated schema is incorrect.

JSON Schema has a propertyNames keyword as of draft 6 (source) that could be used to define a correct schema, but I don't know if that's also included yet in the OpenAPI version that this library targets. If propertyNames can't be used, then IMO the instance should be changed to be more restrictive, so that k has to have an equivalent schema to Text.

This also affects the instance for HashMap since that just reuses the instance for Map.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions