Skip to content

Commit

Permalink
fix: move jsonproperty annotation to getter
Browse files Browse the repository at this point in the history
  • Loading branch information
memdal committed Feb 24, 2025
1 parent b38dd6c commit 815af16
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 44 deletions.
14 changes: 7 additions & 7 deletions src/generators/java/presets/JacksonPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ export const JAVA_JACKSON_PRESET: JavaPreset = {
return renderer.renderBlock(blocks);
}

blocks.push(
renderer.renderAnnotation(
'JsonProperty',
`"${property.unconstrainedPropertyName}"`
)
);

if (!property.required) {
blocks.push(
renderer.renderAnnotation(
Expand Down Expand Up @@ -84,6 +77,13 @@ export const JAVA_JACKSON_PRESET: JavaPreset = {
const blocks: string[] = [];
if (hasUnwrappedOptions) {
blocks.push(renderer.renderAnnotation('JsonAnyGetter'));
} else {
blocks.push(
renderer.renderAnnotation(
'JsonProperty',
`"${property.unconstrainedPropertyName}"`
)
);
}
blocks.push(content);
return renderer.renderBlock(blocks);
Expand Down
48 changes: 24 additions & 24 deletions test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ public interface Animal {
String getPetType();
}",
"public class Boxer implements Animal, Dog {
@JsonProperty(\\"breed\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private final String breed = \\"Boxer\\";
@JsonProperty(\\"petType\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String petType;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
@JsonProperty(\\"breed\\")
public String getBreed() { return this.breed; }
@JsonProperty(\\"petType\\")
@Override
public String getPetType() { return this.petType; }
@Override
Expand Down Expand Up @@ -82,13 +82,13 @@ public interface Animal {
}",
"public class Cat implements Animal, Pet {
@JsonProperty(\\"petType\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String petType;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
@JsonProperty(\\"petType\\")
public String getPetType() { return this.petType; }
public void setPetType(String petType) { this.petType = petType; }
Expand Down Expand Up @@ -198,18 +198,18 @@ Array [
"public class Event {
@Valid
@NotNull
@JsonProperty(\\"action\\")
private Action action;
@JsonProperty(\\"event_time\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private java.time.OffsetDateTime eventTime;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
@JsonProperty(\\"action\\")
public Action getAction() { return this.action; }
public void setAction(Action action) { this.action = action; }
@JsonProperty(\\"event_time\\")
public java.time.OffsetDateTime getEventTime() { return this.eventTime; }
public void setEventTime(java.time.OffsetDateTime eventTime) { this.eventTime = eventTime; }
Expand Down Expand Up @@ -304,41 +304,41 @@ public interface Pet {
}",
"public class Dog implements Pet {
@NotNull
@JsonProperty(\\"id\\")
private String id;
@NotNull
@JsonProperty(\\"source\\")
private String source;
@NotNull
@JsonProperty(\\"specversion\\")
private final String specversion = \\"1.0\\";
@Valid
@NotNull
@JsonProperty(\\"type\\")
private final CloudEventType type = CloudEventType.DOG;
@JsonProperty(\\"dataschema\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String dataschema;
@JsonProperty(\\"time\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private java.time.OffsetDateTime time;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
@JsonProperty(\\"id\\")
public String getId() { return this.id; }
public void setId(String id) { this.id = id; }
@JsonProperty(\\"source\\")
public String getSource() { return this.source; }
public void setSource(String source) { this.source = source; }
@JsonProperty(\\"specversion\\")
public String getSpecversion() { return this.specversion; }
@JsonProperty(\\"type\\")
public CloudEventType getType() { return this.type; }
@JsonProperty(\\"dataschema\\")
public String getDataschema() { return this.dataschema; }
public void setDataschema(String dataschema) { this.dataschema = dataschema; }
@JsonProperty(\\"time\\")
public java.time.OffsetDateTime getTime() { return this.time; }
public void setTime(java.time.OffsetDateTime time) { this.time = time; }
Expand Down Expand Up @@ -425,41 +425,41 @@ public interface Pet {
}",
"public class Cat implements Pet {
@NotNull
@JsonProperty(\\"id\\")
private String id;
@NotNull
@JsonProperty(\\"source\\")
private String source;
@NotNull
@JsonProperty(\\"specversion\\")
private final String specversion = \\"1.0\\";
@Valid
@NotNull
@JsonProperty(\\"type\\")
private final CloudEventType type = CloudEventType.CAT;
@JsonProperty(\\"dataschema\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String dataschema;
@JsonProperty(\\"time\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private java.time.OffsetDateTime time;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
@JsonProperty(\\"id\\")
public String getId() { return this.id; }
public void setId(String id) { this.id = id; }
@JsonProperty(\\"source\\")
public String getSource() { return this.source; }
public void setSource(String source) { this.source = source; }
@JsonProperty(\\"specversion\\")
public String getSpecversion() { return this.specversion; }
@JsonProperty(\\"type\\")
public CloudEventType getType() { return this.type; }
@JsonProperty(\\"dataschema\\")
public String getDataschema() { return this.dataschema; }
public void setDataschema(String dataschema) { this.dataschema = dataschema; }
@JsonProperty(\\"time\\")
public java.time.OffsetDateTime getTime() { return this.time; }
public void setTime(java.time.OffsetDateTime time) { this.time = time; }
Expand Down Expand Up @@ -522,19 +522,19 @@ exports[`JavaGenerator oneOf/discriminator with jackson preset handle one const
Array [
"public class Dog {
@NotNull
@JsonProperty(\\"id\\")
private String id;
@Valid
@NotNull
@JsonProperty(\\"type\\")
private final CloudEventType type = CloudEventType.DOG;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
@JsonProperty(\\"id\\")
public String getId() { return this.id; }
public void setId(String id) { this.id = id; }
@JsonProperty(\\"type\\")
public CloudEventType getType() { return this.type; }
@JsonAnyGetter
Expand Down Expand Up @@ -629,12 +629,12 @@ public interface Pet {
"public class Dog implements Pet {
@Valid
@NotNull
@JsonProperty(\\"type\\")
private final DogType type = DogType.DOG;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
@JsonProperty(\\"type\\")
public DogType getType() { return this.type; }
@JsonAnyGetter
Expand Down Expand Up @@ -711,12 +711,12 @@ public interface Pet {
"public class Cat implements Pet {
@Valid
@NotNull
@JsonProperty(\\"type\\")
private final CatType type = CatType.CAT;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
@JsonProperty(\\"type\\")
public CatType getType() { return this.type; }
@JsonAnyGetter
Expand Down Expand Up @@ -808,13 +808,13 @@ public interface Vehicle {
}",
"public class Cargo {
@Valid
@JsonProperty(\\"vehicle\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private Vehicle vehicle;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
@JsonProperty(\\"vehicle\\")
public Vehicle getVehicle() { return this.vehicle; }
public void setVehicle(Vehicle vehicle) { this.vehicle = vehicle; }
Expand Down Expand Up @@ -863,12 +863,12 @@ public interface Vehicle {
"public class Car implements Vehicle {
@Valid
@NotNull
@JsonProperty(\\"vehicleType\\")
private final VehicleType vehicleType = VehicleType.CAR;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
@JsonProperty(\\"vehicleType\\")
public VehicleType getVehicleType() { return this.vehicleType; }
@JsonAnyGetter
Expand Down Expand Up @@ -945,12 +945,12 @@ public interface Vehicle {
"public class Truck implements Vehicle {
@Valid
@NotNull
@JsonProperty(\\"vehicleType\\")
private final VehicleType vehicleType = VehicleType.TRUCK;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
@JsonProperty(\\"vehicleType\\")
public VehicleType getVehicleType() { return this.vehicleType; }
@JsonAnyGetter
Expand Down
22 changes: 22 additions & 0 deletions test/generators/java/presets/JacksonPreset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ describe('JAVA_JACKSON_PRESET', () => {
expect(models.map((model) => model.result)).toMatchSnapshot();
});

test('should place @JsonProperty annotation on the getter', async () => {
const doc = {
$id: 'AnnotateGetterWithJsonProperty',
type: 'object',
properties: {
a_property: { type: 'string' }
}
};

const generator = new JavaGenerator({
presets: [JAVA_JACKSON_PRESET],
processorOptions: {
interpreter: {
ignoreAdditionalProperties: true
}
}
});

const models = await generator.generate(doc);
expect(models[0].result).toMatchSnapshot();
});

describe('union', () => {
test('handle oneOf with AsyncAPI discriminator with Jackson', async () => {
const asyncapiDoc = {
Expand Down
Loading

0 comments on commit 815af16

Please sign in to comment.