Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:APIJSON APIJSON Demo Privacy Field Hiding

From Leeroopedia
Knowledge Sources
Domains Security, Data_Protection, Serialization
Last Updated 2026-10-14 00:00 GMT

Overview

Design pattern that separates sensitive user data (passwords, payment codes, balance) from public profile data into distinct model classes, using serialization annotations to prevent sensitive fields from appearing in API responses.

Description

Privacy Field Hiding addresses the principle of data minimization: an API should never expose more data than the client needs. In APIJSON, this is implemented by splitting user data across two models:

  • User — Public profile (name, avatar, sex, tags, picture list, contact list). Implements Visitor<Long> for session authentication.
  • Privacy — Sensitive data (phone, email, password, payment password, balance). Access is restricted via @MethodAccess(GETS = {OWNER, ADMIN}).

Sensitive fields use the fastjson @JSONField(name="_password") annotation on getter methods like get__password(). The double-underscore prefix causes fastjson to serialize the field as _password (with a single underscore), but the @JSONField annotation ensures the field is excluded from serialization by default, hiding it from API responses.

This solves:

  • Accidental data exposure: Without separation, a single User model might leak passwords in GET responses.
  • Granular access control: Public data (User) is accessible to more roles; sensitive data (Privacy) requires OWNER or ADMIN.
  • Serialization control: The @JSONField annotation provides a declarative way to hide fields without custom serialization code.

Usage

Use this principle when:

  • Building user-facing APIs where some fields (passwords, balance) must never appear in responses.
  • You want to separate public and private data into different models with different access policies.
  • You need a declarative, annotation-based approach to field hiding rather than manual response filtering.

Theoretical Basis

The pattern relies on two mechanisms:

1. Model separation:

User (public):
  - id, sex, name, head, tag, pictureList, contactIdList
  - @MethodAccess(POST = {UNKNOWN, ADMIN}, DELETE = {ADMIN})
  - Implements Visitor<Long> (session auth)

Privacy (sensitive):
  - id, phone, email, password, payPassword, balance
  - @MethodAccess(GETS = {OWNER, ADMIN}, POST = {UNKNOWN, ADMIN}, DELETE = {ADMIN})
  - @JSONField(name="_password") on get__password()
  - @JSONField(name="_payPassword") on get__payPassword()

2. Serialization hiding:

// The getter name get__password() with @JSONField(name="_password")
// causes fastjson to serialize as "_password" — but since the JSONField
// annotation is applied to the getter, the field can be excluded
// from output by default, appearing only when explicitly requested
// with proper authorization.
@JSONField(name="_password")
public String get__password() {
    return password;
}

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment