Coder Social home page Coder Social logo

Comments (15)

dantownsend avatar dantownsend commented on June 21, 2024 1

@sinisaos That would be great, thanks

from piccolo_api.

dantownsend avatar dantownsend commented on June 21, 2024

@gmos Thanks for reporting this.

PiccoloCRUD should support arrays - there could be a bug somewhere.

In the example below, the years_nominated field is an array. The API should render it like a list.

Screenshot 2021-12-02 at 10 05 43

Is the problem when trying to post a new value back?

from piccolo_api.

sinisaos avatar sinisaos commented on June 21, 2024

@gmos @dantownsend POST also work as expected.

post

from piccolo_api.

dantownsend avatar dantownsend commented on June 21, 2024

@sinisaos Thanks for checking that.

from piccolo_api.

gmos avatar gmos commented on June 21, 2024

Screenshots are from the swagger .../docs page. I expanded the root GET.

Parameters docs:
image
Here it goes wrong. No array structure to be found. The sensors field is depicted as a simple Varchar.

And this is when I make the actual GET call:
image

As you can see the response schema is good. the issue is about the generated parameter input for the root GET.

from piccolo_api.

dantownsend avatar dantownsend commented on June 21, 2024

@gmos Cool, thanks - that's really useful. We'll investigate.

from piccolo_api.

gmos avatar gmos commented on June 21, 2024

Here is the column definition from the Table subclass:

    sensors = Array(
        base_column=Varchar(
            length=16,
            null=False,
        )
    )

And here the automatic creation of the lot.

hoses_router = APIRouter()
hoses_crud = PiccoloCRUD(TempHoses, read_only=False)

FastAPIWrapper(
    "/hoses",
    fastapi_app=cast(FastAPI, hoses_router),  # TODO Get away with the cast
    piccolo_crud=hoses_crud,
    fastapi_kwargs=FastAPIKwargs(
        all_routes={"tags": ["TempHoses"]},
    ),
)

Side question: why do I need a cast to get rid of the type warning? AM I doing something wrong assigning a PiccoloCRUD instance to the FastAPIwrapper?

from piccolo_api.

dantownsend avatar dantownsend commented on June 21, 2024

@gmos The type error might be because you're using FastAPIWrapper with an APIRouter instance, rather than a FastAPI instance.

We probably just need to update this type annotation to be t.Union[FastAPI, APIRouter]:

fastapi_app: FastAPI,

from piccolo_api.

sinisaos avatar sinisaos commented on June 21, 2024

@dantownsend Problem is when you use openapi request url is http://localhost:8000/posts/?tags=tag (which raise Pydantic ValidationError value is not a valid list (type=type_error.list)) and it should be http://localhost:8000/posts/?tags[]=tag like in Piccolo Admin or if you pass request url directly in browser to get correct result.

from piccolo_api.

dantownsend avatar dantownsend commented on June 21, 2024

@sinisaos Well remembered - I forgot about that.

I wonder if we can add a custom validator to the Pydantic model:

https://pydantic-docs.helpmanual.io/usage/validators/

from piccolo_api.

sinisaos avatar sinisaos commented on June 21, 2024

@dantownsend I think I found a problem in openapi filters. We need to use ModeField.outer_type_

type_ = _field.type_
because ModeField.type_ shows the wrong type (class int or str insted List[int] or List[str]).
We need to change the line 401 to this type_ = _field.outer_type_ and the result is.

arrays

Then I add an extra check in crud endpoints _parse_params method for the array columns so that openapi request url accepts multiple query params without square brackets. Method now looks like this

def _parse_params(self, params: QueryParams) -> t.Dict[str, t.Any]:
  params_map: t.Dict[str, t.Any] = {
      i[0]: [j[1] for j in i[1]]
      for i in itertools.groupby(params.multi_items(), lambda x: x[0])
  }

  array_columns = [
      i._meta.name
      for i in self.table._meta.columns
      if i.value_type == list
  ]

  output = {}

  for key, value in params_map.items():
      if key.endswith("[]") or key.rstrip("[]") in array_columns:
          # Is either an array, or multiple values have been passed in
          # for another field.
          key = key.rstrip("[]")
      elif len(value) == 1:
          value = value[0]

      output[key] = value

  return output

And result is

query

Piccolo Admin and request directly in browser remain unchanged.

@gmos Can you please check this on your local Piccolo Api installation and confirm that everything works.?

from piccolo_api.

dantownsend avatar dantownsend commented on June 21, 2024

@sinisaos That's some good investigative work - thanks.

When we make this change we'll also have to update Piccolo Admin at the same time.

from piccolo_api.

dantownsend avatar dantownsend commented on June 21, 2024

@sinisaos Ah, just realised you managed to make it compatible with the current Piccolo Admin, great.

from piccolo_api.

sinisaos avatar sinisaos commented on June 21, 2024

@dantownsend Yes, Piccolo Admin works. If you want I can do PR.

from piccolo_api.

dantownsend avatar dantownsend commented on June 21, 2024

I've released it to PyPI now - version 0.29.2.

Seems to fix the problem. Thanks both.

from piccolo_api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.