by Anonymous » 01 Jul 2025, 11:44
Ich habe eine Situation, in der ich den aktiven Benutzer gegen einen der Werte genehmigen möchte (
) In einer Fastapi -Route. Wenn ein Objekt eines bestimmten Typs eingereicht wird, einer der Schlüssel (
) sollte vorhanden sein und der Benutzer sollte überprüft werden, ob er Zugriff auf die Organisation hat.
Code: Select all
def get_organization_from_body(organization_id: str = Body(None),
user: User = Depends(get_authenticated_user),
organization_service: OrganizationService = Depends(get_organization_service),
) -> Organization:
if not organization_id:
raise HTTPException(status_code=400, detail='Missing organization_id.')
organization = organization_service.get_organization_for_user(organization_id, user)
if not organization:
raise HTTPException(status_code=403, detail='Organization authorization failed.')
return organization
Dies funktioniert einwandfrei, und wenn der API -Endpunkt in der Anforderung eine Organisation über eine organisation_id -Staste erwartet
Code: Select all
@router.post('', response_model=Bundle)
async def create_bundle([...]
organization: Organization = Depends(get_organization_from_body),
) -> Model:
... Und wenn der Benutzer keinen Zugriff auf die Organisation hat, wird eine 403 -Ausnahme erhöht. Mein erster Versuch war also eine JSON -Anfrage als: < /p>
Code: Select all
{
'name': generated_name,
'organization_id': created_organization['id_key']
}
< /code>
und Hinzufügen meines eingehenden pydantischen Modells: < /p>
@router.post('', response_model=Bundle)
async def create_bundle(bundle: BundleCreate,
organization: Organization = Depends(get_organization_from_body),
[...]
) -> BundleModel:
[...]
return bundle
< /code>
Das Ergebnis ist dasselbe, ob das pydantische Modell /Schema organisation_id < /code> als Feld enthält oder nicht: < /p>
class BundleBase(BaseModel):
name: str
class Config:
orm_mode = True
class BundleCreate(BundleBase):
organization_id: str
client_id: Optional[str]
.. aber wenn ich meine Get_organization_from_body Abhängigkeit einführe, sieht Fastapi, dass ich eine andere Abhängigkeit habe, die sich auf ein Körperfeld bezieht, und die Beschreibung des Bündels. organisation_id ist Teil des Bundle Beschreibung, es sollte dort leben. Wenn möglich).
Code: Select all
{'detail': [{'loc': ['body', 'bundle'], 'msg': 'field required', 'type': 'value_error.missing'}]}
und zu Recht, wenn ich den Namen in einem Bundle Taste stattdessen bewegte:
Code: Select all
{
'bundle': {
'name': generated_name,
},
'organization_id': created_organization['id_key']
}
...My Test Pässe und die Anfrage ist erfolgreich. mein Ausgangsformat näher.
Ich habe eine Situation, in der ich den aktiven Benutzer gegen einen der Werte genehmigen möchte ([code]Organization[/code]) In einer Fastapi -Route. Wenn ein Objekt eines bestimmten Typs eingereicht wird, einer der Schlüssel ([code]organization_id[/code]) sollte vorhanden sein und der Benutzer sollte überprüft werden, ob er Zugriff auf die Organisation hat.[code]def get_organization_from_body(organization_id: str = Body(None),
user: User = Depends(get_authenticated_user),
organization_service: OrganizationService = Depends(get_organization_service),
) -> Organization:
if not organization_id:
raise HTTPException(status_code=400, detail='Missing organization_id.')
organization = organization_service.get_organization_for_user(organization_id, user)
if not organization:
raise HTTPException(status_code=403, detail='Organization authorization failed.')
return organization
[/code]
Dies funktioniert einwandfrei, und wenn der API -Endpunkt in der Anforderung eine Organisation über eine organisation_id -Staste erwartet[code]@router.post('', response_model=Bundle)
async def create_bundle([...]
organization: Organization = Depends(get_organization_from_body),
) -> Model:
[/code]
... Und wenn der Benutzer keinen Zugriff auf die Organisation hat, wird eine 403 -Ausnahme erhöht. Mein erster Versuch war also eine JSON -Anfrage als: < /p>
[code]{
'name': generated_name,
'organization_id': created_organization['id_key']
}
< /code>
und Hinzufügen meines eingehenden pydantischen Modells: < /p>
@router.post('', response_model=Bundle)
async def create_bundle(bundle: BundleCreate,
organization: Organization = Depends(get_organization_from_body),
[...]
) -> BundleModel:
[...]
return bundle
< /code>
Das Ergebnis ist dasselbe, ob das pydantische Modell /Schema organisation_id < /code> als Feld enthält oder nicht: < /p>
class BundleBase(BaseModel):
name: str
class Config:
orm_mode = True
class BundleCreate(BundleBase):
organization_id: str
client_id: Optional[str]
[/code]
.. aber wenn ich meine Get_organization_from_body Abhängigkeit einführe, sieht Fastapi, dass ich eine andere Abhängigkeit habe, die sich auf ein Körperfeld bezieht, und die Beschreibung des Bündels. organisation_id ist Teil des Bundle Beschreibung, es sollte dort leben. Wenn möglich).[code]{'detail': [{'loc': ['body', 'bundle'], 'msg': 'field required', 'type': 'value_error.missing'}]}
[/code]
und zu Recht, wenn ich den Namen in einem Bundle Taste stattdessen bewegte:
[code]{
'bundle': {
'name': generated_name,
},
'organization_id': created_organization['id_key']
}
[/code]
...My Test Pässe und die Anfrage ist erfolgreich. mein Ausgangsformat näher.