Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using hashmaps produces invalid openapi schema #6800

Open
ostrolucky opened this issue Nov 15, 2024 · 4 comments
Open

Using hashmaps produces invalid openapi schema #6800

ostrolucky opened this issue Nov 15, 2024 · 4 comments

Comments

@ostrolucky
Copy link
Contributor

API Platform version(s) affected: 4.0.9

Description

How to reproduce

#[ApiResource]
class Test {
    /** @var array<string, Foo> */
    public array $foos;
}

class Foo {
    public string $bar;
}

above produces schema like so

"Test": {
  "type": "object",
  "description": "",
  "deprecated": false,
  "properties": {
    "foos": {
      "type": "object",
      "additionalProperties": {
        "type": "unknown_type"
      }
    }
  }
},

since "unknown_type" isn't actually recognized by OpenAPI, openapi schema validators will reject this kind of schema.

Instead, I would expect it generates schema like so

"Test": {
  "type": "object",
  "description": "",
  "deprecated": false,
  "properties": {
    "foos": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "properties": {"bar": {"type": "string"}}
      }
    }
  }
},

Possible Solution

Additional Context

@vinceAmstoutz
Copy link
Contributor

@ostrolucky Have you tried with Foo[] or array<Foo>?

@ostrolucky
Copy link
Contributor Author

ostrolucky commented Nov 18, 2024

That's not accurate though, for us it's really a hashmap like so

{
  "foos": { 
    "randomProp123" : {
      "bar: "string"
    },
    "randomProp321" : {
      "bar": "string"
    }
  }
}

hence needs to be reflected in php docblocks and openapi docs as such. Especially openapi docs because they are consumed by FE devs and for them it makes a big difference treating the prop as object or array.

In places where we don't use hashmap but list, that's documented as list<Foo> and generated openapi docs is correct, hence this issue is only about hashmaps.

@soyuka
Copy link
Member

soyuka commented Nov 22, 2024

Do you have phpstan/phpdoc-parser ? Can you try adding phpdocumentor/reflection-docblock ?
We do handle the case of an embeded object I don't get why the array makes any difference. Could you check at:

Type::BUILTIN_TYPE_OBJECT => $this->getClassType($type->getClassName(), $type->isNullable(), $readableLink),

@ostrolucky
Copy link
Contributor Author

I have both packages installed. To me it seems issue is that $keyType is not passed to getType

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants