···1212 if (item.$ref) {
1313 // Fetch the paths to the definitions, this converts:
1414 // "#/components/schemas/Form" to ["components", "schemas", "Form"]
1515- const paths = item.$ref
1616- .replace(/^#/g, '')
1717- .split('/')
1818- .filter((item) => item);
1515+ const paths = item.$ref.replace(/^#/g, '').split('/').filter(Boolean);
19162017 // Try to find the reference by walking down the path,
2118 // if we cannot find it, then we throw an error.
···11-import type { Model } from '../../common/interfaces/client';
11+import type { Client } from '../../../types/client';
22import { reservedWords } from '../../common/parser/reservedWords';
33import { getType } from '../../common/parser/type';
44import type { OpenApi } from '../interfaces/OpenApi';
55import { getModel } from './getModel';
6677-export const getModels = (openApi: OpenApi): Model[] => {
77+export const getModels = (
88+ openApi: OpenApi,
99+): Pick<Client, 'models' | 'types'> => {
1010+ const types: Client['types'] = {};
1111+ let models: Client['models'] = [];
1212+813 if (!openApi.components) {
99- return [];
1414+ return {
1515+ models,
1616+ types,
1717+ };
1018 }
11191212- let models: Model[] = [];
1313-1420 Object.entries(openApi.components.schemas ?? {}).forEach(
1521 ([definitionName, definition]) => {
1622 const definitionType = getType({ type: definitionName });
2323+ const name = definitionType.base.replace(reservedWords, '_$1');
2424+ const meta = {
2525+ $ref: `#/components/schemas/${definitionName}`,
2626+ name,
2727+ };
2828+ types[name] = meta;
1729 const model = getModel({
1830 definition,
1931 isDefinition: true,
2020- meta: {
2121- $ref: `#/components/schemas/${definitionName}`,
2222- name: definitionType.base.replace(reservedWords, '_$1'),
2323- },
3232+ meta,
2433 openApi,
3434+ types,
2535 });
2636 models = [...models, model];
2737 },
···3545 }
36463747 const definitionType = getType({ type: definitionName });
4848+ /**
4949+ * Prefix parameter names to avoid name conflicts with schemas.
5050+ * Assuming people are mostly interested in importing schema types
5151+ * and don't care about this name as much. It should be resolved in
5252+ * a cleaner way, there just isn't a good deduplication strategy
5353+ * today. This is a workaround in the meantime, hopefully reducing
5454+ * the chance of conflicts.
5555+ *
5656+ * Example where this would break: schema named `ParameterFoo` and
5757+ * parameter named `Foo` (this would transform to `ParameterFoo`)
5858+ *
5959+ * Note: there's a related code to this workaround in `getType()`
6060+ * method that needs to be cleaned up when this is addressed.
6161+ */
6262+ const name = `Parameter${definitionType.base.replace(reservedWords, '_$1')}`;
6363+ const meta = {
6464+ $ref: `#/components/parameters/${definitionName}`,
6565+ name,
6666+ };
6767+ types[name] = meta;
3868 const model = getModel({
3969 definition: schema,
4070 isDefinition: true,
4141- meta: {
4242- $ref: `#/components/parameters/${definitionName}`,
4343- /**
4444- * Prefix parameter names to avoid name conflicts with schemas.
4545- * Assuming people are mostly interested in importing schema types
4646- * and don't care about this name as much. It should be resolved in
4747- * a cleaner way, there just isn't a good deduplication strategy
4848- * today. This is a workaround in the meantime, hopefully reducing
4949- * the chance of conflicts.
5050- *
5151- * Example where this would break: schema named `ParameterFoo` and
5252- * parameter named `Foo` (this would transform to `ParameterFoo`)
5353- *
5454- * Note: there's a related code to this workaround in `getType()`
5555- * method that needs to be cleaned up when this is addressed.
5656- */
5757- name: `Parameter${definitionType.base.replace(reservedWords, '_$1')}`,
5858- },
7171+ meta,
5972 openApi,
7373+ types,
6074 });
6175 model.deprecated = definition.deprecated;
6276 model.description = definition.description || null;
···6478 },
6579 );
66806767- return models;
8181+ return {
8282+ models,
8383+ types,
8484+ };
6885};