Skip to content

Commit

Permalink
Merge pull request #18 from nicolas-chaulet/fix/composed-schema
Browse files Browse the repository at this point in the history
fix(parser): handle composition with any-of and properties
  • Loading branch information
mrlubos authored Feb 20, 2024
2 parents 920cf80 + d5d95e4 commit 8b1a42c
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ export type Options = {
* @param clientName Custom client class name
* @param useOptions Use options or arguments functions
* @param useUnionTypes Use union types instead of enums
* @param autoformat Process generated files with autoformatter
* @param exportCore Generate core client classes
* @param exportServices Generate services
* @param exportModels Generate models
* @param exportSchemas Generate schemas
* @param ignoreOperationId Ignore operationId
* @param useOperationId should the operationId be used when generating operation names
* @param indent Indentation options (4, 2 or tab)
* @param postfixServices Service name postfix
* @param postfixModels Model name postfix
Expand Down
41 changes: 24 additions & 17 deletions src/openApi/v3/parser/getModelComposition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,30 @@ export const getModelComposition = ({
}

if (properties.length) {
composition.properties.push({
name: 'properties',
export: 'interface',
type: 'any',
base: 'any',
template: null,
link: null,
description: '',
isDefinition: false,
isReadOnly: false,
isNullable: false,
isRequired: false,
imports: [],
enum: [],
enums: [],
properties,
});
const foundComposition = findModelComposition(definition);
if (foundComposition?.type === 'one-of') {
composition.properties.forEach(property => {
property.properties.push(...properties);
});
} else {
composition.properties.push({
name: 'properties',
export: 'interface',
type: 'any',
base: 'any',
template: null,
link: null,
description: '',
isDefinition: false,
isReadOnly: false,
isNullable: false,
isRequired: false,
imports: [],
enum: [],
enums: [],
properties,
});
}
}

return composition;
Expand Down
70 changes: 70 additions & 0 deletions test/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3775,6 +3775,7 @@ export type { CompositionWithNestedAnyOfAndNull } from './models/CompositionWith
export type { CompositionWithOneOf } from './models/CompositionWithOneOf';
export type { CompositionWithOneOfAndComplexArrayDictionary } from './models/CompositionWithOneOfAndComplexArrayDictionary';
export type { CompositionWithOneOfAndNullable } from './models/CompositionWithOneOfAndNullable';
export type { CompositionWithOneOfAndProperties } from './models/CompositionWithOneOfAndProperties';
export type { CompositionWithOneOfAndSimpleArrayDictionary } from './models/CompositionWithOneOfAndSimpleArrayDictionary';
export type { CompositionWithOneOfAndSimpleDictionary } from './models/CompositionWithOneOfAndSimpleDictionary';
export type { CompositionWithOneOfAnonymous } from './models/CompositionWithOneOfAnonymous';
Expand Down Expand Up @@ -3856,6 +3857,7 @@ export { $CompositionWithNestedAnyOfAndNull } from './schemas/$CompositionWithNe
export { $CompositionWithOneOf } from './schemas/$CompositionWithOneOf';
export { $CompositionWithOneOfAndComplexArrayDictionary } from './schemas/$CompositionWithOneOfAndComplexArrayDictionary';
export { $CompositionWithOneOfAndNullable } from './schemas/$CompositionWithOneOfAndNullable';
export { $CompositionWithOneOfAndProperties } from './schemas/$CompositionWithOneOfAndProperties';
export { $CompositionWithOneOfAndSimpleArrayDictionary } from './schemas/$CompositionWithOneOfAndSimpleArrayDictionary';
export { $CompositionWithOneOfAndSimpleDictionary } from './schemas/$CompositionWithOneOfAndSimpleDictionary';
export { $CompositionWithOneOfAnonymous } from './schemas/$CompositionWithOneOfAnonymous';
Expand Down Expand Up @@ -4344,6 +4346,26 @@ export type CompositionWithOneOfAndNullable = {
"
`;

exports[`v3 should generate: test/generated/v3/models/CompositionWithOneOfAndProperties.ts 1`] = `
"/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { NonAsciiStringæøåÆØÅöôêÊ字符串 } from './NonAsciiStringæøåÆØÅöôêÊ字符串';
import type { SimpleParameter } from './SimpleParameter';
export type CompositionWithOneOfAndProperties = ({
foo: SimpleParameter;
baz: number | null;
qux: number;
} | {
bar: NonAsciiStringæøåÆØÅöôêÊ字符串;
baz: number | null;
qux: number;
});

"
`;

exports[`v3 should generate: test/generated/v3/models/CompositionWithOneOfAndSimpleArrayDictionary.ts 1`] = `
"/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
Expand Down Expand Up @@ -5802,6 +5824,54 @@ export const $CompositionWithOneOfAndNullable = {
"
`;

exports[`v3 should generate: test/generated/v3/schemas/$CompositionWithOneOfAndProperties.ts 1`] = `
"/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
export const $CompositionWithOneOfAndProperties = {
type: 'one-of',
contains: [{
properties: {
foo: {
type: 'SimpleParameter',
isRequired: true,
},
baz: {
type: 'number',
isRequired: true,
isNullable: true,
format: 'uint16',
},
qux: {
type: 'number',
isRequired: true,
format: 'uint8',
},
},
}, {
properties: {
bar: {
type: 'NonAsciiStringæøåÆØÅöôêÊ字符串',
isRequired: true,
},
baz: {
type: 'number',
isRequired: true,
isNullable: true,
format: 'uint16',
},
qux: {
type: 'number',
isRequired: true,
format: 'uint8',
},
},
}],
} as const;
"
`;

exports[`v3 should generate: test/generated/v3/schemas/$CompositionWithOneOfAndSimpleArrayDictionary.ts 1`] = `
"/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
Expand Down
46 changes: 46 additions & 0 deletions test/spec/v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -2813,6 +2813,52 @@
}
},
"type": "object"
},
"CompositionWithOneOfAndProperties": {
"type": "object",
"oneOf": [
{
"type": "object",
"required": [
"foo"
],
"properties": {
"foo": {
"$ref": "#/components/parameters/SimpleParameter"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"bar"
],
"properties": {
"bar": {
"$ref": "#/components/schemas/NonAsciiStringæøåÆØÅöôêÊ字符串"
}
},
"additionalProperties": false
}
],
"required": [
"baz",
"qux"
],
"properties": {
"baz": {
"type": "integer",
"format": "uint16",
"minimum": 0.0,
"nullable": true
},
"qux": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
}
}
}
}
Expand Down

0 comments on commit 8b1a42c

Please sign in to comment.