Spec

Mock Data Specification

Mock data generation extension user manual — automatically generate realistic test data from protobuf field definitions.

Generate realistic mock data directly from protobuf field annotations for testing and prototyping.

  • Extension Package: apihug/protobuf/mock/mock.proto
  • Scope: Field-level and Operation-level
  • Scenarios: API testing, frontend development, demo environments

1. Overview

The ApiHug mock system is inspired by Mock.js and Java Faker. It provides a rich set of semantic data generators (Nature types), string/number/date rules, and locale-specific generators for Chinese text, names, and addresses.

1.1 Where Mock is Used

Field-level Mock (via hope.swagger.field):

Proto
string phone = 1 [(hope.swagger.field) = {
  description: "Phone number";
  mock: {
    nature: CN_PHONE
  };
}];

Operation-level Mock (simple response types only):

Proto
option (hope.swagger.operation) = {
  post: "/ping";
  mock: {
    nature: GUID
  };
};

1.2 Mock Message Structure

The top-level Mock message uses oneof rule to select exactly one mock strategy:

Proto
message Mock {
  oneof rule {
    Nature nature = 1;
    StringRule string_rule = 3;
    BoolRule bool_rule = 4;
    DateRule date_rule = 5;
    ImageRule image_rule = 6;
    ColorRule color_rule = 7;
    ChineseRule chinese_rule = 8;
    ChineseNameRule chinese_name_rule = 9;
    ChinaAddressRule china_address_rule = 10;
    NumberRule number_rule = 11;
    MapRule map_rule = 112;
  }
  uint32 max = 200;  // shortcut: array max items
  uint32 min = 201;  // shortcut: array min items
}

2. Nature (Semantic Category)

The Nature enum provides instant, semantic mock data with zero configuration:

2.1 General Data

Enum ValueGenerated ContentExample
ANIMALAnimal nameCat, Dog, Eagle
AVATARAvatar URLhttps://i.pravatar.cc/...
BEERBeer brandHeineken, Guinness
BOOKBook nameThe Great Gatsby
BUSINESSBusiness kindConsulting, Retail
CATCat breedPersian, Siamese
COLORColor nameRed, Blue, Green
COUNTRYCountry nameChina, Japan
CURRENCYCurrency nameUS Dollar, Euro
DOGDog breedLabrador, Poodle
FOODFood namePizza, Sushi
FUNNY_NAMEHumorous nameMike Litoris
GENDERGender (English)Male / Female
HOBBITHobbyFishing, Reading
JOBJob titleSoftware Engineer
MEDICALMedical termHypertension
MUSICMusic genre/nameJazz, Rock
NAMEName (English)John Doe
NATIONNation nameAmerican, Chinese
PROGRAMMING_LANGUAGELanguage nameJava, Python
STOCKStock nameApple, Tesla
TEAMTeam nameLakers, Warriors
WEATHERWeather descriptionCloudy, Sunny
SPACESpace termMars, Nebula
DISEASEDisease nameHeadache, Flu

2.2 Digital & Contact

Enum ValueGenerated ContentExample
EMAILEmail addressuser@example.com
IP4IPv4 address192.168.1.1
IP6IPv6 address2001:0db8:85a3:...
URLHTTP URLhttps://example.com
GUIDUUID550e8400-e29b-...
PHONEInternational phone+1-555-1234

2.3 China-specific

Enum ValueGenerated ContentExample
CN_GENDERChinese gender男 / 女
CN_ADDRESSChinese address上海 静安
CN_PHONEChinese mobile13800138000
CN_UNIVERSITYChinese university清华大学
CIDChinese ID number310101199001011234
MEDICINEMedicine name阿莫西林
HOSPITALHospital name协和医院

2.4 Other

Enum ValueGenerated ContentExample
UNIVERSITYUniversity nameMIT, Stanford
IDID numberGeneric ID
DATE_AND_TIMEDate/time string2024-01-15 10:30:00

2.5 Usage Example

Proto
string email = 1 [(hope.swagger.field) = {
  mock: { nature: EMAIL }
}];

string avatar = 2 [(hope.swagger.field) = {
  mock: { nature: AVATAR }
}];

string phone = 3 [(hope.swagger.field) = {
  mock: { nature: CN_PHONE }
}];

string uuid = 4 [(hope.swagger.field) = {
  mock: { nature: GUID }
}];

3. StringRule (String Generation)

Control string length, character pool, and candidate values.

3.1 Fields

FieldTypeDescription
lengthuint32Exact string length
minuint32Minimum length
maxuint32Maximum length
poolPool enumCharacter pool type
customized_poolstringCustom character set
candidatesrepeated stringPick from fixed list

3.2 Pool Enum

Enum ValueDescription
ORIGINALKeep original format, no transform
LOWERAll lowercase
UPPERAll uppercase
NUMBERDigits only (0-9)
SYMBOLSymbols only
CAPITALIZEFirst letter uppercase

3.3 Examples

Random lowercase string (3-20 chars):

Proto
mock: {
  string_rule: {
    min: 3;
    max: 20;
    pool: LOWER;
  }
}

Fixed candidate values:

Proto
mock: {
  string_rule: {
    candidates: ["PENDING", "APPROVED", "REJECTED"]
  }
}

Exact length with custom pool:

Proto
mock: {
  string_rule: {
    length: 8;
    customized_pool: "ABCDEF0123456789";
  }
}

4. NumberRule (Numeric Generation)

Control integer and decimal ranges.

4.1 Fields

