facebookarchive / swift

An annotation-based Java library for creating Thrift serializable types and services.
Apache License 2.0
900 stars 297 forks source link

Can we make the immutable structs generated by swift-generator a bit Jackson friendly? #153

Closed l3fang closed 6 years ago

l3fang commented 10 years ago

By default, the structs generated by the swift generator does not have

@JsonProperty(value = "<field_name>")

in the constructor for each field. It seems this annotation is needed in order for Jackson to read the struct from json.

For example, I have the following class generated

package com.facebook.calligraphus.bucketing.shardmanager;

/* /

import com.facebook.swift.codec.ThriftConstructor; import com.facebook.swift.codec.ThriftField; import com.facebook.swift.codec.ThriftStruct; import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Map;

import static com.google.common.base.Objects.toStringHelper;

@ThriftStruct("com.facebook.swift.shardmanager.SmcServiceTierScopesProp") public class SmcServiceTierScopesProp { @ThriftConstructor public SmcServiceTierScopesProp( @JsonProperty(value = "scopePrefix") <-- need this line @ThriftField(value=1, name="scopePrefix") final Map<String, String> scopePrefix ) { this.scopePrefix = scopePrefix; }

private final Map<String, String> scopePrefix;

@ThriftField(value=1, name="scopePrefix") public Map<String, String> getScopePrefix() { return scopePrefix; }

@Override public String toString() { return toStringHelper(this) .add("scopePrefix", scopePrefix) .toString(); } }

and I have the following json

String prefixValue = "  {\n" +
  "    \"scopePrefix\" : \n" +
  "      {\n" +
  "        \"calligraphustest-frc1-12\" : \"cluster:frc1.12:\",\n" +
  "        \"calligraphustest-prn1-12\" : \"cluster:prn1.12:\"\n" +
  "      }\n" +
  "    \n" +
  "  }";

  prop = MAPPER.readValue(
    prefixValue,
    SmcServiceTierScopesProp.class);
  return prop.getScopePrefix();

This code throws in the readValue() line if the annotation is not there, because Jackson does not know how to construct the object without the annotation.

Any better suggestions here?

facebook-github-bot commented 9 years ago

Thank you for reaching out to us and we'd like to first apoligize for not getting back to you sooner. We're closing out older issues since updates to the project may address the issue you opened or they may no longer be relevant. If you're still experiencing the problem, please reopen the issue - thanks!

ghost commented 9 years ago

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.