Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHP] Correctly format JSON in headers #4024

Merged
merged 4 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ class ObjectSerializer
*/
public static function toHeaderValue($value)
{
if (method_exists($value, 'toHeaderValue')) {
return $value->toHeaderValue();
}

return self::toString($value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this instead of adding toHeaderValue() to model_generic.mustache ? 💡

// Strip newlines and spaces which comes from JSON_PRETTY_PRINT
return str_replace([PHP_EOL, ' '], '', (string)$value);

This will reduce the if condition.

   if (method_exists($value, 'toHeaderValue')) {
       return $value->toHeaderValue();
   }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to remove spaces, as they are allowed in a header value (just not at the beginning/end, but JSON wouldn't begin/end with spaces anyway). But yeah, otherwise this approach would work I suppose.

Copy link
Contributor Author

@hinrik hinrik Oct 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed this change. It is simpler, but now there are extra indents in the JSON since it was pretty-printed, which doesn't really make sense for a single line.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is why I removed spaces in the sample. 💡

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I see. We should still allow spaces inside strings in the JSON. Would you prefer the previous solution then, with toHeaderValue() on the model, or keep it like this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... sorry for my oversight... I would prefer the previous solution, which makes clean output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,14 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


10 changes: 10 additions & 0 deletions samples/client/petstore/php/OpenAPIClient-php/lib/Model/Animal.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


10 changes: 10 additions & 0 deletions samples/client/petstore/php/OpenAPIClient-php/lib/Model/Cat.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


10 changes: 10 additions & 0 deletions samples/client/petstore/php/OpenAPIClient-php/lib/Model/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


10 changes: 10 additions & 0 deletions samples/client/petstore/php/OpenAPIClient-php/lib/Model/Dog.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,16 @@ public function __toString()
JSON_PRETTY_PRINT
);
}

/**
* Gets a header-safe presentation of the object
*
* @return string
*/
public function toHeaderValue()
{
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}


Loading