Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
More sensible approach to providing a header value
  • Loading branch information
hinrik committed Oct 2, 2019
commit 83b0532bcc88d5e64ff2b8f8246cc2f03b8bcf58
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ class ObjectSerializer
*/
public static function toHeaderValue($value)
{
return json_encode(ObjectSerializer::sanitizeForSerialization($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));
}
}


Loading