Skip to content

Commit

Permalink
Fix invalid call to Builder-setter from Java TypeAdapter (#211)
Browse files Browse the repository at this point in the history
The setter method name was not correctly generated in the TypeAdapter.
Added a field to board.json example to surface the issue, and ensure tests catch it.
  • Loading branch information
RicoYao authored and rahul-malik committed May 22, 2019
1 parent 427edd4 commit 8d23c9b
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 6 deletions.
44 changes: 43 additions & 1 deletion Examples/Cocoa/Sources/Objective_C/Board.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
unsigned int BoardDirtyPropertyCounts:1;
unsigned int BoardDirtyPropertyCreatedAt:1;
unsigned int BoardDirtyPropertyCreator:1;
unsigned int BoardDirtyPropertyCreatorURL:1;
unsigned int BoardDirtyPropertyDescriptionText:1;
unsigned int BoardDirtyPropertyImage:1;
unsigned int BoardDirtyPropertyName:1;
Expand Down Expand Up @@ -107,6 +108,15 @@ - (instancetype)initWithModelDictionary:(NS_VALID_UNTIL_END_OF_SCOPE NSDictionar
self->_boardDirtyProperties.BoardDirtyPropertyCreator = 1;
}
}
{
__unsafe_unretained id value = modelDictionary[@"creator_url"]; // Collection will retain.
if (value != nil) {
if (value != (id)kCFNull) {
self->_creatorURL = [NSURL URLWithString:value];
}
self->_boardDirtyProperties.BoardDirtyPropertyCreatorURL = 1;
}
}
{
__unsafe_unretained id value = modelDictionary[@"description"]; // Collection will retain.
if (value != nil) {
Expand Down Expand Up @@ -163,6 +173,7 @@ - (instancetype)initWithBuilder:(BoardBuilder *)builder initType:(PlankModelInit
_counts = builder.counts;
_createdAt = builder.createdAt;
_creator = builder.creator;
_creatorURL = builder.creatorURL;
_descriptionText = builder.descriptionText;
_image = builder.image;
_name = builder.name;
Expand All @@ -176,7 +187,7 @@ - (instancetype)initWithBuilder:(BoardBuilder *)builder initType:(PlankModelInit
- (NSString *)debugDescription
{
NSArray<NSString *> *parentDebugDescription = [[super debugDescription] componentsSeparatedByString:@"\n"];
NSMutableArray *descriptionFields = [NSMutableArray arrayWithCapacity:8];
NSMutableArray *descriptionFields = [NSMutableArray arrayWithCapacity:9];
[descriptionFields addObject:parentDebugDescription];
struct BoardDirtyProperties props = _boardDirtyProperties;
if (props.BoardDirtyPropertyContributors) {
Expand All @@ -191,6 +202,9 @@ - (NSString *)debugDescription
if (props.BoardDirtyPropertyCreator) {
[descriptionFields addObject:[@"_creator = " stringByAppendingFormat:@"%@", _creator]];
}
if (props.BoardDirtyPropertyCreatorURL) {
[descriptionFields addObject:[@"_creatorURL = " stringByAppendingFormat:@"%@", _creatorURL]];
}
if (props.BoardDirtyPropertyDescriptionText) {
[descriptionFields addObject:[@"_descriptionText = " stringByAppendingFormat:@"%@", _descriptionText]];
}
Expand Down Expand Up @@ -234,6 +248,7 @@ - (BOOL)isEqualToBoard:(Board *)anObject
(_counts == anObject.counts || [_counts isEqualToDictionary:anObject.counts]) &&
(_createdAt == anObject.createdAt || [_createdAt isEqualToDate:anObject.createdAt]) &&
(_creator == anObject.creator || [_creator isEqualToDictionary:anObject.creator]) &&
(_creatorURL == anObject.creatorURL || [_creatorURL isEqual:anObject.creatorURL]) &&
(_descriptionText == anObject.descriptionText || [_descriptionText isEqualToString:anObject.descriptionText]) &&
(_image == anObject.image || [_image isEqual:anObject.image]) &&
(_name == anObject.name || [_name isEqualToString:anObject.name]) &&
Expand All @@ -248,6 +263,7 @@ - (NSUInteger)hash
[_counts hash],
[_createdAt hash],
[_creator hash],
[_creatorURL hash],
[_descriptionText hash],
[_image hash],
[_name hash],
Expand Down Expand Up @@ -307,6 +323,13 @@ - (NSDictionary *)dictionaryObjectRepresentation
[dict setObject:[NSNull null] forKey:@"creator"];
}
}
if (_boardDirtyProperties.BoardDirtyPropertyCreatorURL) {
if (_creatorURL != nil) {
[dict setObject:[_creatorURL absoluteString] forKey:@"creator_url"];
} else {
[dict setObject:[NSNull null] forKey:@"creator_url"];
}
}
if (_boardDirtyProperties.BoardDirtyPropertyDescriptionText) {
if (_descriptionText != nil) {
[dict setObject:_descriptionText forKey:@"description"];
Expand Down Expand Up @@ -353,6 +376,10 @@ - (BOOL)isCreatorSet
{
return _boardDirtyProperties.BoardDirtyPropertyCreator == 1;
}
- (BOOL)isCreatorUrlSet
{
return _boardDirtyProperties.BoardDirtyPropertyCreatorURL == 1;
}
- (BOOL)isDescriptionTextSet
{
return _boardDirtyProperties.BoardDirtyPropertyDescriptionText == 1;
Expand Down Expand Up @@ -386,6 +413,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder
_counts = [aDecoder decodeObjectOfClasses:[NSSet setWithArray:@[[NSDictionary class], [NSNumber class]]] forKey:@"counts"];
_createdAt = [aDecoder decodeObjectOfClass:[NSDate class] forKey:@"created_at"];
_creator = [aDecoder decodeObjectOfClasses:[NSSet setWithArray:@[[NSDictionary class], [NSString class]]] forKey:@"creator"];
_creatorURL = [aDecoder decodeObjectOfClass:[NSURL class] forKey:@"creator_url"];
_descriptionText = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"description"];
_image = [aDecoder decodeObjectOfClass:[Image class] forKey:@"image"];
_name = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"name"];
Expand All @@ -394,6 +422,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder
_boardDirtyProperties.BoardDirtyPropertyCounts = [aDecoder decodeIntForKey:@"counts_dirty_property"] & 0x1;
_boardDirtyProperties.BoardDirtyPropertyCreatedAt = [aDecoder decodeIntForKey:@"created_at_dirty_property"] & 0x1;
_boardDirtyProperties.BoardDirtyPropertyCreator = [aDecoder decodeIntForKey:@"creator_dirty_property"] & 0x1;
_boardDirtyProperties.BoardDirtyPropertyCreatorURL = [aDecoder decodeIntForKey:@"creator_url_dirty_property"] & 0x1;
_boardDirtyProperties.BoardDirtyPropertyDescriptionText = [aDecoder decodeIntForKey:@"description_dirty_property"] & 0x1;
_boardDirtyProperties.BoardDirtyPropertyImage = [aDecoder decodeIntForKey:@"image_dirty_property"] & 0x1;
_boardDirtyProperties.BoardDirtyPropertyName = [aDecoder decodeIntForKey:@"name_dirty_property"] & 0x1;
Expand All @@ -410,6 +439,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder
[aCoder encodeObject:self.counts forKey:@"counts"];
[aCoder encodeObject:self.createdAt forKey:@"created_at"];
[aCoder encodeObject:self.creator forKey:@"creator"];
[aCoder encodeObject:self.creatorURL forKey:@"creator_url"];
[aCoder encodeObject:self.descriptionText forKey:@"description"];
[aCoder encodeObject:self.image forKey:@"image"];
[aCoder encodeObject:self.name forKey:@"name"];
Expand All @@ -418,6 +448,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder
[aCoder encodeInt:_boardDirtyProperties.BoardDirtyPropertyCounts forKey:@"counts_dirty_property"];
[aCoder encodeInt:_boardDirtyProperties.BoardDirtyPropertyCreatedAt forKey:@"created_at_dirty_property"];
[aCoder encodeInt:_boardDirtyProperties.BoardDirtyPropertyCreator forKey:@"creator_dirty_property"];
[aCoder encodeInt:_boardDirtyProperties.BoardDirtyPropertyCreatorURL forKey:@"creator_url_dirty_property"];
[aCoder encodeInt:_boardDirtyProperties.BoardDirtyPropertyDescriptionText forKey:@"description_dirty_property"];
[aCoder encodeInt:_boardDirtyProperties.BoardDirtyPropertyImage forKey:@"image_dirty_property"];
[aCoder encodeInt:_boardDirtyProperties.BoardDirtyPropertyName forKey:@"name_dirty_property"];
Expand All @@ -443,6 +474,9 @@ - (instancetype)initWithModel:(Board *)modelObject
if (boardDirtyProperties.BoardDirtyPropertyCreator) {
_creator = modelObject.creator;
}
if (boardDirtyProperties.BoardDirtyPropertyCreatorURL) {
_creatorURL = modelObject.creatorURL;
}
if (boardDirtyProperties.BoardDirtyPropertyDescriptionText) {
_descriptionText = modelObject.descriptionText;
}
Expand Down Expand Up @@ -479,6 +513,9 @@ - (void)mergeWithModel:(Board *)modelObject
if (modelObject.boardDirtyProperties.BoardDirtyPropertyCreator) {
builder.creator = modelObject.creator;
}
if (modelObject.boardDirtyProperties.BoardDirtyPropertyCreatorURL) {
builder.creatorURL = modelObject.creatorURL;
}
if (modelObject.boardDirtyProperties.BoardDirtyPropertyDescriptionText) {
builder.descriptionText = modelObject.descriptionText;
}
Expand Down Expand Up @@ -517,6 +554,11 @@ - (void)setCreator:(NSDictionary<NSString *, NSString *> *)creator
_creator = creator;
_boardDirtyProperties.BoardDirtyPropertyCreator = 1;
}
- (void)setCreatorURL:(NSURL *)creatorURL
{
_creatorURL = [creatorURL copy];
_boardDirtyProperties.BoardDirtyPropertyCreatorURL = 1;
}
- (void)setDescriptionText:(NSString *)descriptionText
{
_descriptionText = [descriptionText copy];
Expand Down
3 changes: 3 additions & 0 deletions Examples/Cocoa/Sources/Objective_C/include/Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, NSNumber /* Integer */ *> * counts;
@property (nullable, nonatomic, copy, readonly) NSDate * createdAt;
@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, NSString *> * creator;
@property (nullable, nonatomic, copy, readonly) NSURL * creatorURL;
@property (nullable, nonatomic, copy, readonly) NSString * descriptionText;
@property (nonnull, nonatomic, strong, readonly) Image * image;
@property (nullable, nonatomic, copy, readonly) NSString * name;
Expand All @@ -34,6 +35,7 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)isCountsSet;
- (BOOL)isCreatedAtSet;
- (BOOL)isCreatorSet;
- (BOOL)isCreatorUrlSet;
- (BOOL)isDescriptionTextSet;
- (BOOL)isImageSet;
- (BOOL)isNameSet;
Expand All @@ -45,6 +47,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nullable, nonatomic, strong, readwrite) NSDictionary<NSString *, NSNumber /* Integer */ *> * counts;
@property (nullable, nonatomic, copy, readwrite) NSDate * createdAt;
@property (nullable, nonatomic, strong, readwrite) NSDictionary<NSString *, NSString *> * creator;
@property (nullable, nonatomic, copy, readwrite) NSURL * creatorURL;
@property (nullable, nonatomic, copy, readwrite) NSString * descriptionText;
@property (nonnull, nonatomic, strong, readwrite) Image * image;
@property (nullable, nonatomic, copy, readwrite) NSString * name;
Expand Down
1 change: 1 addition & 0 deletions Examples/JS/flow/BoardType.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type BoardType = $Shape<{|
+counts: ?{ +[string]: number } /* Integer */,
+created_at: ?PlankDate,
+creator: ?{ +[string]: string },
+creator_url: ?PlankURI,
+description: ?string,
+image: ImageType,
+name: ?string,
Expand Down
41 changes: 37 additions & 4 deletions Examples/Java/Sources/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Board {
@SerializedName("counts") private @Nullable Map<String, Integer> counts;
@SerializedName("created_at") private @Nullable Date createdAt;
@SerializedName("creator") private @Nullable Map<String, String> creator;
@SerializedName("creator_url") private @Nullable String creatorURL;
@SerializedName("description") private @Nullable String description;
@SerializedName("image") private @NonNull Image image;
@SerializedName("name") private @Nullable String name;
Expand All @@ -47,10 +48,11 @@ public class Board {
static final private int COUNTS_SET = 1 << 2;
static final private int CREATED_AT_SET = 1 << 3;
static final private int CREATOR_SET = 1 << 4;
static final private int DESCRIPTION_SET = 1 << 5;
static final private int IMAGE_SET = 1 << 6;
static final private int NAME_SET = 1 << 7;
static final private int URL_SET = 1 << 8;
static final private int CREATOR_URL_SET = 1 << 5;
static final private int DESCRIPTION_SET = 1 << 6;
static final private int IMAGE_SET = 1 << 7;
static final private int NAME_SET = 1 << 8;
static final private int URL_SET = 1 << 9;

private int _bits = 0;

Expand All @@ -60,6 +62,7 @@ private Board(
@Nullable Map<String, Integer> counts,
@Nullable Date createdAt,
@Nullable Map<String, String> creator,
@Nullable String creatorURL,
@Nullable String description,
@NonNull Image image,
@Nullable String name,
Expand All @@ -71,6 +74,7 @@ private Board(
this.counts = counts;
this.createdAt = createdAt;
this.creator = creator;
this.creatorURL = creatorURL;
this.description = description;
this.image = image;
this.name = name;
Expand Down Expand Up @@ -106,6 +110,7 @@ public boolean equals(Object o) {
Objects.equals(this.counts, that.counts) &&
Objects.equals(this.createdAt, that.createdAt) &&
Objects.equals(this.creator, that.creator) &&
Objects.equals(this.creatorURL, that.creatorURL) &&
Objects.equals(this.description, that.description) &&
Objects.equals(this.image, that.image) &&
Objects.equals(this.name, that.name) &&
Expand All @@ -119,6 +124,7 @@ public int hashCode() {
counts,
createdAt,
creator,
creatorURL,
description,
image,
name,
Expand All @@ -145,6 +151,10 @@ public int hashCode() {
return this.creator;
}

public @Nullable String getCreatorURL() {
return this.creatorURL;
}

public @Nullable String getDescription() {
return this.description;
}
Expand Down Expand Up @@ -181,6 +191,10 @@ public boolean getCreatorIsSet() {
return (this._bits & CREATOR_SET) == CREATOR_SET;
}

public boolean getCreatorURLIsSet() {
return (this._bits & CREATOR_URL_SET) == CREATOR_URL_SET;
}

public boolean getDescriptionIsSet() {
return (this._bits & DESCRIPTION_SET) == DESCRIPTION_SET;
}
Expand All @@ -204,6 +218,7 @@ public static class Builder {
@SerializedName("counts") private @Nullable Map<String, Integer> counts;
@SerializedName("created_at") private @Nullable Date createdAt;
@SerializedName("creator") private @Nullable Map<String, String> creator;
@SerializedName("creator_url") private @Nullable String creatorURL;
@SerializedName("description") private @Nullable String description;
@SerializedName("image") private @NonNull Image image;
@SerializedName("name") private @Nullable String name;
Expand All @@ -220,6 +235,7 @@ private Builder(@NonNull Board model) {
this.counts = model.counts;
this.createdAt = model.createdAt;
this.creator = model.creator;
this.creatorURL = model.creatorURL;
this.description = model.description;
this.image = model.image;
this.name = model.name;
Expand Down Expand Up @@ -257,6 +273,12 @@ public Builder setCreator(@Nullable Map<String, String> value) {
return this;
}

public Builder setCreatorURL(@Nullable String value) {
this.creatorURL = value;
this._bits |= CREATOR_URL_SET;
return this;
}

public Builder setDescription(@Nullable String value) {
this.description = value;
this._bits |= DESCRIPTION_SET;
Expand Down Expand Up @@ -301,6 +323,10 @@ public Builder setUrl(@Nullable String value) {
return this.creator;
}

public @Nullable String getCreatorURL() {
return this.creatorURL;
}

public @Nullable String getDescription() {
return this.description;
}
Expand All @@ -324,6 +350,7 @@ public Board build() {
this.counts,
this.createdAt,
this.creator,
this.creatorURL,
this.description,
this.image,
this.name,
Expand All @@ -348,6 +375,9 @@ public void mergeFrom(Board model) {
if (model.getCreatorIsSet()) {
this.creator = model.creator;
}
if (model.getCreatorURLIsSet()) {
this.creatorURL = model.creatorURL;
}
if (model.getDescriptionIsSet()) {
this.description = model.description;
}
Expand Down Expand Up @@ -426,6 +456,9 @@ public Board read(JsonReader reader) throws IOException {
case ("creator"):
builder.setCreator(map_String__String_TypeAdapter.read(reader));
break;
case ("creator_url"):
builder.setCreatorURL(stringTypeAdapter.read(reader));
break;
case ("description"):
builder.setDescription(stringTypeAdapter.read(reader));
break;
Expand Down
4 changes: 4 additions & 0 deletions Examples/PDK/board.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"type": "string",
"format": "uri"
},
"creator_url" : {
"type": "string",
"format": "uri"
},
"description" : { "type": "string" },
"creator": {
"type": "object",
Expand Down
2 changes: 1 addition & 1 deletion Sources/Core/JavaModelRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public struct JavaModelRenderer: JavaFileRenderer {

func renderBuilderSetters(modifiers: JavaModifier = [.public]) -> [JavaIR.Method] {
let setters = transitiveProperties.map { param, schemaObj in
JavaIR.method(modifiers, "Builder set\(Languages.java.snakeCaseToCamelCase(param))(\(self.typeFromSchema(param, schemaObj)) value)") { [
JavaIR.method(modifiers, "Builder set\(Languages.java.snakeCaseToCapitalizedPropertyName(param))(\(self.typeFromSchema(param, schemaObj)) value)") { [
"this." + Languages.java.snakeCaseToPropertyName(param) + " = value;",
"this._bits |= " + param.uppercased() + "_SET;",
"return this;",
Expand Down

0 comments on commit 8d23c9b

Please sign in to comment.