question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFE: global or class level support for fields prefix

See original GitHub issue

In some code guidlines, fields are required to have some prefix(e.g. the “m” in AOSP and the underscores in jackson souce codes and we developers are required to follow these ), so our data class will look like these

public class User {
  private Long mId;
  public void getId() {
    return mId;
  }
  public void setId(Long id) {
    mId = id;
  }

}

At this case, the mId field must be annotated by the JsonProperty("id"), if not , jackson will recognize mId and id as two properties. That’s ok if we have few fields or few classes. But it really pains when we have to mark all fields in all class those have the prefix. After searching documents, I found there is a workaround – insert a AnnotationIntrospector by registering a custom module(some hardcode here)

class PrefixIntrospector extends JacksonAnnotationIntrospector {
    private static final Pattern M = Pattern.compile("\\bm([A-Z]).*");

    private static String doTrans(Annotated a) {
        if (!(a instanceof AnnotatedField)) {
            return null;
        }
        AnnotatedField f = (AnnotatedField) a;
        String name = f.getName();
        Matcher matcher = M.matcher(name);
        if (matcher.find()) {
            String start = matcher.group(1).toLowerCase();
            if (name.length() > 2) {
                return start + name.substring(2);
            }
            return start;
        }
        return null;
    }

    @Override
    public String findImplicitPropertyName(final AnnotatedMember m) {
        String parent = super.findImplicitPropertyName(m);
        if (parent != null) {
            return parent;
        }
        return doTrans(m);
    }

    @Override
    public PropertyName findNameForDeserialization(final Annotated a) {
        PropertyName parent = super.findNameForDeserialization(a);
        if (parent != null) {
            return parent;
        }
        String newName = doTrans(a);
        return newName == null ? null : PropertyName.construct(newName);
    }

    @Override
    public PropertyName findNameForSerialization(final Annotated a) {
        PropertyName parent = super.findNameForSerialization(a);
        if (parent != null) {
            return parent;
        }
        String newName = doTrans(a);
        return newName == null ? null : PropertyName.construct(newName);
    }
}

That works for me, but let’s go further, could there be a global config or class level annotation for this case? Maybe an annotation like these:

// a global config
public ObjectMapper newMapper() {
  ObjectMapper mapper = new ObjectMapper();
  mapper.configure(FIELDS_PREFIX, "m");
  return mapper;
}

// or class level annotation
@FiledsStyle(prefix="m")
public class User {
  private Long mId;
  private String mName;
  // omits getters and setters
}

If there is a feature already here and I didn’t find, I am sorry for wasting your time. At last, thanks for reading

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
avandermeydencommented, Jun 5, 2021

If you want the PROPAGATE_TRANSIENT_MARKER to work, the workaround presented in this solution is incorrect. You really only need to override the findImplicitPropertyName as if you return anything but null from the other two the code thinks you have an actual annotation, and ignores the transient keyword.

0reactions
cowtowncodercommented, Feb 20, 2021

No plans to tackle this, closing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rational ClearQuest behavior changes for versions 7.1.1.x ...
Cause. A defect correction or request for enhancement (RFE) causes a behavior change in any product. In Rational ClearQuest, such changes ...
Read more >
Classification Complexity - an overview | ScienceDirect Topics
Classification complexity: O(ndl) because each rule and field may need to be considered and each prefix match may require l steps.
Read more >
RHSA-2022:5069 - Security Advisory - Red Hat Customer Portal
prometheus/client_golang: Denial of service using InstrumentHandlerCounter (CVE-2022-21698); golang: crash in a golang.org/x/crypto/ssh server ( ...
Read more >
TSO Tutorial - Jay Moseley
With the prefix set to your User ID, it ensures that datasets you create during your TSO session will be cataloged in the...
Read more >
API Reference — scikit-learn 1.2.0 documentation
This is the class and function reference of scikit-learn. Please refer to the full user guide for further details, as the class and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found