Skip to content

Commit 9d3c46e

Browse files
fix: discriminator with special character and default (#2333)
1 parent e673094 commit 9d3c46e

File tree

3 files changed

+138
-3
lines changed

3 files changed

+138
-3
lines changed

src/generators/java/renderers/ClassRenderer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,15 @@ export const JAVA_DEFAULT_CLASS_PRESET: ClassPresetType<JavaOptions> = {
270270
}
271271

272272
const isDiscriminatorProperty =
273-
property.propertyName === model.options.discriminator?.discriminator;
273+
property.unconstrainedPropertyName ===
274+
model.options.discriminator?.discriminator;
274275
if (!isDiscriminatorProperty && property.property.originalInput?.default) {
275276
return JavaDefaultRendererUtil.renderFieldWithDefault(property);
276277
}
277278

278279
if (
279280
options.useModelNameAsConstForDiscriminatorProperty &&
280-
property.unconstrainedPropertyName ===
281-
model.options.discriminator?.discriminator &&
281+
isDiscriminatorProperty &&
282282
property.property.type === 'String'
283283
) {
284284
return `private final ${property.property.type} ${property.propertyName} = "${model.name}";`;

test/generators/java/JavaGenerator.spec.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,106 @@ 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 Owner example',
900+
version: '1.0.0'
901+
},
902+
channels: {
903+
owner: {
904+
address: 'owner',
905+
messages: {
906+
Pet: {
907+
$ref: '#/components/messages/Owner'
908+
}
909+
}
910+
}
911+
},
912+
operations: {
913+
ownerAvailable: {
914+
action: 'receive',
915+
channel: {
916+
$ref: '#/channels/owner'
917+
}
918+
}
919+
},
920+
components: {
921+
messages: {
922+
Owner: {
923+
payload: {
924+
schema: {
925+
$ref: '#/components/schemas/Owner'
926+
}
927+
}
928+
}
929+
},
930+
schemas: {
931+
Owner: {
932+
title: 'Owner',
933+
type: 'object',
934+
properties: {
935+
pets: {
936+
type: 'array',
937+
items: {
938+
$ref: '#/components/schemas/Pet'
939+
}
940+
}
941+
}
942+
},
943+
Pet: {
944+
title: 'Pet',
945+
type: 'object',
946+
properties: {
947+
'@type': {
948+
type: 'string',
949+
default: 'Fish'
950+
}
951+
},
952+
required: ['@type'],
953+
discriminator: '@type',
954+
oneOf: [
955+
{
956+
$ref: '#/components/schemas/Fish'
957+
},
958+
{
959+
$ref: '#/components/schemas/Bird'
960+
}
961+
]
962+
},
963+
Bird: {
964+
title: 'Bird',
965+
type: 'object',
966+
allOf: [
967+
{
968+
$ref: '#/components/schemas/Pet'
969+
}
970+
]
971+
},
972+
Fish: {
973+
title: 'Fish',
974+
type: 'object',
975+
allOf: [
976+
{
977+
$ref: '#/components/schemas/Pet'
978+
}
979+
]
980+
}
981+
}
982+
}
983+
};
984+
const generator = new JavaGenerator({
985+
useModelNameAsConstForDiscriminatorProperty: true
986+
});
987+
const models = await generator.generate(asyncapiDoc);
988+
expect(models.map((model) => model.result)).toMatchSnapshot();
989+
});
990+
});
991+
892992
describe('when collection type is list and unique items is true it should render a set', () => {
893993
test('should create a set for unique arrays', async () => {
894994
const asyncapiDoc = {

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,41 @@ 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+
String getAtType();
1737+
}",
1738+
"public class Owner {
1739+
private Pet[] pets;
1740+
1741+
public Pet[] getPets() { return this.pets; }
1742+
public void setPets(Pet[] pets) { this.pets = pets; }
1743+
}",
1744+
"public class Fish implements Pet {
1745+
private final String atType = \\"Fish\\";
1746+
private Map<String, Object> additionalProperties;
1747+
1748+
public String getAtType() { return this.atType; }
1749+
1750+
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
1751+
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
1752+
}",
1753+
"public class Bird implements Pet {
1754+
private final String atType = \\"Bird\\";
1755+
private Map<String, Object> additionalProperties;
1756+
1757+
public String getAtType() { return this.atType; }
1758+
1759+
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
1760+
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
1761+
}",
1762+
]
1763+
`;
1764+
17301765
exports[`JavaGenerator should not render optional imports when only required field 1`] = `
17311766
"public class OtherClass {
17321767
private String color;

0 commit comments

Comments
 (0)