Skip to content

Commit a6e7502

Browse files
committed
feat: Include 'extends' for intermediate interfaces
1 parent 1feddbd commit a6e7502

File tree

3 files changed

+237
-233
lines changed

3 files changed

+237
-233
lines changed

src/generators/java/renderers/ClassRenderer.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,29 @@ export class ClassRenderer extends JavaRenderer<ConstrainedObjectModel> {
3232
this.dependencyManager.addDependency('import java.util.Map;');
3333
}
3434

35-
if (this.model.options.isExtended) {
36-
return `public interface ${this.model.name} {
37-
${this.indent(this.renderBlock(content, 2))}
38-
}`;
39-
}
35+
const abstractType = this.model.options.isExtended ? 'interface' : 'class';
4036

4137
const parentUnions = this.getParentUnions();
4238
const extend = this.model.options.extend?.filter(
4339
(extend) => extend.options.isExtended
4440
);
45-
const implement = [...(parentUnions ?? []), ...(extend ?? [])];
41+
const parents = [...(parentUnions ?? []), ...(extend ?? [])];
4642

47-
if (implement.length) {
48-
for (const i of implement) {
43+
if (parents.length) {
44+
for (const i of parents) {
4945
this.dependencyManager.addModelDependency(i);
5046
}
5147

52-
return `public class ${this.model.name} implements ${implement
48+
const inheritanceKeyworkd = this.model.options.isExtended ? 'extends' : 'implements';
49+
50+
return `public ${abstractType} ${this.model.name} ${inheritanceKeyworkd} ${parents
5351
.map((i) => i.name)
5452
.join(', ')} {
5553
${this.indent(this.renderBlock(content, 2))}
5654
}`;
5755
}
5856

59-
return `public class ${this.model.name} {
57+
return `public ${abstractType} ${this.model.name} {
6058
${this.indent(this.renderBlock(content, 2))}
6159
}`;
6260
}
@@ -168,7 +166,7 @@ export const isDiscriminatorOrDictionary = (
168166
property: ConstrainedObjectPropertyModel
169167
): boolean =>
170168
model.options.discriminator?.discriminator ===
171-
property.unconstrainedPropertyName ||
169+
property.unconstrainedPropertyName ||
172170
property.property instanceof ConstrainedDictionaryModel;
173171

174172
export const JAVA_DEFAULT_CLASS_PRESET: ClassPresetType<JavaOptions> = {

test/generators/java/JavaGenerator.spec.ts

Lines changed: 36 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
2-
JavaGenerator,
32
JAVA_COMMON_PRESET,
43
JAVA_CONSTRAINTS_PRESET,
54
JAVA_DESCRIPTION_PRESET,
6-
JAVA_JACKSON_PRESET
5+
JAVA_JACKSON_PRESET,
6+
JavaGenerator
77
} from '../../../src/generators';
88

99
describe('JavaGenerator', () => {
@@ -282,20 +282,20 @@ describe('JavaGenerator', () => {
282282
});
283283

284284
describe('allowInheritance', () => {
285-
test('should create interface for Pet and CloudEvent', async () => {
285+
test('should create interface for Animal, Pet and Dog', async () => {
286286
const asyncapiDoc = {
287287
asyncapi: '2.5.0',
288288
info: {
289289
title: 'CloudEvent example',
290290
version: '1.0.0'
291291
},
292292
channels: {
293-
pet: {
293+
animal: {
294294
publish: {
295295
message: {
296296
oneOf: [
297297
{
298-
$ref: '#/components/messages/Dog'
298+
$ref: '#/components/messages/Boxer'
299299
},
300300
{
301301
$ref: '#/components/messages/Cat'
@@ -307,96 +307,58 @@ describe('JavaGenerator', () => {
307307
},
308308
components: {
309309
messages: {
310-
Dog: {
310+
Boxer: {
311311
payload: {
312-
title: 'Dog',
313-
allOf: [
314-
{
315-
$ref: '#/components/schemas/CloudEvent'
316-
},
317-
{
318-
type: 'object',
319-
properties: {
320-
type: {
321-
const: 'Dog'
322-
},
323-
data: {
324-
type: 'string'
325-
},
326-
test: {
327-
$ref: '#/components/schemas/TestAllOf'
328-
}
329-
}
330-
}
331-
]
312+
$ref: '#/components/schemas/Boxer'
332313
}
333314
},
334315
Cat: {
335316
payload: {
336-
title: 'Cat',
337-
allOf: [
338-
{
339-
$ref: '#/components/schemas/CloudEvent'
340-
},
341-
{
342-
type: 'object',
343-
properties: {
344-
type: {
345-
const: 'Cat'
346-
},
347-
test: {
348-
$ref: '#/components/schemas/Test'
349-
}
350-
}
351-
}
352-
]
317+
$ref: '#/components/schemas/Cat'
353318
}
354319
}
355320
},
356321
schemas: {
357-
CloudEvent: {
358-
title: 'CloudEvent',
322+
Pet: {
323+
title: 'Pet',
359324
type: 'object',
360-
discriminator: 'type',
325+
discriminator: 'petType',
361326
properties: {
362-
id: {
327+
petType: {
363328
type: 'string'
364-
},
365-
type: {
366-
title: 'CloudEventType',
367-
type: 'string',
368-
description: 'test'
369-
},
370-
sequencetype: {
371-
title: 'CloudEvent.SequenceType',
372-
type: 'string',
373-
enum: ['Integer']
374329
}
375330
},
376-
required: ['id', 'type']
331+
required: ['type']
377332
},
378-
Test: {
379-
title: 'Test',
380-
type: 'object',
381-
properties: {
382-
testProp: {
383-
type: 'string'
333+
Cat: {
334+
title: 'Cat',
335+
allOf: [
336+
{
337+
$ref: '#/components/schemas/Pet'
384338
}
385-
}
339+
]
386340
},
387-
TestAllOf: {
388-
title: 'TestAllOf',
341+
Dog: {
342+
title: 'Dog',
389343
allOf: [
390-
{ $ref: '#/components/schemas/Test' },
391344
{
392-
type: 'object',
393-
properties: {
394-
testProp2: {
395-
type: 'string'
396-
}
397-
}
345+
$ref: '#/components/schemas/Pet'
398346
}
399347
]
348+
},
349+
Boxer: {
350+
title: 'Boxer',
351+
type: 'object',
352+
allOf: [
353+
{
354+
$ref: '#/components/schemas/Dog'
355+
}
356+
],
357+
properties: {
358+
breed: {
359+
const: 'Boxer'
360+
}
361+
}
400362
}
401363
}
402364
}

0 commit comments

Comments
 (0)