Skip to content

Commit 60567bd

Browse files
hinrikackintosh
authored andcommitted
[PHP] Correctly format JSON in headers (#4024)
* [PHP] Correctly format JSON in headers `ObjectSerializer::toHeaderValue()` in the generated PHP code calls `toString()` on the values, which formats JSON with the `JSON_PRETTY_PRINT` option. This will result in a multi-line header which cannot be parsed since linebreaks aren't allowed by RFC 7230. In my case I have a header schema called `UpdateUser` which I had hoped would be serialized as `{"type":"staff","id":123}`. Every single `__toString()` in the generator does the same thing, so I figured it's safe to change `toHeaderValue()` to convert to JSON directly, without `JSON_PRETTY_PRINT`. This fix works for me. * More sensible approach to providing a header value * Just strip the newlines * Go back to previous solution
1 parent cc1bfe5 commit 60567bd

92 files changed

Lines changed: 902 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ class ObjectSerializer
140140
*/
141141
public static function toHeaderValue($value)
142142
{
143+
if (method_exists($value, 'toHeaderValue')) {
144+
return $value->toHeaderValue();
145+
}
146+
143147
return self::toString($value);
144148
}
145149

modules/openapi-generator/src/main/resources/php/model_generic.mustache

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,14 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
410410
JSON_PRETTY_PRINT
411411
);
412412
}
413+
414+
/**
415+
* Gets a header-safe presentation of the object
416+
*
417+
* @return string
418+
*/
419+
public function toHeaderValue()
420+
{
421+
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
422+
}
413423
}

samples/client/petstore/php/OpenAPIClient-php/lib/Model/AdditionalPropertiesAnyType.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ public function __toString()
292292
JSON_PRETTY_PRINT
293293
);
294294
}
295+
296+
/**
297+
* Gets a header-safe presentation of the object
298+
*
299+
* @return string
300+
*/
301+
public function toHeaderValue()
302+
{
303+
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
304+
}
295305
}
296306

297307

samples/client/petstore/php/OpenAPIClient-php/lib/Model/AdditionalPropertiesArray.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ public function __toString()
292292
JSON_PRETTY_PRINT
293293
);
294294
}
295+
296+
/**
297+
* Gets a header-safe presentation of the object
298+
*
299+
* @return string
300+
*/
301+
public function toHeaderValue()
302+
{
303+
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
304+
}
295305
}
296306

297307

samples/client/petstore/php/OpenAPIClient-php/lib/Model/AdditionalPropertiesBoolean.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ public function __toString()
292292
JSON_PRETTY_PRINT
293293
);
294294
}
295+
296+
/**
297+
* Gets a header-safe presentation of the object
298+
*
299+
* @return string
300+
*/
301+
public function toHeaderValue()
302+
{
303+
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
304+
}
295305
}
296306

297307

samples/client/petstore/php/OpenAPIClient-php/lib/Model/AdditionalPropertiesClass.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,16 @@ public function __toString()
592592
JSON_PRETTY_PRINT
593593
);
594594
}
595+
596+
/**
597+
* Gets a header-safe presentation of the object
598+
*
599+
* @return string
600+
*/
601+
public function toHeaderValue()
602+
{
603+
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
604+
}
595605
}
596606

597607

samples/client/petstore/php/OpenAPIClient-php/lib/Model/AdditionalPropertiesInteger.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ public function __toString()
292292
JSON_PRETTY_PRINT
293293
);
294294
}
295+
296+
/**
297+
* Gets a header-safe presentation of the object
298+
*
299+
* @return string
300+
*/
301+
public function toHeaderValue()
302+
{
303+
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
304+
}
295305
}
296306

297307

samples/client/petstore/php/OpenAPIClient-php/lib/Model/AdditionalPropertiesNumber.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ public function __toString()
292292
JSON_PRETTY_PRINT
293293
);
294294
}
295+
296+
/**
297+
* Gets a header-safe presentation of the object
298+
*
299+
* @return string
300+
*/
301+
public function toHeaderValue()
302+
{
303+
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
304+
}
295305
}
296306

297307

samples/client/petstore/php/OpenAPIClient-php/lib/Model/AdditionalPropertiesObject.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ public function __toString()
292292
JSON_PRETTY_PRINT
293293
);
294294
}
295+
296+
/**
297+
* Gets a header-safe presentation of the object
298+
*
299+
* @return string
300+
*/
301+
public function toHeaderValue()
302+
{
303+
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
304+
}
295305
}
296306

297307

samples/client/petstore/php/OpenAPIClient-php/lib/Model/AdditionalPropertiesString.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ public function __toString()
292292
JSON_PRETTY_PRINT
293293
);
294294
}
295+
296+
/**
297+
* Gets a header-safe presentation of the object
298+
*
299+
* @return string
300+
*/
301+
public function toHeaderValue()
302+
{
303+
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
304+
}
295305
}
296306

297307

0 commit comments

Comments
 (0)