redis/redis-om-python

Redis-OM with FastAPI : pk and Additional properties

codot-fr opened this issue · 5 comments

Hello,

I've followed the doc concerning Redis-OM with FastAPI and I'm a bit surprised to see automatic fields like the "pk" appearing in the API model for a POST method.

models

class RemoteOS(HashModel):
    ip_address: IPvAnyAddress
    login: str
    password: SecretStr

api

@app.post("/remote-os")
def add_remote_os(remoteOS: RemoteOS):
    return remoteOS.save()

Exposed schema in the fast api doc "request body" for the API:

{
  "pk": "string",
  "ip_address": "string",
  "login": "string",
  "password": "********",
  "additionalProp1": {}
}

I would expect only the following:

{
  "ip_address": "string",
  "login": "string",
  "password": "********"
}

What am I missing?

I'm using the following libraries:

fastapi==0.112.1
pydantic==2.8.2
redis==5.0.8
redis-om==0.3.2

Thanks.

I don't think we have anything to do with the "additionalprop1" field, that appears to be a swagger issue: fastapi/fastapi#9820, however the pk field is part of the model, it's part of the RedisModel and is applied to all models underneath it, it is what we use to build the key names for the records and allow direct lookups of keys in Redis (remember we're a key-value store when all is said and done.)

Good call for the additionalprop field!

I understand the mechanic of the pk, but as it's auto-generated it shouldn't appear in the API documentation, right? I mean, it's confusing for an end user of the API that would think he needs to set a value to the pk.

https://stackoverflow.com/questions/74137116/how-to-hide-a-pydantic-discriminator-field-from-fastapi-docs similar question to yours looks like you may be able to drop it via configuraiton.

Fundamentally though, if you are having your model pull double duty as a request object (or triple duty if you are using it as a response object) having its properties show up in your auto-generated OAS spec is both foreseeable and valid.

Thanks for your help!

Just to add on to this, if you want to set your own pk, there is an option to set on of your Field fields in your Pydantic schema to be the primary key.

class SomeSchema(JsonModel):
    my_pk: str = Field(primary_key=True)