Skip to content

Better rendering for HashMaps w/ OpenAPI #1133

@disconsented

Description

@disconsented

Is your feature request related to a problem? Please describe.
I've been trying to understand why the ToSchema doesn't render as expected, I've worked out that this is due to structs with HashMap<String, T> being expanded into a generic object type like below.

{
  "openapi": "3.1.0",
  "info": {
    "title": "inkwell api",
    "version": "0.0.1"
  },
  "paths": {},
  "components": {
    "schemas": {
      "document.Document": {
        "type": "object",
        "required": [
          "id",
          "order",
          "colour",
          "title",
          "category",
          "tags",
          "template",
          "fields"
        ],
        "properties": {
          "category": {
            "type": "string"
          },
          "colour": {
            "$ref": "#/components/schemas/block.Colour"
          },
          "fields": {
            "type": "object",
            "additionalProperties": true
          },
          "id": {
            "type": "string"
          },
          "order": {
            "$ref": "#/components/schemas/block.Numerical"
          },
          "tags": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/block.Phrase"
            }
          },
          "template": {
            "type": "string"
          },
          "title": {
            "type": "string"
          }
        }
      }
    }
  }
}

For anyone else who stumbles across the issue before it's resolved, I've worked around it by manually inserting the reference:

    if let RefOr::Type(Schema::Object(schema)) = doc
        .components
        .schemas
        .0
        .get_mut("document.Document")
        .expect("missing Document, has the schema changed?")
    {
        if let RefOr::Type(Schema::Object(fields)) = schema.properties.get_mut("fields").unwrap(){
            fields.additional_properties = Some(Box::new(AdditionalProperties::RefOr(RefOr::Ref(
                schema::Ref::new("#/components/schemas/block.BlockData"),
            ))));
        }
    }

Describe the solution you'd like
OpenAPI does support this kind of type (https://swagger.io/docs/specification/v3_0/data-models/dictionaries/), so it'd be good if we can reference other types here.

Going from:

Object {
    schema_type: Basic(
        Object,
    ),
    name: None,
    format: None,
    description: None,
    default_value: None,
    enum_values: [],
    required: {},
    properties: {},
    additional_properties: Some(
        FreeForm(
            true,
        ),
    ),
    deprecated: None,
    examples: [],
    write_only: None,
    read_only: None,
    xml: None,
    multiple_of: None,
    maximum: None,
    minimum: None,
    exclusive_maximum: None,
    exclusive_minimum: None,
    max_length: None,
    min_length: None,
    pattern: None,
    max_properties: None,
    min_properties: None,
    extensions: {},
    content_encoding: "",
    content_media_type: "",
}

To:

 Object {
    schema_type: Basic(
        Object,
    ),
    name: None,
    format: None,
    description: None,
    default_value: None,
    enum_values: [],
    required: {},
    properties: {},
    additional_properties: Some(
        RefOr(
            Ref(
                Ref {
                    ref_location: "#/components/schemas/SomethingElse",
                    description: "",
                    summary: "",
                },
            ),
        ),
    ),
    deprecated: None,
    examples: [],
    write_only: None,
    read_only: None,
    xml: None,
    multiple_of: None,
    maximum: None,
    minimum: None,
    exclusive_maximum: None,
    exclusive_minimum: None,
    max_length: None,
    min_length: None,
    pattern: None,
    max_properties: None,
    min_properties: None,
    extensions: {},
    content_encoding: "",
    content_media_type: "",
}

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