api-platform / api-platform/core

Standalone business model not belonging to collection

Abierto
#8,042 0 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
PHP
Estrellas
2.6k
Forks
980
Merge medio
2 d 4 h
PR fusionados (30 d)
49

Descripción

**Description**
It would be convenient to declare a Resource as a simple Item, without attaching it to a collection, and thus without an identifier property to identify it in a non-sense collection in this case.

A business model is not synonym to `a set of collections of items`.
Of course most elements of my business model are collections, but rarely 100%.
Sometimes their is resources that are unique or let's say standalone (don't know if this is the right word to describe it).

**Examples**

Here are 2 examples :

1. If I make an API for my blog. I want to publish a global abstract, with some general informations about the blog itself. My application is a blog, not a collection of blogs, so it has an abstract, not a collection of abstracts :

```
#[ApiResource(
description: 'The blog overview',
inCollection: false, // default to true
itemOperations: [
// GET api.myblog.com/overview
// ==> { 'author': 'cdr', 'created_at': '2010-01-01', 'last_article': 'api.myblog.com/articles/standalone-resource-request' }
'get' => ['path' => '/overview'],

// PATCH api.myblog.com/overview ; so that admins can modify the 'lastArticle' property
'patch' => ['path' => '/overview']
],
routePrefix: '/'
)]
class BlogAbstract
{
// #[ApiProperty(identifier: true)] -- No identifier, this is 'THE' abstract of 'THE' blog
// public string $id = 'TheOnlyOne';

private string $author = '@me';
private \DateTime $createdAt; //= new \DateTime('2010-01-01');
private string $lastArticle = 'I would love to be able to define Item resource that do not stand within a collection';
}
```

2. If I make an e-commerce application, consumers have a prepaid account, not a collection of prepaid accounts :

```
#[ApiResource(
description: 'Your prepaid account',
inCollection: false, // default to true
itemOperations: [
// GET api.my-ecommerce.com/users/{userId}/prepaidAccount
// ==> { 'amount': '1300 CHF' }
'get' => ['path' => '/users/{userId}/prepaidAccount'],
],
routePrefix: '/'
)]
/**
* Note : I really want userId to be before prepaidAccount,
even if I could use the 'userId' as a unique identifier of the prepaidAccount,
because I like to keep consistence with the relations semantic, and with my other uri like this :
GET api.my-ecommerce.com/users/{userId}/orders
GET api.my-ecommerce.com/users/{userId}/orders/{orderId}
GET api.my-ecommerce.com/users/{userId}/prepaidAccount
*/
class PrepaidAccount
{
// #[ApiProperty(identifier: false)] -- No identifier, this is 'THE' prepaidAccount of the user
// public string $id = 'TheOnlyOne';
private float $amount = 1300;
private string $currency = 'CHF';
}
```

3. NB: this concept exists even in the more basic Swagger-OA3 official get started example, so I think it's really a standard and acceptable behaviour : https://petstore.swagger.io/?displayOperationId=false#/store/getInventory

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.