Skip to content

Commit

Permalink
fix: patch local copy of open api document
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Aug 10, 2023
1 parent 3ddc224 commit 8af173c
Showing 1 changed file with 36 additions and 43 deletions.
79 changes: 36 additions & 43 deletions lib/swagger-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ export class SwaggerModule {
}

if (options.swaggerOptions.patchDocumentOnRequest) {
document = options.swaggerOptions.patchDocumentOnRequest(
req,
res,
document
const documentToSerialize =
options.swaggerOptions.patchDocumentOnRequest(req, res, document);
const swaggerInitJsPerRequest = buildSwaggerInitJS(
documentToSerialize,
options.swaggerOptions
);
return res.send(swaggerInitJsPerRequest);
}

if (!swaggerInitJS) {
Expand Down Expand Up @@ -145,11 +147,13 @@ export class SwaggerModule {
}

if (options.swaggerOptions.patchDocumentOnRequest) {
document = options.swaggerOptions.patchDocumentOnRequest(
req,
res,
document
const documentToSerialize =
options.swaggerOptions.patchDocumentOnRequest(req, res, document);
const swaggerInitJsPerRequest = buildSwaggerInitJS(
documentToSerialize,
options.swaggerOptions
);
return res.send(swaggerInitJsPerRequest);
}

if (!swaggerInitJS) {
Expand Down Expand Up @@ -177,11 +181,14 @@ export class SwaggerModule {
}

if (options.swaggerOptions.patchDocumentOnRequest) {
document = options.swaggerOptions.patchDocumentOnRequest(
req,
res,
document
const documentToSerialize =
options.swaggerOptions.patchDocumentOnRequest(req, res, document);
const htmlPerRequest = buildSwaggerHTML(
baseUrlForSwaggerUI,
documentToSerialize,
options.swaggerOptions
);
return res.send(htmlPerRequest);
}

if (!html) {
Expand All @@ -205,11 +212,14 @@ export class SwaggerModule {
}

if (options.swaggerOptions.patchDocumentOnRequest) {
document = options.swaggerOptions.patchDocumentOnRequest(
req,
res,
document
const documentToSerialize =
options.swaggerOptions.patchDocumentOnRequest(req, res, document);
const htmlPerRequest = buildSwaggerHTML(
baseUrlForSwaggerUI,
documentToSerialize,
options.swaggerOptions
);
return res.send(htmlPerRequest);
}

if (!html) {
Expand All @@ -219,7 +229,6 @@ export class SwaggerModule {
options.swaggerOptions
);
}

res.send(html);
});
} catch (err) {
Expand All @@ -231,29 +240,18 @@ export class SwaggerModule {
*/
}

let yamlDocument: string;
let jsonDocument: string;

httpAdapter.get(normalizeRelPath(options.jsonDocumentUrl), (req, res) => {
res.type('application/json');

if (!document) {
document = lazyBuildDocument();
}

if (options.swaggerOptions.patchDocumentOnRequest) {
document = options.swaggerOptions.patchDocumentOnRequest(
req,
res,
document
);
}

if (!jsonDocument) {
jsonDocument = JSON.stringify(document);
}
const documentToSerialize = options.swaggerOptions.patchDocumentOnRequest
? options.swaggerOptions.patchDocumentOnRequest(req, res, document)
: document;

res.send(jsonDocument);
res.send(JSON.stringify(documentToSerialize));
});

httpAdapter.get(normalizeRelPath(options.yamlDocumentUrl), (req, res) => {
Expand All @@ -263,18 +261,13 @@ export class SwaggerModule {
document = lazyBuildDocument();
}

if (options.swaggerOptions.patchDocumentOnRequest) {
document = options.swaggerOptions.patchDocumentOnRequest(
req,
res,
document
);
}

if (!yamlDocument) {
yamlDocument = jsyaml.dump(document, { skipInvalid: true });
}
const documentToSerialize = options.swaggerOptions.patchDocumentOnRequest
? options.swaggerOptions.patchDocumentOnRequest(req, res, document)
: document;

const yamlDocument = jsyaml.dump(documentToSerialize, {
skipInvalid: true
});
res.send(yamlDocument);
});
}
Expand Down

0 comments on commit 8af173c

Please sign in to comment.