khrt / Raisin

Raisin - a REST API micro framework for Perl 🐫 🐪
61 stars 30 forks source link

ArrayRef[HashRef] param #71

Open slobo opened 5 years ago

slobo commented 5 years ago

We would like an endpoint to to take a list of objects as an input, ex:

{
  entries: [
   { id: 1, title: 'Entry 1'},
   { id: 2, title: 'Entry 2'}
  ]
}

Naive approach, where we don't validate each object in list would be like this

  params(
    requires(
      entries => (
        type => ArrayRef[HashRef],
        desc => 'Entries',
      )
    ),
  );

This generates following bit in swagger:

{
  "parameters": [
    {
      "required": true,
      "description": "Entries",
      "items": {
        "$ref": "#/definitions/Entries-MD5Hash"
      },
      "in": "body",
      "type": "array",
      "name": "entries"
    }
  ]
}

But there is no definition of Entries-MD5Hash in definitions section in swagger, which makes the swagger.json out of spec.

Ideally, we would be able to define the structure of the HashRef too as part of the definition.

Any suggestions how to deal with this?

Thanks!

slobo commented 5 years ago

[[[egg on face]]] We were using old release, seems there is better behaviour in latest release. will experiment with that a bit first

khrt commented 5 years ago

I can see the very same behaviour on current master.

khrt commented 5 years ago

I assume the issue is in https://github.com/khrt/Raisin/blob/master/lib/Raisin/Plugin/Swagger.pm#L286 where everything which is not HashRef is ignored.

slobo commented 5 years ago

It would be great if we could still use group syntax to describe the hashref that's in arrayref.

On Sun., Jul. 28, 2019, 09:18 Artur Khabibullin, notifications@github.com wrote:

I assume the issue is in https://github.com/khrt/Raisin/blob/master/lib/Raisin/Plugin/Swagger.pm#L286 where everything which is not HashRef is ignored.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/khrt/Raisin/issues/71?email_source=notifications&email_token=AABI6XAZZHYI7H6ZQSBESHTQBXBGZA5CNFSM4IHHE3R2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD27BYNY#issuecomment-515775543, or mute the thread https://github.com/notifications/unsubscribe-auth/AABI6XCDA3GXZGZDAR5UBD3QBXBGZANCNFSM4IHHE3RQ .

khrt commented 5 years ago

I've kinda fixed it. The fix is that if type of a parameter is ArrayRef[.+] the plugin generates parameters of type array with items any-type. See https://swagger.io/docs/specification/data-models/data-types/

Not sure if it's what you want, but at least the spec is valid now.

khrt commented 5 years ago

Released to CPAN to anyway, even if it doesn't solve your issue.

slobo commented 5 years ago

Thanks for such quick action, this helps! I'll keep the ticket open in hopes we can get more extensive ArrayRef[HashRef] support such that any deep nested JSON structure can be described eventually