Skip to content

Commit

Permalink
feat: adds inheritance with interfaces for java
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethaasan committed Nov 6, 2023
1 parent 766db7a commit 04d0f9a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
6 changes: 5 additions & 1 deletion src/generators/java/presets/ConstraintsPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export const JAVA_CONSTRAINTS_PRESET: JavaPreset = {
return content;
},
// eslint-disable-next-line sonarjs/cognitive-complexity
property({ renderer, property, content }) {
property({ renderer, property, content, model }) {
if (model.options.isExtended) {
return '';
}

const annotations: string[] = [];

if (property.required) {
Expand Down
6 changes: 5 additions & 1 deletion src/generators/java/presets/JacksonPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export const JAVA_JACKSON_PRESET: JavaPreset = {
renderer.dependencyManager.addDependency(JACKSON_ANNOTATION_DEPENDENCY);
return content;
},
property({ renderer, property, content }) {
property({ renderer, property, content, model }) {
if (model.options.isExtended) {
return '';
}

//Properties that are dictionaries with unwrapped options, cannot get the annotation because it cannot be accurately unwrapped by the jackson library.
const isDictionary =
property.property instanceof ConstrainedDictionaryModel;
Expand Down
31 changes: 26 additions & 5 deletions src/generators/java/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class ClassRenderer extends JavaRenderer<ConstrainedObjectModel> {

if (this.model.options.isExtended) {
return `public interface ${this.model.name} {
${this.indent(this.renderBlock(content, 2))}
}`;
${this.indent(this.renderBlock(content, 2))}
}`;
}

const parentUnions = this.getParentUnions();
Expand Down Expand Up @@ -134,25 +134,46 @@ export const JAVA_DEFAULT_CLASS_PRESET: ClassPresetType<JavaOptions> = {
self({ renderer }) {
return renderer.defaultSelf();
},
property({ property }) {
property({ property, model }) {
if (model.options.isExtended) {
return '';
}

if (property.property.options.const?.value) {
return `private final ${property.property.type} ${property.propertyName} = ${property.property.options.const.value};`;
}

return `private ${property.property.type} ${property.propertyName};`;
},
getter({ property }) {
getter({ property, model }) {
const getterName = `get${FormatHelpers.toPascalCase(
property.propertyName
)}`;

if (model.options.isExtended) {
return `public ${property.property.type} ${getterName}();`;
}

return `@Override
public ${property.property.type} ${getterName}() { return this.${property.propertyName}; }`;
},
setter({ property }) {
setter({ property, model }) {
if (property.property.options.const?.value) {
return '';
}
const setterName = FormatHelpers.toPascalCase(property.propertyName);

if (model.options.isExtended) {
if (
model.options.discriminator?.discriminator ===
property.unconstrainedPropertyName
) {
return '';
}

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}; }`;
}
Expand Down
28 changes: 6 additions & 22 deletions test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,14 @@ public interface Pet {
}
}",
"public interface CloudEvent {
@NotNull
@JsonProperty(\\"id\\")
private String id;
@NotNull
@JsonProperty(\\"type\\")
private String type;
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
@Override
public String getId() { return this.id; }
@Override
public void setId(String id) { this.id = id; }
public String getId();
public void setId(String id);
@Override
public String getType() { return this.type; }
@Override
public void setType(String type) { this.type = type; }
public String getType();
@Override
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
@Override
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}",
public Map<String, Object> getAdditionalProperties();
public void setAdditionalProperties(Map<String, Object> additionalProperties);
}",
"public enum CloudEventType {
DOG((String)\\"Dog\\"), CAT((String)\\"Cat\\");
Expand Down

0 comments on commit 04d0f9a

Please sign in to comment.