Comments (2)
I think a way we could "fix" this is by introducing some kind of new attribute to make it a little bit easier for users to mark their property as required=true|false
. This could then be done on the property level instead of on class level.
class GetContactsDto
{
/**
* @var Contact[]
*/
#[Required]
protected array $items = [];
Using the solution provided by @DominicLuidold seems a little bit hacky for this, but for now you should be able to overwrite the setting it as required with the OA\Schema
attribute from swagger-php.
#[OA\Schema(required: ['items'])]
class GetContactsDto
{
...
from nelmioapidocbundle.
I found issues with '\Symfony\Component\Validator\Constraints\NotBlank'. When merged fix for this bug #2222 can't set the property required if it has default value.
In my opinion, the current behavior is correct and works as intended.
Consider a scenario where the DTO is used not for a GET
endpoint but for a POST
or PUT
API endpoint by your Angular application - the proposed changes in #2288 would create an inconsistency (which got caught by a failing test): When your Angular app sends data, it would be correct that $count
is not marked as required, as the data and the DTO would still be valid even if the $count
property wasn't included (since there is a fallback/default value). This means it is, in fact, an optional value.
This logic applies to your case as well, and I don't believe there is an easy solution within the Nelmio bundle. However, a simple fix for your problem could be to avoid defining the default value at the property level. Instead, you can define it within the getCount()
method, as shown below (additionally, see https://3v4l.org/YlcuX):
class GetContactDto
{
protected int $count;
public function getCount(): int
{
return $this->count ?? 0;
}
}
from nelmioapidocbundle.
Related Issues (20)
- [Feature]: Describing default error response
- [Question]: Code breaks after updating from 4.25.2 to 4.25.3 HOT 2
- [Bug]: Upgrade to 4.26.0 breaks phpunit-tests HOT 2
- [Bug]: Deprecation errors after updating to 4.26.1 HOT 4
- [Question]: Is there a way to override the documentation for a specific class/type? HOT 1
- [Bug]: Type required for oneOf defined property HOT 1
- [Question]: multiple allow_origin entries HOT 1
- [Bug]: The PropertyInfo component was not able to guess the type of Company::$edAt. You may need to add a `@var` annotation or use `@OA\Property(type="")` to make its type explicit HOT 1
- [Bug]: Attribute-defined schemas containing oneOf/anyOf/allOf are merged with property types, no overwriting of types possible
- [Bug]: Symfony 7.1 - deprecation Symfony\Component\HttpKernel\DependencyInjection\Extension
- [Bug]: CSRF token is always in a schema HOT 4
- [Question]: Assert/Choice overrides enum HOT 1
- `OpenApi\Processors\ProcessorInterface` has been deprecated
- [Question]: GitHub Sponsors HOT 5
- [Bug]: Symfony 7.1 - MapRequestPayload resolves array with new "type" parameter
- [Bug]: UuidPropertyDescriber::describe()" method will require a new "array $context" argument in the next major version of its interface HOT 1
- [Question]: Why I cant see in swagger properties from model? HOT 5
- [Bug]: Not nullable with default are required HOT 3
- [Feature]: Easy way to ignore properties from schema generation without relying on Symfony's `#[Ignore]` attribute
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nelmioapidocbundle.