Skip to content

Commit

Permalink
Improve inheritance handling to include extension for intermediate in…
Browse files Browse the repository at this point in the history
…terfaces
  • Loading branch information
Jessewb786 committed Jan 20, 2025
1 parent 1feddbd commit d7fd84b
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 233 deletions.
20 changes: 9 additions & 11 deletions src/generators/java/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,29 @@ export class ClassRenderer extends JavaRenderer<ConstrainedObjectModel> {
this.dependencyManager.addDependency('import java.util.Map;');
}

if (this.model.options.isExtended) {
return `public interface ${this.model.name} {
${this.indent(this.renderBlock(content, 2))}
}`;
}
const abstractType = this.model.options.isExtended ? 'interface' : 'class';

const parentUnions = this.getParentUnions();
const extend = this.model.options.extend?.filter(
(extend) => extend.options.isExtended
);
const implement = [...(parentUnions ?? []), ...(extend ?? [])];
const parents = [...(parentUnions ?? []), ...(extend ?? [])];

if (implement.length) {
for (const i of implement) {
if (parents.length) {
for (const i of parents) {
this.dependencyManager.addModelDependency(i);
}

return `public class ${this.model.name} implements ${implement
const inheritanceKeyworkd = this.model.options.isExtended ? 'extends' : 'implements';

return `public ${abstractType} ${this.model.name} ${inheritanceKeyworkd} ${parents
.map((i) => i.name)
.join(', ')} {
${this.indent(this.renderBlock(content, 2))}
}`;
}

return `public class ${this.model.name} {
return `public ${abstractType} ${this.model.name} {
${this.indent(this.renderBlock(content, 2))}
}`;
}
Expand Down Expand Up @@ -168,7 +166,7 @@ export const isDiscriminatorOrDictionary = (
property: ConstrainedObjectPropertyModel
): boolean =>
model.options.discriminator?.discriminator ===
property.unconstrainedPropertyName ||
property.unconstrainedPropertyName ||
property.property instanceof ConstrainedDictionaryModel;

export const JAVA_DEFAULT_CLASS_PRESET: ClassPresetType<JavaOptions> = {
Expand Down
110 changes: 36 additions & 74 deletions test/generators/java/JavaGenerator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
JavaGenerator,
JAVA_COMMON_PRESET,
JAVA_CONSTRAINTS_PRESET,
JAVA_DESCRIPTION_PRESET,
JAVA_JACKSON_PRESET
JAVA_JACKSON_PRESET,
JavaGenerator
} from '../../../src/generators';

describe('JavaGenerator', () => {
Expand Down Expand Up @@ -282,20 +282,20 @@ describe('JavaGenerator', () => {
});

describe('allowInheritance', () => {
test('should create interface for Pet and CloudEvent', async () => {
test('should create interface for Animal, Pet and Dog', async () => {
const asyncapiDoc = {
asyncapi: '2.5.0',
info: {
title: 'CloudEvent example',
version: '1.0.0'
},
channels: {
pet: {
animal: {
publish: {
message: {
oneOf: [
{
$ref: '#/components/messages/Dog'
$ref: '#/components/messages/Boxer'
},
{
$ref: '#/components/messages/Cat'
Expand All @@ -307,96 +307,58 @@ describe('JavaGenerator', () => {
},
components: {
messages: {
Dog: {
Boxer: {
payload: {
title: 'Dog',
allOf: [
{
$ref: '#/components/schemas/CloudEvent'
},
{
type: 'object',
properties: {
type: {
const: 'Dog'
},
data: {
type: 'string'
},
test: {
$ref: '#/components/schemas/TestAllOf'
}
}
}
]
$ref: '#/components/schemas/Boxer'
}
},
Cat: {
payload: {
title: 'Cat',
allOf: [
{
$ref: '#/components/schemas/CloudEvent'
},
{
type: 'object',
properties: {
type: {
const: 'Cat'
},
test: {
$ref: '#/components/schemas/Test'
}
}
}
]
$ref: '#/components/schemas/Cat'
}
}
},
schemas: {
CloudEvent: {
title: 'CloudEvent',
Pet: {
title: 'Pet',
type: 'object',
discriminator: 'type',
discriminator: 'petType',
properties: {
id: {
petType: {
type: 'string'
},
type: {
title: 'CloudEventType',
type: 'string',
description: 'test'
},
sequencetype: {
title: 'CloudEvent.SequenceType',
type: 'string',
enum: ['Integer']
}
},
required: ['id', 'type']
required: ['type']
},
Test: {
title: 'Test',
type: 'object',
properties: {
testProp: {
type: 'string'
Cat: {
title: 'Cat',
allOf: [
{
$ref: '#/components/schemas/Pet'
}
}
]
},
TestAllOf: {
title: 'TestAllOf',
Dog: {
title: 'Dog',
allOf: [
{ $ref: '#/components/schemas/Test' },
{
type: 'object',
properties: {
testProp2: {
type: 'string'
}
}
$ref: '#/components/schemas/Pet'
}
]
},
Boxer: {
title: 'Boxer',
type: 'object',
allOf: [
{
$ref: '#/components/schemas/Dog'
}
],
properties: {
breed: {
const: 'Boxer'
}
}
}
}
}
Expand Down
Loading

0 comments on commit d7fd84b

Please sign in to comment.