Skip to content

Commit ffd061f

Browse files
Add test for invalid default
1 parent 83d07f8 commit ffd061f

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

test/generators/java/JavaGenerator.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,61 @@ describe('JavaGenerator', () => {
12481248
const models = await generator.generate(asyncapiDoc);
12491249
expect(models.map((model) => model.result)).toMatchSnapshot();
12501250
});
1251+
1252+
test('should create enum field without default if invalid', async () => {
1253+
const asyncapiDoc = {
1254+
asyncapi: '3.0.0',
1255+
info: {
1256+
title: 'Owner example',
1257+
version: '1.0.0'
1258+
},
1259+
channels: {
1260+
owner: {
1261+
address: 'owner',
1262+
messages: {
1263+
Pet: {
1264+
$ref: '#/components/messages/Owner'
1265+
}
1266+
}
1267+
}
1268+
},
1269+
operations: {
1270+
ownerAvailable: {
1271+
action: 'receive',
1272+
channel: {
1273+
$ref: '#/channels/owner'
1274+
}
1275+
}
1276+
},
1277+
components: {
1278+
messages: {
1279+
Owner: {
1280+
payload: {
1281+
schema: {
1282+
$ref: '#/components/schemas/Owner'
1283+
}
1284+
}
1285+
}
1286+
},
1287+
schemas: {
1288+
Owner: {
1289+
type: 'object',
1290+
properties: {
1291+
legalForm: {
1292+
type: 'string',
1293+
title: 'LegalForm',
1294+
enum: ['PERSON', 'CORPORATION'],
1295+
default: 'NON_ENUM_VALUE'
1296+
}
1297+
}
1298+
}
1299+
}
1300+
}
1301+
};
1302+
1303+
const models = await generator.generate(asyncapiDoc);
1304+
expect(models.map((model) => model.result)).toMatchSnapshot();
1305+
});
12511306
});
12521307

12531308
describe('when default with custom type mappings is used, render field with default', () => {

test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,6 +2303,44 @@ Array [
23032303
]
23042304
`;
23052305
2306+
exports[`JavaGenerator when default is used, render field with default should create enum field without default if invalid 1`] = `
2307+
Array [
2308+
"public class Owner {
2309+
private LegalForm legalForm;
2310+
2311+
public LegalForm getLegalForm() { return this.legalForm; }
2312+
public void setLegalForm(LegalForm legalForm) { this.legalForm = legalForm; }
2313+
}",
2314+
"public enum LegalForm {
2315+
PERSON((String)\\"PERSON\\"), CORPORATION((String)\\"CORPORATION\\");
2316+
2317+
private final String value;
2318+
2319+
LegalForm(String value) {
2320+
this.value = value;
2321+
}
2322+
2323+
public String getValue() {
2324+
return value;
2325+
}
2326+
2327+
public static LegalForm fromValue(String value) {
2328+
for (LegalForm e : LegalForm.values()) {
2329+
if (e.value.equals(value)) {
2330+
return e;
2331+
}
2332+
}
2333+
throw new IllegalArgumentException(\\"Unexpected value '\\" + value + \\"'\\");
2334+
}
2335+
2336+
@Override
2337+
public String toString() {
2338+
return String.valueOf(value);
2339+
}
2340+
}",
2341+
]
2342+
`;
2343+
23062344
exports[`JavaGenerator when default is used, render field with default should create field with default 1`] = `
23072345
Array [
23082346
"public class Owner {

0 commit comments

Comments
 (0)