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

Bugfix/required and nullable difference #150

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export interface ISwaggerOptions {
urlFilters?: Array<string>
/** custom function to format the output file (default: prettier.format()) **/
format?: (s: string) => string
/** match with tsconfig */
/** force required option for fields */
strictRequiredChecks?: boolean | undefined
/** force nullable option for fields */
strictNullChecks?: boolean | undefined
/** definition Class mode */
modelMode?: 'class' | 'interface'
Expand All @@ -86,6 +88,7 @@ const defaultOptions: ISwaggerOptions = {
useStaticMethod: true,
useCustomerRequestInstance: false,
include: [],
strictRequiredChecks: true,
strictNullChecks: true,
/** definition Class mode ,auto use interface mode to streamlined code*/
modelMode?: 'interface'
Expand Down
5 changes: 3 additions & 2 deletions example/swagger/codegen-customMethodNameMode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { codegen } = require('../../dist/index.js')
const { codegen } = require('../../dist/index.js');

codegen({
methodNameMode: (reqProps) => {
Expand All @@ -9,9 +9,10 @@ codegen({
},
source: require('../swagger-operationId.json'),
outputDir: './swagger/services',
strictRequiredChecks: false,
strictNullChecks: false,
modelMode: 'interface',
extendDefinitionFile: './swagger/customerDefinition.ts',
extendGenericType: ['JsonResult'],
sharedServiceOptions: true
})
});
5 changes: 3 additions & 2 deletions example/swagger/codegen-shortOperationId.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const { codegen } = require('../../dist/index.js')
const { codegen } = require('../../dist/index.js');

codegen({
methodNameMode: 'shortOperationId',
source: require('../swagger-operationId.json'),
outputDir: './swagger/services',
strictRequiredChecks: false,
strictNullChecks: false,
modelMode: 'interface',
extendDefinitionFile: './swagger/customerDefinition.ts',
extendGenericType: ['JsonResult'],
sharedServiceOptions: true
})
});
5 changes: 3 additions & 2 deletions example/swagger/codegen.generic.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// const { codegen } = require('swagger-axios-codegen')
const { codegen } = require('../../dist/index.js')
const { codegen } = require('../../dist/index.js');

codegen({
methodNameMode: 'path',
source: require('../swaggerGeneric.json'),
// remoteUrl: 'http://localhost:44307/swagger/v1/swagger.json',
outputDir: './swagger/services',
fileName: 'indexGeneric.ts',
strictRequiredChecks: false,
strictNullChecks: false,
modelMode: 'interface'
})
});
7 changes: 4 additions & 3 deletions example/swagger/codegen.include.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// const { codegen } = require('swagger-axios-codegen')
const { codegen } = require('../../dist/index.js')
const { codegen } = require('../../dist/index.js');

let include = [
// "products-test",
Expand All @@ -11,12 +11,13 @@ let include = [
// 'Products*',
'!Products',
{ 'User': ['*', '!history'] },
]
];
codegen({
methodNameMode: 'path',
strictRequiredChecks: false,
strictNullChecks: false,
modelMode: 'interface',
source: require('../swagger.json'),
outputDir: './swagger/services',
include
})
});
5 changes: 3 additions & 2 deletions example/swagger/codegen.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// const { codegen } = require('swagger-axios-codegen')
const { codegen } = require('../../dist/index.js')
const { codegen } = require('../../dist/index.js');

codegen({
methodNameMode: 'path',
source: require('../swagger.json'),
// remoteUrl: 'http://localhost:44307/swagger/v1/swagger.json',
outputDir: './swagger/services',
strictRequiredChecks: false,
strictNullChecks: false,
// useCustomerRequestInstance: true,
modelMode: 'interface',
extendDefinitionFile: './swagger/customerDefinition.ts',
extendGenericType: ['JsonResult'],
sharedServiceOptions: true
})
});
5 changes: 3 additions & 2 deletions example/swagger/codegen.v3.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// const { codegen } = require('swagger-axios-codegen')
const { codegen } = require('../../dist/index.js')
const { codegen } = require('../../dist/index.js');

