Skip to content

Commit 9f2faa2

Browse files
Test case for discriminator with special character and default
1 parent e673094 commit 9f2faa2

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

test/generators/java/JavaGenerator.spec.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,94 @@ describe('JavaGenerator', () => {
889889
});
890890
});
891891

892+
describe('renders discriminator containing a special character', () => {
893+
test('when discriminator has a default', async () => {
894+
// note: the default discriminator value is not used in the output. See JacksonPreset.spec.ts for that.
895+
896+
const asyncapiDoc = {
897+
asyncapi: '3.0.0',
898+
info: {
899+
title: 'Pet example',
900+
version: '1.0.0'
901+
},
902+
channels: {
903+
pet: {
904+
address: 'pet',
905+
messages: {
906+
Pet: {
907+
$ref: '#/components/messages/Pet'
908+
}
909+
}
910+
}
911+
},
912+
operations: {
913+
petAvailable: {
914+
action: 'receive',
915+
channel: {
916+
$ref: '#/channels/pet'
917+
}
918+
}
919+
},
920+
components: {
921+
messages: {
922+
Pet: {
923+
payload: {
924+
schema: {
925+
$ref: '#/components/schemas/Pet'
926+
}
927+
}
928+
}
929+
},
930+
schemas: {
931+
Pet: {
932+
title: 'Pet',
933+
type: 'object',
934+
properties: {
935+
"@type": {
936+
type: 'string',
937+
default: 'Fish'
938+
}
939+
},
940+
required: ['@type'],
941+
discriminator: '@type',
942+
oneOf: [
943+
{
944+
$ref: '#/components/schemas/Fish'
945+
},
946+
{
947+
$ref: '#/components/schemas/Bird'
948+
}
949+
]
950+
},
951+
Bird: {
952+
title: 'Bird',
953+
type: 'object',
954+
allOf: [
955+
{
956+
$ref: '#/components/schemas/Pet'
957+
}
958+
]
959+
},
960+
Fish: {
961+
title: 'Fish',
962+
type: 'object',
963+
allOf: [
964+
{
965+
$ref: '#/components/schemas/Pet'
966+
}
967+
]
968+
}
969+
}
970+
}
971+
};
972+
const generator = new JavaGenerator({
973+
useModelNameAsConstForDiscriminatorProperty: true
974+
});
975+
const models = await generator.generate(asyncapiDoc);
976+
expect(models.map((model) => model.result)).toMatchSnapshot();
977+
});
978+
});
979+
892980
describe('when collection type is list and unique items is true it should render a set', () => {
893981
test('should create a set for unique arrays', async () => {
894982
const asyncapiDoc = {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,37 @@ Array [
17271727
]
17281728
`;
17291729
1730+
exports[`JavaGenerator renders discriminator containing a special character when discriminator has a default 1`] = `
1731+
Array [
1732+
"/**
1733+
* Pet represents a union of types: Fish, Bird
1734+
*/
1735+
public interface Pet {
1736+
1737+
}",
1738+
"public class Fish implements Pet {
1739+
private String atType = \\"Fish\\";
1740+
private Map<String, Object> additionalProperties;
1741+
1742+
public String getAtType() { return this.atType; }
1743+
public void setAtType(String atType) { this.atType = atType; }
1744+
1745+
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
1746+
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
1747+
}",
1748+
"public class Bird implements Pet {
1749+
private String atType = \\"Fish\\";
1750+
private Map<String, Object> additionalProperties;
1751+
1752+
public String getAtType() { return this.atType; }
1753+
public void setAtType(String atType) { this.atType = atType; }
1754+
1755+
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
1756+
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
1757+
}",
1758+
]
1759+
`;
1760+
17301761
exports[`JavaGenerator should not render optional imports when only required field 1`] = `
17311762
"public class OtherClass {
17321763
private String color;

0 commit comments

Comments
 (0)