Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/generators/java/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,31 @@ 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
.map((i) => i.name)
.join(', ')} {
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
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
Loading