mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
- Support for JSON Schema type "as an array of primary types"
This commit is contained in:
parent
931ee9c805
commit
50c44cce7e
1
src/client/interfaces/Type.d.ts
vendored
1
src/client/interfaces/Type.d.ts
vendored
@ -3,4 +3,5 @@ export interface Type {
|
||||
base: string;
|
||||
template: string | null;
|
||||
imports: string[];
|
||||
isNullable: boolean;
|
||||
}
|
||||
|
||||
@ -17,6 +17,7 @@ export function getType(value?: string, template?: string): Type {
|
||||
base: 'any',
|
||||
template: null,
|
||||
imports: [],
|
||||
isNullable: false,
|
||||
};
|
||||
|
||||
const valueClean = stripNamespace(value || '');
|
||||
|
||||
2
src/openApi/v3/interfaces/OpenApiSchema.d.ts
vendored
2
src/openApi/v3/interfaces/OpenApiSchema.d.ts
vendored
@ -25,7 +25,7 @@ export interface OpenApiSchema extends OpenApiReference, WithEnumExtension {
|
||||
minProperties?: number;
|
||||
required?: string[];
|
||||
enum?: (string | number)[];
|
||||
type?: string;
|
||||
type?: string | string[];
|
||||
allOf?: OpenApiSchema[];
|
||||
oneOf?: OpenApiSchema[];
|
||||
anyOf?: OpenApiSchema[];
|
||||
|
||||
@ -180,6 +180,7 @@ export function getModel(openApi: OpenApi, definition: OpenApiSchema, isDefiniti
|
||||
model.type = definitionType.type;
|
||||
model.base = definitionType.base;
|
||||
model.template = definitionType.template;
|
||||
model.isNullable = definitionType.isNullable;
|
||||
model.imports.push(...definitionType.imports);
|
||||
model.default = getModelDefault(definition, model);
|
||||
return model;
|
||||
|
||||
@ -7,6 +7,7 @@ describe('getType', () => {
|
||||
expect(type.base).toEqual('number');
|
||||
expect(type.template).toEqual(null);
|
||||
expect(type.imports).toEqual([]);
|
||||
expect(type.isNullable).toEqual(false);
|
||||
});
|
||||
|
||||
it('should convert string', () => {
|
||||
@ -15,6 +16,7 @@ describe('getType', () => {
|
||||
expect(type.base).toEqual('string');
|
||||
expect(type.template).toEqual(null);
|
||||
expect(type.imports).toEqual([]);
|
||||
expect(type.isNullable).toEqual(false);
|
||||
});
|
||||
|
||||
it('should convert string array', () => {
|
||||
@ -23,6 +25,7 @@ describe('getType', () => {
|
||||
expect(type.base).toEqual('string');
|
||||
expect(type.template).toEqual(null);
|
||||
expect(type.imports).toEqual([]);
|
||||
expect(type.isNullable).toEqual(false);
|
||||
});
|
||||
|
||||
it('should convert template with primary', () => {
|
||||
@ -31,6 +34,7 @@ describe('getType', () => {
|
||||
expect(type.base).toEqual('Link');
|
||||
expect(type.template).toEqual('string');
|
||||
expect(type.imports).toEqual(['Link']);
|
||||
expect(type.isNullable).toEqual(false);
|
||||
});
|
||||
|
||||
it('should convert template with model', () => {
|
||||
@ -39,6 +43,7 @@ describe('getType', () => {
|
||||
expect(type.base).toEqual('Link');
|
||||
expect(type.template).toEqual('Model');
|
||||
expect(type.imports).toEqual(['Link', 'Model']);
|
||||
expect(type.isNullable).toEqual(false);
|
||||
});
|
||||
|
||||
it('should have double imports', () => {
|
||||
@ -47,6 +52,7 @@ describe('getType', () => {
|
||||
expect(type.base).toEqual('Link');
|
||||
expect(type.template).toEqual('Link');
|
||||
expect(type.imports).toEqual(['Link', 'Link']);
|
||||
expect(type.isNullable).toEqual(false);
|
||||
});
|
||||
|
||||
it('should convert generic', () => {
|
||||
@ -55,6 +61,7 @@ describe('getType', () => {
|
||||
expect(type.base).toEqual('T');
|
||||
expect(type.template).toEqual(null);
|
||||
expect(type.imports).toEqual([]);
|
||||
expect(type.isNullable).toEqual(false);
|
||||
});
|
||||
|
||||
it('should support dot', () => {
|
||||
@ -63,6 +70,7 @@ describe('getType', () => {
|
||||
expect(type.base).toEqual('model_000');
|
||||
expect(type.template).toEqual(null);
|
||||
expect(type.imports).toEqual(['model_000']);
|
||||
expect(type.isNullable).toEqual(false);
|
||||
});
|
||||
|
||||
it('should support dashes', () => {
|
||||
@ -71,6 +79,7 @@ describe('getType', () => {
|
||||
expect(type.base).toEqual('some_special_schema');
|
||||
expect(type.template).toEqual(null);
|
||||
expect(type.imports).toEqual(['some_special_schema']);
|
||||
expect(type.isNullable).toEqual(false);
|
||||
});
|
||||
|
||||
it('should support dollar sign', () => {
|
||||
@ -79,5 +88,24 @@ describe('getType', () => {
|
||||
expect(type.base).toEqual('$some_special_schema');
|
||||
expect(type.template).toEqual(null);
|
||||
expect(type.imports).toEqual(['$some_special_schema']);
|
||||
expect(type.isNullable).toEqual(false);
|
||||
});
|
||||
|
||||
it('should support multiple base types', () => {
|
||||
const type = getType(['string', 'int']);
|
||||
expect(type.type).toEqual('string | number');
|
||||
expect(type.base).toEqual('string | number');
|
||||
expect(type.template).toEqual(null);
|
||||
expect(type.imports).toEqual([]);
|
||||
expect(type.isNullable).toEqual(false);
|
||||
});
|
||||
|
||||
it('should support multiple nullable types', () => {
|
||||
const type = getType(['string', 'null']);
|
||||
expect(type.type).toEqual('string');
|
||||
expect(type.base).toEqual('string');
|
||||
expect(type.template).toEqual(null);
|
||||
expect(type.imports).toEqual([]);
|
||||
expect(type.isNullable).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
@ -5,21 +5,35 @@ import { stripNamespace } from './stripNamespace';
|
||||
function encode(value: string): string {
|
||||
return value.replace(/^[^a-zA-Z_$]+/g, '').replace(/[^\w$]+/g, '_');
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse any string value into a type object.
|
||||
* @param value String value like "integer" or "Link[Model]".
|
||||
* @param values String or String[] value like "integer", "Link[Model]" or ["string", "null"]
|
||||
* @param template Optional template class from parent (needed to process generics)
|
||||
*/
|
||||
export function getType(value?: string, template?: string): Type {
|
||||
export function getType(values?: string | string[], template?: string): Type {
|
||||
const result: Type = {
|
||||
type: 'any',
|
||||
base: 'any',
|
||||
template: null,
|
||||
imports: [],
|
||||
isNullable: false,
|
||||
};
|
||||
|
||||
const valueClean = stripNamespace(value || '');
|
||||
// Special case for JSON Schema spec (december 2020, page 17),
|
||||
// that allows type to be an array of primitive types...
|
||||
if (Array.isArray(values)) {
|
||||
const type = values
|
||||
.filter(value => value !== 'null')
|
||||
.filter(value => hasMappedType(value))
|
||||
.map(value => getMappedType(value))
|
||||
.join(' | ');
|
||||
result.type = type;
|
||||
result.base = type;
|
||||
result.isNullable = values.includes('null');
|
||||
return result;
|
||||
}
|
||||
|
||||
const valueClean = stripNamespace(values || '');
|
||||
|
||||
if (/\[.*\]$/g.test(valueClean)) {
|
||||
const matches = valueClean.match(/(.*?)\[(.*)\]$/);
|
||||
|
||||
@ -2094,7 +2094,7 @@ export class ParametersService {
|
||||
parameterPath: string,
|
||||
): Promise<void> {
|
||||
const result = await __request({
|
||||
method: 'GET',
|
||||
method: 'POST',
|
||||
path: \`/api/v\${OpenAPI.VERSION}/parameters/\${parameterPath}\`,
|
||||
headers: {
|
||||
'parameterHeader': parameterHeader,
|
||||
@ -2132,7 +2132,7 @@ export class ParametersService {
|
||||
_default?: string,
|
||||
): Promise<void> {
|
||||
const result = await __request({
|
||||
method: 'GET',
|
||||
method: 'POST',
|
||||
path: \`/api/v\${OpenAPI.VERSION}/parameters/\${parameterPath1}/\${parameterPath2}/\${parameterPath3}\`,
|
||||
headers: {
|
||||
'parameter.header': parameterHeader,
|
||||
@ -3572,7 +3572,7 @@ exports[`v3 should generate: ./test/generated/v3/models/SimpleStringWithPattern.
|
||||
/**
|
||||
* This is a simple string
|
||||
*/
|
||||
export type SimpleStringWithPattern = string | null;"
|
||||
export type SimpleStringWithPattern = string;"
|
||||
`;
|
||||
|
||||
exports[`v3 should generate: ./test/generated/v3/schemas/$ArrayWithArray.ts 1`] = `
|
||||
@ -4392,7 +4392,6 @@ exports[`v3 should generate: ./test/generated/v3/schemas/$SimpleStringWithPatter
|
||||
/* eslint-disable */
|
||||
export const $SimpleStringWithPattern = {
|
||||
type: 'string',
|
||||
isNullable: true,
|
||||
maxLength: 64,
|
||||
pattern: '^[a-zA-Z0-9_]*$',
|
||||
};"
|
||||
@ -4791,7 +4790,7 @@ export class ParametersService {
|
||||
requestBody: ModelWithString | null,
|
||||
): Promise<void> {
|
||||
const result = await __request({
|
||||
method: 'GET',
|
||||
method: 'POST',
|
||||
path: \`/api/v\${OpenAPI.VERSION}/parameters/\${parameterPath}\`,
|
||||
cookies: {
|
||||
'parameterCookie': parameterCookie,
|
||||
@ -4835,7 +4834,7 @@ export class ParametersService {
|
||||
_default?: string,
|
||||
): Promise<void> {
|
||||
const result = await __request({
|
||||
method: 'GET',
|
||||
method: 'POST',
|
||||
path: \`/api/v\${OpenAPI.VERSION}/parameters/\${parameterPath1}/\${parameterPath2}/\${parameterPath3}\`,
|
||||
cookies: {
|
||||
'PARAMETER-COOKIE': parameterCookie,
|
||||
|
||||
@ -57,4 +57,22 @@ describe('v3.fetch', () => {
|
||||
});
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
|
||||
it('formData', async () => {
|
||||
const result = await browser.evaluate(async () => {
|
||||
const { ParametersService } = window.api;
|
||||
return await ParametersService.callWithParameters(
|
||||
'valueHeader',
|
||||
'valueQuery',
|
||||
'valueForm',
|
||||
'valueCookie',
|
||||
'valuePath',
|
||||
{
|
||||
prop: 'valueBody'
|
||||
}
|
||||
);
|
||||
});
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -48,4 +48,19 @@ describe('v3.node', () => {
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
|
||||
it('formData', async () => {
|
||||
const { ParametersService } = require('./generated/v3/node/index.js');
|
||||
const result = await ParametersService.callWithParameters(
|
||||
'valueHeader',
|
||||
'valueQuery',
|
||||
'valueForm',
|
||||
'valueCookie',
|
||||
'valuePath',
|
||||
{
|
||||
prop: 'valueBody'
|
||||
}
|
||||
);
|
||||
console.log(result)
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
@ -55,7 +55,7 @@
|
||||
}
|
||||
},
|
||||
"/api/v{api-version}/parameters/{parameterPath}": {
|
||||
"get": {
|
||||
"post": {
|
||||
"tags": [
|
||||
"Parameters"
|
||||
],
|
||||
@ -106,7 +106,7 @@
|
||||
}
|
||||
},
|
||||
"/api/v{api-version}/parameters/{parameter.path.1}/{parameter-path-2}/{PARAMETER-PATH-3}": {
|
||||
"get": {
|
||||
"post": {
|
||||
"tags": [
|
||||
"Parameters"
|
||||
],
|
||||
|
||||
15879
test/spec/v3.json
15879
test/spec/v3.json
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user