Skip to content

Commit dd0f924

Browse files
fix: classes implementing union should not have discriminator setter (#2251)
1 parent e7916cf commit dd0f924

File tree

6 files changed

+20
-44
lines changed

6 files changed

+20
-44
lines changed

src/generators/java/JavaPreset.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ export interface UnionPreset<R extends AbstractRenderer, O>
4848
discriminatorGetter?: (
4949
args: PresetArgs<R, O, ConstrainedUnionModel>
5050
) => string;
51-
discriminatorSetter?: (
52-
args: PresetArgs<R, O, ConstrainedUnionModel>
53-
) => string;
5451
}
5552

5653
export type RecordPresetType<O> = InterfacePreset<RecordRenderer, O>;

src/generators/java/renderers/ClassRenderer.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ export class ClassRenderer extends JavaRenderer<ConstrainedObjectModel> {
4646
this.dependencyManager.addModelDependency(i);
4747
}
4848

49-
const inheritanceKeyworkd = this.model.options.isExtended
49+
const inheritanceKeyword = this.model.options.isExtended
5050
? 'extends'
5151
: 'implements';
5252

5353
return `public ${abstractType} ${
5454
this.model.name
55-
} ${inheritanceKeyworkd} ${parents.map((i) => i.name).join(', ')} {
55+
} ${inheritanceKeyword} ${parents.map((i) => i.name).join(', ')} {
5656
${this.indent(this.renderBlock(content, 2))}
5757
}`;
5858
}
@@ -138,7 +138,10 @@ const getOverride = (
138138
property: ConstrainedObjectPropertyModel
139139
) => {
140140
const isOverride = model.options.extend?.find((extend) => {
141-
if (!extend.options.isExtended || isDictionary(model, property)) {
141+
if (
142+
!extend.options.isExtended ||
143+
property.property instanceof ConstrainedDictionaryModel
144+
) {
142145
return false;
143146
}
144147

@@ -166,12 +169,18 @@ export const isDiscriminatorOrDictionary = (
166169
property: ConstrainedObjectPropertyModel
167170
): boolean =>
168171
model.options.discriminator?.discriminator ===
169-
property.unconstrainedPropertyName || isDictionary(model, property);
172+
property.unconstrainedPropertyName ||
173+
property.property instanceof ConstrainedDictionaryModel;
170174

171-
export const isDictionary = (
175+
export const isDiscriminatorInTree = (
172176
model: ConstrainedObjectModel,
173177
property: ConstrainedObjectPropertyModel
174-
): boolean => property.property instanceof ConstrainedDictionaryModel;
178+
): boolean =>
179+
model.options?.extend?.some(
180+
(ext) =>
181+
ext?.options?.discriminator?.discriminator ===
182+
property.unconstrainedPropertyName
183+
) ?? false;
175184

176185
const isEnumImplementedByConstValue = (
177186
model: ConstrainedObjectModel,
@@ -269,6 +278,10 @@ export const JAVA_DEFAULT_CLASS_PRESET: ClassPresetType<JavaOptions> = {
269278
return `public void set${setterName}(${property.property.type} ${property.propertyName});`;
270279
}
271280

281+
if (isDiscriminatorInTree(model, property)) {
282+
return '';
283+
}
284+
272285
// don't render override for enums that are set with a const value
273286
const override = !isEnumOrEnumInExtended(model, property)
274287
? getOverride(model, property)

src/generators/java/renderers/UnionRenderer.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export class UnionRenderer extends JavaRenderer<ConstrainedUnionModel> {
2121

2222
if (this.model.options.discriminator) {
2323
content.push(await this.runDiscriminatorGetterPreset());
24-
content.push(await this.runDiscriminatorSetterPreset());
2524
}
2625
content.push(await this.runAdditionalContentPreset());
2726

@@ -36,10 +35,6 @@ export class UnionRenderer extends JavaRenderer<ConstrainedUnionModel> {
3635
runDiscriminatorGetterPreset(): Promise<string> {
3736
return this.runPreset('discriminatorGetter');
3837
}
39-
40-
runDiscriminatorSetterPreset(): Promise<string> {
41-
return this.runPreset('discriminatorSetter');
42-
}
4338
}
4439

4540
export const JAVA_DEFAULT_UNION_PRESET: UnionPresetType<JavaOptions> = {
@@ -54,17 +49,6 @@ export const JAVA_DEFAULT_UNION_PRESET: UnionPresetType<JavaOptions> = {
5449
return `${model.options.discriminator.type} get${FormatHelpers.toPascalCase(
5550
getSanitizedDiscriminatorName(model)
5651
)}();`;
57-
},
58-
discriminatorSetter({ model }) {
59-
if (!model.options.discriminator?.type) {
60-
return '';
61-
}
62-
63-
return `void set${FormatHelpers.toPascalCase(
64-
getSanitizedDiscriminatorName(model)
65-
)}(${model.options.discriminator.type} ${FormatHelpers.toCamelCase(
66-
getSanitizedDiscriminatorName(model)
67-
)});`;
6852
}
6953
};
7054

test/generators/java/JavaGenerator.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ describe('JavaGenerator', () => {
480480
],
481481
collectionType: 'List',
482482
processorOptions: {
483-
interpreter: {
483+
jsonSchema: {
484484
allowInheritance: true
485485
}
486486
}

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Array [
1212
*/
1313
public interface Animal {
1414
String getPetType();
15-
void setPetType(String petType);
1615
}",
1716
"public class Boxer implements Animal, Dog {
1817
@JsonProperty(\\"breed\\")
@@ -32,8 +31,6 @@ public interface Animal {
3231
3332
@Override
3433
public String getPetType() { return this.petType; }
35-
@Override
36-
public void setPetType(String petType) { this.petType = petType; }
3734
3835
@Override
3936
public String getColor() { return this.color; }
@@ -107,8 +104,6 @@ public interface Animal {
107104
108105
@Override
109106
public String getPetType() { return this.petType; }
110-
@Override
111-
public void setPetType(String petType) { this.petType = petType; }
112107
113108
@Override
114109
public String getColor() { return this.color; }
@@ -175,7 +170,6 @@ Array [
175170
*/
176171
public interface Pet {
177172
String getPetType();
178-
void setPetType(String petType);
179173
}",
180174
"public class Owner {
181175
@JsonProperty(\\"name\\")
@@ -365,7 +359,6 @@ public interface Pet {
365359
private Map<String, Object> additionalProperties;
366360
367361
public String getPetType() { return this.petType; }
368-
public void setPetType(String petType) { this.petType = petType; }
369362
370363
public String getBreed() { return this.breed; }
371364
@@ -428,7 +421,6 @@ Array [
428421
*/
429422
public interface Animal {
430423
String getAtPetType();
431-
void setAtPetType(String atPetType);
432424
}",
433425
"public class FlyingFish implements Animal, Fish {
434426
@JsonProperty(\\"breed\\")
@@ -445,8 +437,6 @@ public interface Animal {
445437
446438
@Override
447439
public String getAtPetType() { return this.atPetType; }
448-
@Override
449-
public void setAtPetType(String atPetType) { this.atPetType = atPetType; }
450440
451441
@JsonAnyGetter
452442
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
@@ -508,8 +498,6 @@ public interface Animal {
508498
509499
@Override
510500
public String getAtPetType() { return this.atPetType; }
511-
@Override
512-
public void setAtPetType(String atPetType) { this.atPetType = atPetType; }
513501
514502
@JsonAnyGetter
515503
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
@@ -563,7 +551,6 @@ Array [
563551
*/
564552
public interface Vehicle {
565553
VehicleType getVehicleType();
566-
void setVehicleType(VehicleType vehicleType);
567554
}",
568555
"public class Car implements Vehicle {
569556
private final VehicleType vehicleType = VehicleType.CAR;
@@ -721,7 +708,6 @@ Array [
721708
*/
722709
public interface Pet {
723710
CloudEventType getType();
724-
void setType(CloudEventType type);
725711
}",
726712
"public class Dog implements Pet {
727713
@NotNull
@@ -1226,7 +1212,6 @@ Array [
12261212
*/
12271213
public interface Vehicle {
12281214
VehicleType getVehicleType();
1229-
void setVehicleType(VehicleType vehicleType);
12301215
}",
12311216
"public class Cargo {
12321217
@Valid

test/generators/java/presets/__snapshots__/JacksonPreset.spec.ts.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ Array [
9393
*/
9494
public interface Vehicle {
9595
String getVehicleType();
96-
void setVehicleType(String vehicleType);
9796
}",
9897
"public class Car implements Vehicle {
9998
@JsonProperty(\\"vehicle_type\\")
@@ -152,7 +151,6 @@ Array [
152151
*/
153152
public interface Vehicle {
154153
String getVehicleType();
155-
void setVehicleType(String vehicleType);
156154
}",
157155
"public class Car implements Vehicle {
158156
@JsonProperty(\\"vehicleType\\")
@@ -199,7 +197,6 @@ Array [
199197
*/
200198
public interface Vehicle {
201199
String getVehicleType();
202-
void setVehicleType(String vehicleType);
203200
}",
204201
"public class Car implements Vehicle {
205202
@JsonProperty(\\"vehicleType\\")

0 commit comments

Comments
 (0)