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

Serialize Courier default values #284

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -234,22 +234,32 @@ object CourierFormats extends StrictLogging {
recordSchema.getProperties.get(passthroughAnnotation) ==
java.lang.Boolean.TRUE.asInstanceOf[AnyRef]

val newSchemaPath = schemaPath.push(recordSchema)

JsObject(dataMap.entrySet.asScala.flatMap { field =>
val fieldName = field.getKey
val fieldData = field.getValue
val fieldSchema = recordSchema.getField(fieldName)
if (fieldSchema != null) {
Some(fieldName -> dataToJsValue(newSchemaPath, fieldData, fieldSchema.getType))
} else {
if (passthrough) {
Some(fieldName -> schemalessDataToJsValue(fieldData))
val fieldMapping = if (passthrough) {
// The record is annotated with the @passthroughExempt annotation, so we propagate all
// fields in the underlying DataMap when serializing to JSON.
dataMap.entrySet.asScala.map { field =>
field.getKey -> schemalessDataToJsValue(field.getValue)
}.toSeq
} else {
// The record is not annotated with the @passthroughExempt annotation so we rely on the
// schema to get the list of possible fields and propagate their associated values in the
// underlying DataMap, falling back to the field's default value when possible.
val newSchemaPath = schemaPath.push(recordSchema)

recordSchema.getFields.asScala.flatMap { field =>
val fieldName = field.getName
val fieldData = dataMap.get(fieldName)
if (fieldData != null) {
Some(fieldName -> dataToJsValue(newSchemaPath, fieldData, field.getType))
} else if (field.getDefault != null) {
Some(fieldName -> dataToJsValue(newSchemaPath, field.getDefault, field.getType))
} else {
None
}
}
}.toSeq)
}

JsObject(fieldMapping)
}

def unionToJsObject(dataMap: DataMap, unionSchema: UnionDataSchema): JsObject = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace org.coursera.naptime.courier

@passthroughExempt
record TestPassthroughExemptRecord {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace org.coursera.naptime.courier

record TestRecordWithDefaultValue {
field: int = 100
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,39 @@ class CourierFormatsTest extends AssertionsForJUnit {
assert(parsed === roundTripped)
}

@Test
def testRecordTemplateFormats_PassthroughExemptRecord_PassesThroughAllFields(): Unit = {
implicit val converter = CourierFormats.recordTemplateFormats[TestPassthroughExemptRecord]

val json = """ { "string": "value", "int": 1 } """
val parsed = Json.parse(json)
val record = Json.fromJson[TestPassthroughExemptRecord](parsed).get
val roundTripped = Json.toJson(record)
assert(parsed === roundTripped)
}

@Test
def testRecordTemplateFormat_RecordWithDefaultValueSet_SerializesUsingSetValue(): Unit = {
implicit val converter = CourierFormats.recordTemplateFormats[TestRecordWithDefaultValue]

val fieldValue = 123
val record = TestRecordWithDefaultValue(fieldValue)
val serialized = Json.toJson(record)
val expected = Json.parse(s""" { "field": $fieldValue } """)
assert(expected === serialized)
}

@Test
def testRecordTemplateFormat_RecordWithDefaultValueUnset_SerializesUsingDefaultValue(): Unit = {
implicit val converter = CourierFormats.recordTemplateFormats[TestRecordWithDefaultValue]

val defaultFieldValue = 100
val record = TestRecordWithDefaultValue()
val serialized = Json.toJson(record)
val expected = Json.parse(s""" { "field": $defaultFieldValue } """)
assert(expected === serialized)
}

@Test
def testFlatTypedDefinition(): Unit = {
implicit val converter = CourierFormats.recordTemplateFormats[MockWithFlatTypedDefinition]
Expand Down
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version in ThisBuild := "0.11.5"
version in ThisBuild := "0.11.6"