Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: handle values with multiple types (including null) [SCH-1643] #57

Merged
merged 13 commits into from
Mar 6, 2019

Conversation

colinking
Copy link
Contributor

@colinking colinking commented Mar 1, 2019

This PR extends our test suite to cover JSON Schemas with properties that specify union types (type: ["string", "number"]), including union types with a null type (type: ["string", "null"]).

Specifically, this resolves an issue with the Android client not generating enums (and instead crashing during typewriter gen-android).


While testing the example Android app against Turo's inferred spec, I also noticed that there were collisions between property key names, because similar properties (like optional any and optional_any) would map to the same key identifier (OPTIONAL_ANY_KEY). This was fixed by inlining these values.


I also identified an issue with event name collisions, specifically if you have two event names that differ by delimiter characters (f.e. check_in_event and checkin_event). They both map to the same filename (causing one of the files to be overwritten). In this example case, CheckInEvent.java and CheckinEvent.java (note: file systems are case-insensitive). Java will end up complaining because the class name (either CheckInEvent or CheckinEvent) differs from the filename.

This can be solved by an upstream PR to QuickType (see our conversation in Slack), but I'm not going to block this PR on that. The fix would be to make the Namer case-insensitive so that it uses a suffix in both the class and file names to differentiate between these events.

The simplest solution for affected users is to remove all but one of the colliding events. Usually these events appear due to inferring on bad data, so it usually won't be a problem.

I've added a warning that notifies users when their Tracking Plan contains events that collide in Android.


I also made a few refactoring changes:

  • The logic to generate QuickType's inputData is duplicated across all QuickType-based clients, so I moved that out to utils/rules.ts
  • Removed PropertiesSerializable because it wasn't necessary

@colinking colinking changed the title fix: handle values with multiple types (including null) fix: handle values with multiple types (including null) [SCH-1643] Mar 2, 2019
@@ -217,10 +223,18 @@ + (SEGExampleEvent *)initWithBlock:(SEGExampleEventBuilderBlock)block
@throw [NSException exceptionWithName:@"Missing Required Property" reason:@"SEGExampleEvent is missing a required property: requiredInt" userInfo:NULL];
}

if (builder.requiredNullableString == NULL) {
@throw [NSException exceptionWithName:@"Missing Required Property" reason:@"SEGExampleEvent is missing a required property: requiredNullableString" userInfo:NULL];
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: nullable types are now supported (as in, the client will compile), but this runtime validation is (clearly) not the desired behavior. However, this will change when we add in a full JSON Schema library to replace these required property checks, so I'm not going to bother with changing this behavior.

@colinking colinking marked this pull request as ready for review March 5, 2019 04:44
Copy link

@f2prateek f2prateek left a comment

Choose a reason for hiding this comment

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

generated code looks reasonable to me!

@colinking colinking merged commit 6cce0f7 into master Mar 6, 2019
@colinking colinking deleted the colin/fix-multi-types branch March 6, 2019 01:24
colinking added a commit that referenced this pull request Mar 18, 2020
Our Android example application stores Typewriter generated files at `com.segment.analytics`, which concealed a bug in #57 where `com.segment.analytics.Properties` (from `analytics-android`) was not being imported.

This PR:
- Moves the generated Typewriter client in the Android example app to `com.segment.generated`.
- Adds `com.segment.analytics.Properties` as an import in generated files.
- Switches `List<Object>` for `List<Properties`, since `Objects` would otherwise just be `.toString`-ed when the event is fired
- Fixes serialization for `List<T>` by casting to `List<Properties>`
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants