Skip to content

Commit 1295fdc

Browse files
Merge pull request #192 from MarcinOrlowski/dev
Release 9.2.1
2 parents d464dd4 + 047ec66 commit 1295fdc

File tree

5 files changed

+38
-27
lines changed

5 files changed

+38
-27
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "marcin-orlowski/laravel-api-response-builder",
33
"description": "Helps building nice, normalized and easy to consume Laravel REST API.",
44
"homepage": "https://github.com/MarcinOrlowski/laravel-api-response-builder",
5-
"version": "9.2.1",
5+
"version": "9.2.2",
66
"keywords": [
77
"laravel",
88
"json",

docs/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
99
## CHANGE LOG ##
1010

11+
* v9.2.2 (2021-03-05)
12+
* [RB-190] Fixed converting resource and resource collection (reported by @achinkumar121).
13+
1114
* v9.2.1 (2021-01-18)
1215
* [RB-186] ExceptionHandler now expects `\Throwable` instead of `\Exception`.
1316

docs/config.md

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,35 @@
6666
Meaning of parameters:
6767

6868
* `handler` (mandatory) specifies a full name of the class implementing `ConverterContract`. Object of that class will be
69-
instantiated and conversion method will be invoked with object given as argument. The `key` is a string that will be used
70-
as the JSON response as key to array representation when object of that class is passed as direct payload
71-
(i.e. `success($object);`). Note, that `key` is not used otherwise, so if you have i.e. array of objects, they will be
72-
properly converted without `key` used.
73-
* `key` (mandatory) can be a string or `NULL`. A string is useful for some converters when dealing with an object of a given class being returned directly
74-
as response payload (i.e. `success($collection)`). Otherwise `NULL` can be used to tell `ResponseBuilder` to return object directly, which may be useful.
69+
instantiated and conversion method invoked with object to convert passed as method argument.
70+
* `key` (mandatory) can be a string or `NULL`. When dealing with an object of a given class being returned directly
71+
as response payload (i.e. `success($collection)`), the `key` string will be used in returned JSON as converted data
72+
key. Passing `NULL` tells `ResponseBuilder` to return object without using the key, which is useful while dealing
73+
with i.e. collection of objects as collection will be `key`ed in the response, but objects should be returned as
74+
plain JSON array.
75+
76+
77+
```json
78+
...
79+
"data": {
80+
"given-key": {
81+
[converted object data]
82+
}
83+
}
84+
...
85+
```
86+
87+
7588
* `pri` (optional) is an integer being entry's priority (default `0`). Entries with higher values will be matched first. If you got one
7689
class extending another and you want to support both of them with separate configuration, then you **must** ensure child
7790
class has higher priority than it's parent class.
7891

79-
The above configures two classes (`Model` and `Collection`). Whenever object of that class is spotted, method specified in
80-
`method` key would be called on that object and data that method returns will be returned in JSON object.
81-
8292
All configuration entries are assigned priority `0` which can be changed using `pri` key (integer). This value is used to
8393
sort the entries to ensure that matching order is preserved. Entries with higher priority are matched first etc. This is
8494
very useful when you want to indirect configuration for two classes where additionally second extends first one.
8595
So if you have class `A` and `B` that extends `A` and you want different handling for `B` than you have set for `A`
8696
then `B` related configuration must be set with higher priority.
87-
97+
8898
> ![IMPORTANT](img/warning.png) For each object `ResponseBuilder` checks if we have configuration entry matching **exactly**
8999
> object class name. If no such mapping is found, then the whole configuration is walked again, but this time we take inheritance
90100
> into consideration and use `instanceof` to see if we have a match, therefore you need to pay attention your config specifies
@@ -140,17 +150,17 @@ $data = [
140150
* `integer`
141151
* `string`
142152

143-
For each of these types there's configuration entry in `primitives` node of `converter` config, consisting of `key` entry.
144-
The value of `key` is an arbitrary string, that will be used when given primivite will be passed as direct payload. For example,
145-
pre v9 would require
153+
For each of these types there's configuration entry in `primitives` node of `converter` config. Each entry defined `key`
154+
which is an arbitrary string, used for given primitive. The default value for all supported primitives is `value`.
155+
For example, pre v9 would require
146156

147-
RB::success(['my_key' => 12.25]);
157+
RB::success(['value' => 12.25]);
148158

149159
while with v9+ if can be simplified:
150160

151161
RB::success(12.25);
152162

153-
and both would produce the same
163+
and both would yield the same result:
154164

155165
```json
156166
{
@@ -159,15 +169,16 @@ and both would produce the same
159169
"locale": "en",
160170
"message": "OK",
161171
"data": {
162-
"my_key": 12.25
172+
"value": 12.25
163173
}
164174
}
165175
```
166176

167-
assuming string`my_key` is the value of `key` entry for primitive type `double`.
168-
169177
### debug ###
170178

179+
> ![NOTE](img/warning.png) Do not use debug mode on production as it may expose i.e. your filesystem structure
180+
> or class names or other internals that should not really be public.
181+
171182
```php
172183
'debug' => [
173184
'debug_key' => 'debug',
@@ -187,12 +198,8 @@ assuming string`my_key` is the value of `key` entry for primitive type `double`.
187198
```
188199

189200
`debug_key` - name of the JSON key trace data should be put under when in `debug` node.
190-
191-
/**
192-
* When ExceptionHandler kicks in and this is set to @true,
193-
* then returned JSON structure will contain additional debug data
194-
* with information about class name, file name and line number.
195-
*/
201+
When `ExceptionHandler` kicks with debug mode enabled, returned JSON structure would
202+
contain additional debug data with information about class name, file name and line number:
196203

197204
```json
198205
{
@@ -210,6 +217,7 @@ assuming string`my_key` is the value of `key` entry for primitive type `double`.
210217
}
211218
}
212219
```
220+
213221
### encoding_options ###
214222

215223
This option controls data JSON encoding. Since v3.1, encoding was relying on framework's defaults, however this

src/Converters/ToArrayConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public function convert(object $obj, /** @scrutinizer ignore-unused */ array $co
3535
{
3636
Validator::assertIsObject('obj', $obj);
3737

38-
return $obj->toArray();
38+
return $obj->toArray(null);
3939
}
4040
}

tests/Models/TestModelJsonResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function getVal(): ?string
5252
*
5353
* @return array
5454
*/
55-
public function toArray($request = null): array
55+
public function toArray($request): array
5656
{
5757
return [
5858
self::FIELD_NAME => $this->val,

0 commit comments

Comments
 (0)