Skip to content

Commit

Permalink
feat: adds java interfaces when using allof
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethaasan committed Nov 8, 2023
1 parent 91f2380 commit 199e6fe
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
28 changes: 24 additions & 4 deletions src/generators/java/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ ${this.indent(this.renderBlock(content, 2))}
}
}

const getOverride = (
model: ConstrainedObjectModel,
property: ConstrainedObjectPropertyModel
) => {
const isOverride = model.options.extend?.find((extend) => {
if (
extend instanceof ConstrainedObjectModel &&
extend.properties[property.propertyName]
) {
return true;
}
});

return isOverride ? '@Override\n' : '';
};

export const JAVA_DEFAULT_CLASS_PRESET: ClassPresetType<JavaOptions> = {
self({ renderer }) {
return renderer.defaultSelf();
Expand Down Expand Up @@ -158,8 +174,9 @@ export const JAVA_DEFAULT_CLASS_PRESET: ClassPresetType<JavaOptions> = {
return `public ${property.property.type} ${getterName}();`;
}

return `@Override
public ${property.property.type} ${getterName}() { return this.${property.propertyName}; }`;
return `${getOverride(model, property)}public ${
property.property.type
} ${getterName}() { return this.${property.propertyName}; }`;
},
setter({ property, model }) {
if (property.property.options.const?.value) {
Expand All @@ -179,7 +196,10 @@ public ${property.property.type} ${getterName}() { return this.${property.proper
return `public void set${setterName}(${property.property.type} ${property.propertyName});`;
}

return `@Override
public void set${setterName}(${property.property.type} ${property.propertyName}) { this.${property.propertyName} = ${property.propertyName}; }`;
return `${getOverride(model, property)}public void set${setterName}(${
property.property.type
} ${property.propertyName}) { this.${property.propertyName} = ${
property.propertyName
}; }`;
}
};
3 changes: 3 additions & 0 deletions test/generators/java/JavaGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ describe('JavaGenerator', () => {
properties: {
type: {
const: 'Dog'
},
data: {
type: 'string'
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public interface Pet {
@NotNull
@JsonProperty(\\"type\\")
private final CloudEventType type = CloudEventType.DOG;
@JsonProperty(\\"data\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String data;
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -31,6 +34,9 @@ public interface Pet {
@Override
public CloudEventType getType() { return this.type; }
public String getData() { return this.data; }
public void setData(String data) { this.data = data; }
@Override
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
@Override
Expand All @@ -48,19 +54,21 @@ public interface Pet {
return
Objects.equals(this.id, self.id) &&
Objects.equals(this.type, self.type) &&
Objects.equals(this.data, self.data) &&
Objects.equals(this.additionalProperties, self.additionalProperties);
}
@Override
public int hashCode() {
return Objects.hash((Object)id, (Object)type, (Object)additionalProperties);
return Objects.hash((Object)id, (Object)type, (Object)data, (Object)additionalProperties);
}
@Override
public String toString() {
return \\"class Dog {\\\\n\\" +
\\" id: \\" + toIndentedString(id) + \\"\\\\n\\" +
\\" type: \\" + toIndentedString(type) + \\"\\\\n\\" +
\\" data: \\" + toIndentedString(data) + \\"\\\\n\\" +
\\" additionalProperties: \\" + toIndentedString(additionalProperties) + \\"\\\\n\\" +
\\"}\\";
}
Expand Down

0 comments on commit 199e6fe

Please sign in to comment.