diff --git a/src/oatpp/parser/json/mapping/Serializer.cpp b/src/oatpp/parser/json/mapping/Serializer.cpp index cd7c99d1..f1acd5f4 100644 --- a/src/oatpp/parser/json/mapping/Serializer.cpp +++ b/src/oatpp/parser/json/mapping/Serializer.cpp @@ -155,11 +155,12 @@ void Serializer::serializeObject(Serializer* serializer, auto dispatcher = static_cast(polymorph.valueType->polymorphicDispatcher); auto fields = dispatcher->getProperties()->getList(); auto object = static_cast(polymorph.get()); + auto config = serializer->getConfig(); for (auto const& field : fields) { auto value = field->get(object); - if(value || serializer->getConfig()->includeNullFields) { + if (value || config->includeNullFields || (field->info.required && config->alwaysIncludeRequired)) { (first) ? first = false : stream->writeSimple(",", 1); serializeString(stream, (p_char8)field->name, std::strlen(field->name)); stream->writeSimple(":", 1); diff --git a/src/oatpp/parser/json/mapping/Serializer.hpp b/src/oatpp/parser/json/mapping/Serializer.hpp index 7174d9e8..ab754624 100644 --- a/src/oatpp/parser/json/mapping/Serializer.hpp +++ b/src/oatpp/parser/json/mapping/Serializer.hpp @@ -65,9 +65,15 @@ public: /** * Include fields with value == nullptr into serialized json. + * Field will still be included when field-info `required` is set to true and &id:alwaysIncludeRequired is set to true. */ bool includeNullFields = true; + /** + * Always include required fields (set in in DTO_FIELD_INFO) even if they are `value == nullptr` + */ + bool alwaysIncludeRequired = false; + /** * If `true` - insert string `""` in json field value in case unknown field found. * Fail if `false`.