codegen({
methodNameMode: 'path',
source: require('../swagger3.json'),
// remoteUrl: 'http://localhost:44307/swagger/v1/swagger.json',
outputDir: './swagger/services',
fileName: 'indexv3.ts',
strictRequiredChecks: false,
strictNullChecks: false,
modelMode: 'interface'
})
});
4 changes: 3 additions & 1 deletion src/baseInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export interface ISwaggerOptions {
/** include types which are not included during the filtering **/
includeTypes?: Array<string>
format?: (s: string) => string
/** match with tsconfig */
/** force required option for fields */
strictRequiredChecks?: boolean | undefined
/** force required option for fields */
strictNullChecks?: boolean | undefined
/** definition Class mode */
modelMode?: 'class' | 'interface'
Expand Down
6 changes: 4 additions & 2 deletions src/index.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ function codegenInclude(
if (allImport.includes(item.name)) {
const text =
options.modelMode === 'interface'
? interfaceTemplate(item.value.name, item.value.props, [], options.strictNullChecks)
? interfaceTemplate(item.value.name, item.value.props, [], options.strictRequiredChecks, options.strictNullChecks)
: classTemplate(
item.value.name,
item.value.props,
[],
options.strictRequiredChecks,
options.strictNullChecks,
options.useClassTransformer,
options.generateValidationModel
Expand Down Expand Up @@ -225,11 +226,12 @@ function codegenMultimatchInclude(
if (allImport.includes(item.name)) {
const text =
options.modelMode === 'interface'
? interfaceTemplate(item.value.name, item.value.props, [], options.strictNullChecks)
? interfaceTemplate(item.value.name, item.value.props, [], options.strictRequiredChecks, options.strictNullChecks)
: classTemplate(
item.value.name,
item.value.props,
[],
options.strictRequiredChecks,
options.strictNullChecks,
options.useClassTransformer,
options.generateValidationModel
Expand Down
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
classTemplate,
typeTemplate
} from './templates/template'
import { customerServiceHeader, serviceHeader, definitionHeader, disableLint } from './templates/serviceHeader'
import { customerServiceHeader, serviceHeader, definitionHeader } from './templates/serviceHeader'
import { isOpenApi3, findDeepRefs, setDefinedGenericTypes, getDefinedGenericTypes, trimString } from './utils'
import { requestCodegen, IRequestClass, IRequestMethods } from './requestCodegen'
import { componentsCodegen } from './componentsCodegen'
Expand All @@ -31,6 +31,7 @@ const defaultOptions: ISwaggerOptions = {
modelMode: 'interface',
include: [],
includeTypes: [],
strictRequiredChecks: true,
strictNullChecks: true,
useClassTransformer: false,
extendGenericType: [],
Expand Down Expand Up @@ -125,9 +126,7 @@ export async function codegen(params: ISwaggerOptions) {
for (const item of allImport) {
if (!uniqueImports.includes(item)) uniqueImports.push(item)
}
console.log(disableLint());

text = disableLint() + text
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove disableLint

text = serviceTemplate(className + options.serviceNameSuffix, text, uniqueImports)
writeFile(options.outputDir || '', className + 'Service.ts', format(text, options))
})
Expand All @@ -139,11 +138,12 @@ export async function codegen(params: ISwaggerOptions) {
Object.values(models).forEach(item => {
const text =
params.modelMode === 'interface'
? interfaceTemplate(item.value.name, item.value.props, [], params.strictNullChecks)
? interfaceTemplate(item.value.name, item.value.props, [], params.strictRequiredChecks, params.strictNullChecks)
: classTemplate(
item.value.name,
item.value.props,
[],
params.strictRequiredChecks,
params.strictNullChecks,
options.useClassTransformer,
options.generateValidationModel
Expand Down Expand Up @@ -219,11 +219,12 @@ function codegenAll(
Object.values(models).forEach(item => {
const text =
options.modelMode === 'interface'
? interfaceTemplate(item.value.name, item.value.props, [], options.strictNullChecks)
? interfaceTemplate(item.value.name, item.value.props, [], options.strictRequiredChecks, options.strictNullChecks)
: classTemplate(
item.value.name,
item.value.props,
[],
options.strictRequiredChecks,
options.strictNullChecks,
options.useClassTransformer,
options.generateValidationModel
Expand All @@ -244,9 +245,7 @@ function codegenAll(
}
apiSource += text
})
// console.log(disableLint());

apiSource = disableLint() + apiSource
writeFile(options.outputDir || '', options.fileName || '', format(apiSource, options))
} catch (error) {
console.log('error', error)
Expand Down Expand Up @@ -313,11 +312,12 @@ function codegenInclude(
if (allImport.includes(item.name) || options.includeTypes.includes(item.name)) {
const text =
options.modelMode === 'interface'
? interfaceTemplate(item.value.name, item.value.props, [], options.strictNullChecks)
? interfaceTemplate(item.value.name, item.value.props, [], options.strictRequiredChecks, options.strictNullChecks)
: classTemplate(
item.value.name,
item.value.props,
[],
options.strictRequiredChecks,
options.strictNullChecks,
options.useClassTransformer,
options.generateValidationModel
Expand Down Expand Up @@ -459,11 +459,12 @@ function codegenMultimatchInclude(
if (allImport.includes(item.name) || options.includeTypes.includes(item.name)) {
const text =
options.modelMode === 'interface'
? interfaceTemplate(item.value.name, item.value.props, [], options.strictNullChecks)
? interfaceTemplate(item.value.name, item.value.props, [], options.strictRequiredChecks, options.strictNullChecks)
: classTemplate(
item.value.name,
item.value.props,
[],
options.strictRequiredChecks,
options.strictNullChecks,
options.useClassTransformer,
options.generateValidationModel
Expand All @@ -489,7 +490,6 @@ function codegenMultimatchInclude(
}
})

apiSource = disableLint() + apiSource
apiSource += reqSource + defSource
writeFile(options.outputDir || '', options.fileName || '', format(apiSource, options))
}
Expand Down
2 changes: 2 additions & 0 deletions src/swaggerInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ export interface IDefinitionProperty {
type: string
enum: any[]
format: string
readOnly: boolean
nullable: boolean
maxLength: number
$ref: string
allOf: IDefinitionProperty[]
Expand Down
7 changes: 0 additions & 7 deletions src/templates/serviceHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ export function serviceHeader(options: ISwaggerOptions) {
`;
}

export function disableLint() {
return `/** Generate by swagger-axios-codegen */
// @ts-nocheck
/* eslint-disable */

`}


export function customerServiceHeader(options: ISwaggerOptions) {

Expand Down
Loading