FieldTypeDescription
minint64Integer part minimum
maxint64Integer part maximum
min_integeruint32Minimum integer digits
max_integeruint32Maximum integer digits
min_fractionuint32Minimum decimal places
max_fractionuint32Maximum decimal places

4.2 Example

Proto
mock: {
  number_rule: {
    min: 1;
    max: 100000;
    min_fraction: 0;
    max_fraction: 2;  // 0-2 decimal places
  }
}

5. BoolRule (Boolean Generation)

5.1 Fields

FieldTypeDescription
minuint32Minimum (for array context)
maxuint32Maximum (for array context)
currentboolStarting value

6. DateRule (Date/Time Generation)

6.1 Birthday Mode

Generate realistic birthday dates:

Proto
mock: {
  date_rule: {
    birth_day: {
      min_age: 18;
      max_age: 60;
    }
  }
}

6.2 Relative Time Mode

Generate dates relative to "now":

Proto
mock: {
  date_rule: {
    time_gap: 30;
    time_unit: DAYS;
    dir: PAST;
  }
}

6.3 TimeUnit Enum

Enum ValueDescription
NANOSECONDSNanoseconds
MICROSECONDSMicroseconds
MILLISECONDSMilliseconds
SECONDSSeconds
MINUTESMinutes
HOURSHours
DAYSDays

6.4 Dir Enum

Enum ValueDescription
PASTDate in the past
FUTUREDate in the future

7. ImageRule (Placeholder Images)

7.1 Fields

FieldTypeDescription
widthuint32Image width (px)
heightuint32Image height (px)
background_hex_colorstringBackground color
textstringText overlay

7.2 Example

Proto
mock: {
  image_rule: {
    width: 200;
    height: 200;
    background_hex_color: "#4A90D9";
    text: "Avatar";
  }
}

8. ColorRule (Color Values)

8.1 Type Enum

Enum ValueFormatExample
HEX#FFF or #FFFFFF#4A90D9
RGBrgb(r,g,b)rgb(121, 210, 242)
RGBArgba(r,g,b,a)rgba(121, 242, 167, 0.50)
HSLhsl(h,s,l)hsl(228, 82, 71)
NAMEColor namered, blue, yellow

8.2 Example

Proto
mock: {
  color_rule: {
    type: HEX;
  }
}

9. ChineseRule (Chinese Text)

Generate Chinese paragraphs, sentences, words, or titles.

9.1 Type Enum

Enum ValueDescription
PARAGRAPHLong paragraph
SENTENCEMedium sentence
WORDSingle word
TITLENews-style title

9.2 Fields

FieldTypeDescription
typeType enumText type
lengthuint32Exact length
minuint32Minimum length
maxuint32Maximum length
poolstringCharacter pool

9.3 Example

Proto
mock: {
  chinese_rule: {
    type: TITLE;
    min: 5;
    max: 20;
  }
}

10. ChineseNameRule (Chinese Names)

10.1 Type Enum

Enum ValueDescription
FIRSTGiven name (名)
LASTSurname (姓)
NAMEFull name

10.2 Example

Proto
mock: {
  chinese_name_rule: {
    type: NAME
  }
}

11. ChinaAddressRule (Chinese Addresses)

11.1 Type Enum

Enum ValueDescriptionExample
REGIONRegion华北, 华东
PROVINCEProvince陕西省
CITYCity淮北市
COUNTYDistrict/County东昌区
ADDRESSFull address上海市,闵行区,联航路1188号

11.2 Fields

FieldTypeDescription
typeType enumAddress level
with_prefixboolInclude administrative prefix

11.3 Example

Proto
mock: {
  china_address_rule: {
    type: ADDRESS;
    with_prefix: true;
  }
}

12. Array and Map Rules

12.1 ArrayRule

Wrap any mock rule for array generation with size control:

Proto
message ArrayRule {
  oneof rule {
    Nature nature = 1;
    StringRule string_rule = 3;
    // ... all other rule types
    NumberRule number_rule = 11;
  }
  uint32 max = 100;  // max array size
  uint32 min = 101;  // min array size
}

Shortcut: Use max/min directly on the Mock message for list size control:

Proto
mock: {
  nature: EMAIL;
  max: 10;  // max 10 items
  min: 3;   // min 3 items
}

12.2 MapRule

Generate key-value pairs with independent key and value rules:

Proto
message MapRule {
  oneof keyRule {
    Nature key_nature = 1;
    StringRule key_string_rule = 3;
    // ... all other rule types
  }
  oneof valueRule {
    Nature val_nature = 101;
    StringRule val_string_rule = 102;
    // ... all other rule types
  }
  uint32 max = 200;
  uint32 min = 201;
}

13. Quick Reference

Common Mock Patterns

Use CaseMock Configuration
Emailmock: { nature: EMAIL }
Phone (China)mock: { nature: CN_PHONE }
UUIDmock: { nature: GUID }
Random namemock: { nature: NAME }
Avatarmock: { nature: AVATAR }
Enum valuesmock: { string_rule: { candidates: ["A", "B", "C"] } }
Pricemock: { number_rule: { min: 1; max: 9999; max_fraction: 2 } }
Birthdaymock: { date_rule: { birth_day: { min_age: 18; max_age: 60 } } }
Recent datemock: { date_rule: { time_gap: 30; time_unit: DAYS; dir: PAST } }
Chinese namemock: { chinese_name_rule: { type: NAME } }
Chinese addressmock: { china_address_rule: { type: ADDRESS } }
Colormock: { color_rule: { type: HEX } }
Placeholder imagemock: { image_rule: { width: 200; height: 200 } }
Copyright © 2026 ApiHug·AI-native Enterprise Architecture Factory