• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RobinTail / express-zod-api / 3733855692

pending completion
3733855692

Pull #752

github

GitHub
Merge 82de5eda7 into f8fe2a03a
Pull Request #752: Bump @typescript-eslint/parser from 5.46.1 to 5.47.0

413 of 440 branches covered (93.86%)

969 of 969 relevant lines covered (100.0%)

325.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.51
/src/open-api.ts
1
import {
176✔
2
  OpenApiBuilder,
3
  OperationObject,
4
  SecuritySchemeObject,
5
  SecuritySchemeType,
6
} from "openapi3-ts";
7
import { defaultInputSources } from "./common-helpers";
176✔
8
import { CommonConfig } from "./config-type";
9
import { mapLogicalContainer } from "./logical-container";
176✔
10
import { Method } from "./method";
11
import {
176✔
12
  depictRequest,
13
  depictRequestParams,
14
  depictResponse,
15
  depictSecurity,
16
  depictSecurityRefs,
17
  depictTags,
18
  ensureShortDescription,
19
  reformatParamsInPath,
20
} from "./open-api-helpers";
21
import { Routing, RoutingCycleParams, routingCycle } from "./routing";
176✔
22

23
interface GeneratorParams {
24
  title: string;
25
  version: string;
26
  serverUrl: string;
27
  routing: Routing;
28
  config: CommonConfig;
29
  /** @default Successful response */
30
  successfulResponseDescription?: string;
31
  /** @default Error response */
32
  errorResponseDescription?: string;
33
  /** @default true */
34
  hasSummaryFromDescription?: boolean;
35
}
36

37
export class OpenAPI extends OpenApiBuilder {
176✔
38
  protected lastSecuritySchemaIds: Partial<Record<SecuritySchemeType, number>> =
264✔
39
    {};
40

41
  protected ensureUniqSecuritySchemaName(subject: SecuritySchemeObject) {
42
    for (const name in this.rootDoc.components?.securitySchemes || {}) {
48!
43
      if (
48✔
44
        JSON.stringify(subject) ===
45
        JSON.stringify(this.rootDoc.components?.securitySchemes?.[name])
46
      ) {
47
        return name;
8✔
48
      }
49
    }
50
    this.lastSecuritySchemaIds[subject.type] =
40✔
51
      (this.lastSecuritySchemaIds?.[subject.type] || 0) + 1;
72✔
52
    return `${subject.type.toUpperCase()}_${
40✔
53
      this.lastSecuritySchemaIds[subject.type]
54
    }`;
55
  }
56

57
  public constructor({
58
    routing,
59
    config,
60
    title,
61
    version,
62
    serverUrl,
63
    successfulResponseDescription = "Successful response",
264✔
64
    errorResponseDescription = "Error response",
264✔
65
    hasSummaryFromDescription = true,
264✔
66
  }: GeneratorParams) {
67
    super();
264✔
68
    this.addInfo({ title, version }).addServer({ url: serverUrl });
264✔
69
    const endpointCb: RoutingCycleParams["endpointCb"] = (
264✔
70
      endpoint,
71
      path,
72
      _method
73
    ) => {
74
      const method = _method as Method;
312✔
75
      const commonParams = { path, method, endpoint };
312✔
76
      const [shortDesc, longDesc] = (["short", "long"] as const).map(
312✔
77
        endpoint.getDescription.bind(endpoint)
78
      );
79
      const inputSources =
80
        config.inputSources?.[method] || defaultInputSources[method];
312✔
81
      const depictedParams = depictRequestParams({
312✔
82
        ...commonParams,
83
        inputSources,
84
      });
85
      const operation: OperationObject = {
312✔
86
        responses: {
87
          "200": depictResponse({
88
            ...commonParams,
89
            description: successfulResponseDescription,
90
            isPositive: true,
91
          }),
92
          "400": depictResponse({
93
            ...commonParams,
94
            description: errorResponseDescription,
95
            isPositive: false,
96
          }),
97
        },
98
      };
99
      if (longDesc) {
312✔
100
        operation.description = longDesc;
24✔
101
        if (hasSummaryFromDescription && shortDesc === undefined) {
24✔
102
          operation.summary = ensureShortDescription(longDesc);
16✔
103
        }
104
      }
105
      if (shortDesc) {
312✔
106
        operation.summary = ensureShortDescription(shortDesc);
24✔
107
      }
108
      if (endpoint.getTags().length > 0) {
312✔
109
        operation.tags = endpoint.getTags();
40✔
110
      }
111
      if (depictedParams.length > 0) {
312✔
112
        operation.parameters = depictedParams;
120✔
113
      }
114
      if (inputSources.includes("body")) {
312✔
115
        operation.requestBody = depictRequest(commonParams);
208✔
116
      }
117
      const securityRefs = depictSecurityRefs(
248✔
118
        mapLogicalContainer(
119
          depictSecurity(endpoint.getSecurity()),
120
          (securitySchema) => {
121
            const name = this.ensureUniqSecuritySchemaName(securitySchema);
48✔
122
            const scopes = ["oauth2", "openIdConnect"].includes(
48✔
123
              securitySchema.type
124
            )
125
              ? endpoint.getScopes()
48✔
126
              : [];
127
            this.addSecurityScheme(name, securitySchema);
48✔
128
            return { name, scopes };
48✔
129
          }
130
        )
131
      );
132
      if (securityRefs.length > 0) {
248✔
133
        operation.security = securityRefs;
24✔
134
      }
135
      const swaggerCompatiblePath = reformatParamsInPath(path);
248✔
136
      this.addPath(swaggerCompatiblePath, {
248✔
137
        [method]: operation,
138
      });
139
    };
140
    routingCycle({ routing, endpointCb });
264✔
141
    this.rootDoc.tags = config.tags ? depictTags(config.tags) : [];
200✔
142
  }
143
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc