Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parser): handle composition with any-of and properties #18

Merged
merged 1 commit into from
Feb 20, 2024
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
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
Loading