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

feat: add display options and fix array bug #2103

Merged
merged 3 commits into from
Jul 10, 2022
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
19 changes: 19 additions & 0 deletions packages/swagger/src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,23 @@ export interface SwaggerOptions {
* 可以使用 1-xxx、2-xxx、3-xxx 来定义 tag
*/
tagSortable?: boolean;
/**
* UI 展示中需要用到的配置
* 可以参考 https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md#display
*/
displayOptions?: {
deepLinking?: boolean;
displayOperationId?: boolean;
defaultModelsExpandDepth?: number;
defaultModelExpandDepth?: number;
defaultModelRendering?: 'example' | 'model';
displayRequestDuration?: boolean;
docExpansion?: 'list' | 'full' | 'none';
filter?: boolean | string;
maxDisplayedTags?: number;
showExtensions?: boolean;
showCommonExtensions?: boolean;
useUnsafeMarkdown?: boolean;
tryItOutEnabled?: boolean;
};
}
15 changes: 12 additions & 3 deletions packages/swagger/src/swaggerExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ export class SwaggerExplorer {
if (param.in === 'query') {
p.allowEmptyValue = param.allowEmptyValue || false;
}
if (param.example) {
if (typeof param.example !== undefined) {
p.example = param.example;
}
if (param.examples) {
Expand Down Expand Up @@ -559,13 +559,22 @@ export class SwaggerExplorer {
}

if (param.isArray) {
let ref;
if (p?.schema?.$ref) {
ref = p.schema.$ref;
}

p.schema = {
type: 'array',
items: {
type: p?.schema?.$ref ?? convertSchemaType(param.type),
format: param.format,
},
};
if (ref) {
p.schema.items.$ref = ref;
} else {
p.schema.items.type = convertSchemaType(param.type);
}
} else {
if (!p.schema) {
p.schema = {
Expand Down Expand Up @@ -622,7 +631,7 @@ export class SwaggerExplorer {
Object.keys(props).forEach(key => {
const metadata = props[key].metadata;

if (metadata?.example) {
if (typeof metadata?.example !== undefined) {
if (!tt.example) {
tt.example = {};
}
Expand Down
11 changes: 9 additions & 2 deletions packages/swagger/src/swaggerMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '@midwayjs/decorator';
import { readFileSync } from 'fs';
import { join, extname } from 'path';
import type { SwaggerOptions } from './interfaces';
import { SwaggerExplorer } from './swaggerExplorer';

@Provide()
Expand All @@ -24,7 +25,7 @@ export class SwaggerMiddleware
implements IMiddleware<IMidwayContext, NextFunction, unknown>
{
@Config('swagger')
private swaggerConfig: any;
private swaggerConfig: SwaggerOptions;

private swaggerUiAssetPath: string;

Expand Down Expand Up @@ -126,9 +127,15 @@ export class SwaggerMiddleware
}

replaceInfo(content: string): string {
let str = `location.href.replace('${this.swaggerConfig.swaggerPath}/index.html', '${this.swaggerConfig.swaggerPath}/index.json'),\n validatorUrl: null,`;
if (this.swaggerConfig.displayOptions) {
Object.keys(this.swaggerConfig.displayOptions).forEach(key => {
str += `\n${key}: ${this.swaggerConfig.displayOptions[key]},`;
});
}
return content.replace(
'"https://petstore.swagger.io/v2/swagger.json",',
`location.href.replace('${this.swaggerConfig.swaggerPath}/index.html', '${this.swaggerConfig.swaggerPath}/index.json'),\n validatorUrl: null,`
str
);
}

Expand Down
19 changes: 19 additions & 0 deletions site/docs/extensions/swagger.md
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,25 @@ export interface SwaggerOptions {
* 可以使用 1-xxx、2-xxx、3-xxx 来定义 tag
*/
tagSortable?: boolean;
/**
* UI 展示中需要用到的配置
* 可以参考 https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md#display
*/
displayOptions?: {
deepLinking?: boolean;
displayOperationId?: boolean;
defaultModelsExpandDepth?: number;
defaultModelExpandDepth?: number;
defaultModelRendering?: 'example' | 'model';
displayRequestDuration?: boolean;
docExpansion?: 'list' | 'full' | 'none';
filter?: boolean | string;
maxDisplayedTags?: number;
showExtensions?: boolean;
showCommonExtensions?: boolean;
useUnsafeMarkdown?: boolean;
tryItOutEnabled?: boolean;
};
}
/**
* 继承自 https://swagger.io/specification/#security-scheme-object
Expand Down