Skip to content

Commit

Permalink
fix(core): codegen should not import self and default to required ref…
Browse files Browse the repository at this point in the history
…erence types (#761)
  • Loading branch information
AgentEnder authored Sep 20, 2023
1 parent cdc3654 commit 2310556
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,34 @@ exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: l
export * from './interfaces/person';
export * from './interfaces/temperature';
export * from './interfaces/weather-forecast';
export * from './interfaces/my-type';
"
`;

exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/interfaces/company.ts 1`] = `
"import { Person } from './person';
export interface Company {
CEO?: Person
CEO: Person
employees?: Person[]
}
"
`;

exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/interfaces/my-type.ts 1`] = `
"
export interface MyType {
id: string
children: MyType
}
"
`;

exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/interfaces/person.ts 1`] = `
"import { Company } from './company';
export interface Person {
employer?: Company
employer: Company
}
"
`;
Expand All @@ -42,9 +52,9 @@ import { Person } from './person';
export interface WeatherForecast {
date: string
temperature?: Temperature
temperature: Temperature
summary?: string
forecaster?: Person
forecaster: Person
}
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ export function parsePropertyDefintion(

return {
type: reference,
nullable: true,
nullable:
'nullable' in propertyDefinition
? !!propertyDefinition.nullable
: false,
};
} else if (
propertyDefinition.type &&
Expand Down
221 changes: 120 additions & 101 deletions packages/core/src/generators/swagger-typescript/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,118 +4,132 @@ import { libraryGenerator } from '@nrwl/js';

import generator from './generator';

const MOCK_SWAGGER_JSON = `{
"openapi": "3.0.1",
"info": {
"title": "NxDotnet.Test.Webapi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
"version": "1.0"
},
"paths": {
"/WeatherForecast": {
"get": {
"tags": [
"WeatherForecast"
],
"operationId": "GetWeatherForecast",
"responses": {
"200": {
"description": "Success",
"content": {
"text/plain": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/WeatherForecast"
}
}
},
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/WeatherForecast"
}
}
const MOCK_SWAGGER_JSON = JSON.stringify(
{
openapi: '3.0.1',
info: {
title:
'NxDotnet.Test.Webapi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null',
version: '1.0',
},
paths: {
'/WeatherForecast': {
get: {
tags: ['WeatherForecast'],
operationId: 'GetWeatherForecast',
responses: {
'200': {
description: 'Success',
content: {
'text/plain': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/WeatherForecast',
},
},
},
'application/json': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/WeatherForecast',
},
},
},
'text/json': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/WeatherForecast',
},
},
},
},
"text/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/WeatherForecast"
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Company": {
"type": "object",
"properties": {
"CEO": {
"$ref": "#/components/schemas/Person"
},
"employees": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Person"
},
"nullable": true
}
},
},
"additionalProperties": false
},
"Person": {
"type": "object",
"properties": {
"employer": {
"$ref": "#/components/schemas/Company"
}
},
components: {
schemas: {
Company: {
type: 'object',
properties: {
CEO: {
$ref: '#/components/schemas/Person',
},
employees: {
type: 'array',
items: {
$ref: '#/components/schemas/Person',
},
nullable: true,
},
},
additionalProperties: false,
},
"additionalProperties": false
},
"Temperature": {
"type": "object",
"properties": {
"temperatureC": {
"type": "integer",
"format": "int32"
Person: {
type: 'object',
properties: {
employer: {
$ref: '#/components/schemas/Company',
},
},
"temperatureF": {
"type": "integer",
"format": "int32",
"readOnly": true
}
additionalProperties: false,
},
"additionalProperties": false
},
"WeatherForecast": {
"type": "object",
"properties": {
"date": {
"type": "string",
"format": "date-time"
Temperature: {
type: 'object',
properties: {
temperatureC: {
type: 'integer',
format: 'int32',
},
temperatureF: {
type: 'integer',
format: 'int32',
readOnly: true,
},
},
"temperature": {
"$ref": "#/components/schemas/Temperature"
additionalProperties: false,
},
WeatherForecast: {
type: 'object',
properties: {
date: {
type: 'string',
format: 'date-time',
},
temperature: {
$ref: '#/components/schemas/Temperature',
},
summary: {
type: 'string',
nullable: true,
},
forecaster: {
$ref: '#/components/schemas/Person',
},
},
"summary": {
"type": "string",
"nullable": true
additionalProperties: false,
},
MyType: {
type: 'object',
properties: {
id: {
type: 'string',
},
children: {
$ref: '#/components/schemas/MyType',
},
},
"forecaster": {
"$ref": "#/components/schemas/Person"
}
},
"additionalProperties": false
}
}
}
}`;
},
},
},
null,
2,
);

describe('swagger-typescript generator', () => {
let tree: Tree;
Expand Down Expand Up @@ -154,6 +168,11 @@ describe('swagger-typescript generator', () => {
'libs/generated-ts/src/interfaces/company.ts',
tree,
);
expectFileToMatchSnapshot(
'libs/generated-ts/src/interfaces/my-type.ts',
tree,
);

expectFileToMatchSnapshot('libs/generated-ts/src/index.ts', tree);
});
});
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/generators/swagger-typescript/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ function generateInterfaceFiles(
if (
next !== 'object' &&
!builtInTypes.has(next) &&
!necessary.has(next)
!necessary.has(next) &&
!(next === tsInterface.name)
) {
necessary.set(next, names(next));
}
Expand Down

0 comments on commit 2310556

Please sign in to comment.