Skip to content

Commit 7d52dbf

Browse files
authored
Merge pull request #349 from abraham/copilot/fix-348
Add ISO 639 format detection for language code parameters and attributes
2 parents c89d6f7 + a9680a3 commit 7d52dbf

File tree

4 files changed

+387
-13
lines changed

4 files changed

+387
-13
lines changed

dist/schema.json

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31453,7 +31453,8 @@
3145331453
"type": [
3145431454
"string",
3145531455
"null"
31456-
]
31456+
],
31457+
"format": "iso-639-1"
3145731458
}
3145831459
},
3145931460
"required": [
@@ -31895,7 +31896,8 @@
3189531896
},
3189631897
"locale": {
3189731898
"description": "The locale of the account.",
31898-
"type": "string"
31899+
"type": "string",
31900+
"format": "iso-639-1"
3189931901
},
3190031902
"role": {
3190131903
"description": "The current role of the account.",
@@ -33909,7 +33911,8 @@
3390933911
"description": "Primary languages of the website and its staff.",
3391033912
"type": "array",
3391133913
"items": {
33912-
"type": "string"
33914+
"type": "string",
33915+
"format": "iso-639-1"
3391333916
}
3391433917
},
3391533918
"registrations": {
@@ -34813,7 +34816,8 @@
3481334816
"type": [
3481434817
"string",
3481534818
"null"
34816-
]
34819+
],
34820+
"format": "iso-639-1"
3481734821
}
3481834822
},
3481934823
"required": [
@@ -35334,7 +35338,8 @@
3533435338
"null"
3533535339
],
3533635340
"items": {
35337-
"type": "string"
35341+
"type": "string",
35342+
"format": "iso-639-1"
3533835343
}
3533935344
}
3534035345
},
@@ -35692,7 +35697,8 @@
3569235697
"type": [
3569335698
"string",
3569435699
"null"
35695-
]
35700+
],
35701+
"format": "iso-639-1"
3569635702
},
3569735703
"media_ids": {
3569835704
"description": "IDs of the MediaAttachments that will be attached to the status.",
@@ -36013,7 +36019,8 @@
3601336019
"type": [
3601436020
"string",
3601536021
"null"
36016-
]
36022+
],
36023+
"format": "iso-639-1"
3601736024
},
3601836025
"muted": {
3601936026
"description": "If the current token has an authorized user: Have you muted notifications for this status's conversation?",
@@ -36650,7 +36657,8 @@
3665036657
},
3665136658
"detected_source_language": {
3665236659
"description": "The language of the source text, as auto-detected by the machine translation provider.",
36653-
"type": "string"
36660+
"type": "string",
36661+
"format": "iso-639-1"
3665436662
},
3665536663
"media_attachments": {
3665636664
"description": "The translated media descriptions of the status.",
@@ -36958,7 +36966,8 @@
3695836966
"description": "Primary languages of the website and its staff.",
3695936967
"type": "array",
3696036968
"items": {
36961-
"type": "string"
36969+
"type": "string",
36970+
"format": "iso-639-1"
3696236971
}
3696336972
},
3696436973
"registrations": {
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
import { EntityConverter } from '../../generators/EntityConverter';
2+
import { TypeParser } from '../../generators/TypeParser';
3+
import { UtilityHelpers } from '../../generators/UtilityHelpers';
4+
import { EntityAttribute } from '../../interfaces/EntityAttribute';
5+
6+
describe('EntityConverter - ISO 639 format detection', () => {
7+
let entityConverter: EntityConverter;
8+
let typeParser: TypeParser;
9+
let utilityHelpers: UtilityHelpers;
10+
11+
beforeEach(() => {
12+
utilityHelpers = new UtilityHelpers();
13+
typeParser = new TypeParser(utilityHelpers);
14+
entityConverter = new EntityConverter(typeParser, utilityHelpers);
15+
});
16+
17+
test('should apply iso-639-1 format when attribute type contains "ISO 639-1"', () => {
18+
const attribute: EntityAttribute = {
19+
name: 'language',
20+
type: 'String (ISO 639-1 language two-letter code)',
21+
description: 'The default posting language',
22+
optional: false,
23+
nullable: false,
24+
deprecated: false,
25+
enumValues: [],
26+
versions: ['2.4.2'],
27+
};
28+
29+
const result = entityConverter.convertAttribute(attribute);
30+
expect(result.type).toBe('string');
31+
expect(result.format).toBe('iso-639-1');
32+
});
33+
34+
test('should apply iso-639-1 format when attribute type contains "ISO 639 Part 1"', () => {
35+
const attribute: EntityAttribute = {
36+
name: 'language',
37+
type: 'String (ISO 639 Part 1 two-letter language code)',
38+
description: 'The default posting language',
39+
optional: false,
40+
nullable: false,
41+
deprecated: false,
42+
enumValues: [],
43+
versions: ['2.4.2'],
44+
};
45+
46+
const result = entityConverter.convertAttribute(attribute);
47+
expect(result.type).toBe('string');
48+
expect(result.format).toBe('iso-639-1');
49+
});
50+
51+
test('should apply iso-639-1 format to array items when type indicates ISO 639-1', () => {
52+
const attribute: EntityAttribute = {
53+
name: 'languages',
54+
type: 'Array of String (ISO 639-1 language two-letter code)',
55+
description: 'Which languages are you following from this user?',
56+
optional: false,
57+
nullable: false,
58+
deprecated: false,
59+
enumValues: [],
60+
versions: ['4.0.0'],
61+
};
62+
63+
const result = entityConverter.convertAttribute(attribute);
64+
expect(result.type).toBe('array');
65+
expect(result.items).toEqual({
66+
type: 'string',
67+
format: 'iso-639-1',
68+
});
69+
});
70+
71+
test('should apply iso-639-1 format with nullable attribute', () => {
72+
const attribute: EntityAttribute = {
73+
name: 'language',
74+
type: 'String (ISO 639-1 language two-letter code)',
75+
description: 'The default posting language',
76+
optional: true,
77+
nullable: true,
78+
deprecated: false,
79+
enumValues: [],
80+
versions: ['2.4.2'],
81+
};
82+
83+
const result = entityConverter.convertAttribute(attribute);
84+
expect(result.type).toEqual(['string', 'null']);
85+
expect(result.format).toBe('iso-639-1');
86+
});
87+
88+
test('should apply iso-639-1 format with enum values', () => {
89+
const attribute: EntityAttribute = {
90+
name: 'language',
91+
type: 'String (ISO 639-1 language two-letter code)',
92+
description: 'The default posting language',
93+
optional: false,
94+
nullable: false,
95+
deprecated: false,
96+
enumValues: ['en', 'es', 'fr'],
97+
versions: ['2.4.2'],
98+
};
99+
100+
const result = entityConverter.convertAttribute(attribute);
101+
expect(result.type).toBe('string');
102+
expect(result.format).toBe('iso-639-1');
103+
expect(result.enum).toEqual(['en', 'es', 'fr']);
104+
});
105+
106+
test('should apply iso-639-1 format with case-insensitive detection', () => {
107+
const attribute: EntityAttribute = {
108+
name: 'language',
109+
type: 'String (iso 639-1 language code)',
110+
description: 'The default posting language',
111+
optional: false,
112+
nullable: false,
113+
deprecated: false,
114+
enumValues: [],
115+
versions: ['2.4.2'],
116+
};
117+
118+
const result = entityConverter.convertAttribute(attribute);
119+
expect(result.type).toBe('string');
120+
expect(result.format).toBe('iso-639-1');
121+
});
122+
123+
test('should not apply iso-639-1 format to regular string attributes', () => {
124+
const attribute: EntityAttribute = {
125+
name: 'content',
126+
type: 'String',
127+
description: 'The content of the status',
128+
optional: false,
129+
nullable: false,
130+
deprecated: false,
131+
enumValues: [],
132+
versions: ['1.0.0'],
133+
};
134+
135+
const result = entityConverter.convertAttribute(attribute);
136+
expect(result.type).toBe('string');
137+
expect(result.format).toBeUndefined();
138+
});
139+
140+
test('should not apply iso-639-1 format when type does not contain ISO 639', () => {
141+
const attribute: EntityAttribute = {
142+
name: 'language',
143+
type: 'String (language code)',
144+
description: 'The default posting language',
145+
optional: false,
146+
nullable: false,
147+
deprecated: false,
148+
enumValues: [],
149+
versions: ['2.4.2'],
150+
};
151+
152+
const result = entityConverter.convertAttribute(attribute);
153+
expect(result.type).toBe('string');
154+
expect(result.format).toBeUndefined();
155+
});
156+
157+
test('should preserve other formats and not apply iso-639-1 when datetime format is detected', () => {
158+
const attribute: EntityAttribute = {
159+
name: 'created_at',
160+
type: 'String (ISO8601 datetime)',
161+
description: 'When the account was created',
162+
optional: false,
163+
nullable: false,
164+
deprecated: false,
165+
enumValues: [],
166+
versions: ['1.0.0'],
167+
};
168+
169+
const result = entityConverter.convertAttribute(attribute);
170+
expect(result.type).toBe('string');
171+
expect(result.format).toBe('date-time');
172+
});
173+
174+
test('should apply iso-639-1 format with "or empty string" variation', () => {
175+
const attribute: EntityAttribute = {
176+
name: 'language',
177+
type: 'String (ISO 639-1 language two-letter code) or empty string',
178+
description: 'The default posting language',
179+
optional: true,
180+
nullable: true,
181+
deprecated: false,
182+
enumValues: [],
183+
versions: ['2.4.2'],
184+
};
185+
186+
const result = entityConverter.convertAttribute(attribute);
187+
expect(result.type).toEqual(['string', 'null']);
188+
expect(result.format).toBe('iso-639-1');
189+
});
190+
});

0 commit comments

Comments
 (0)