Skip to content

Commit

Permalink
Merge pull request #148 from Abraxas-365/fix/swagger-date-time
Browse files Browse the repository at this point in the history
fix: swagger date time
  • Loading branch information
marclave authored Sep 25, 2024
2 parents 337926c + 3d0e07f commit 18860ac
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
Binary file modified bun.lockb
Binary file not shown.
71 changes: 67 additions & 4 deletions src/swagger/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
import type { OpenAPIV3 } from 'openapi-types'
import { OpenAPIV3 } from 'openapi-types';

type DateTimeSchema = {
type: 'string';
format: 'date-time';
default?: string;
};

type SchemaObject = OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject;

function isSchemaObject(schema: SchemaObject): schema is OpenAPIV3.SchemaObject {
return 'type' in schema || 'properties' in schema || 'items' in schema;
}

function isDateTimeProperty(key: string, schema: OpenAPIV3.SchemaObject): boolean {
return (key === 'createdAt' || key === 'updatedAt') &&
'anyOf' in schema &&
Array.isArray(schema.anyOf);
}

function transformDateProperties(schema: SchemaObject): SchemaObject {
if (!isSchemaObject(schema) || typeof schema !== 'object' || schema === null) {
return schema;
}

const newSchema: OpenAPIV3.SchemaObject = { ...schema };

Object.entries(newSchema).forEach(([key, value]) => {
if (isSchemaObject(value)) {
if (isDateTimeProperty(key, value)) {
const dateTimeFormat = value.anyOf?.find((item): item is OpenAPIV3.SchemaObject =>
isSchemaObject(item) && item.format === 'date-time'
);
if (dateTimeFormat) {
const dateTimeSchema: DateTimeSchema = {
type: 'string',
format: 'date-time',
default: dateTimeFormat.default
};
(newSchema as Record<string, SchemaObject>)[key] = dateTimeSchema;
}
} else {
(newSchema as Record<string, SchemaObject>)[key] = transformDateProperties(value);
}
}
});

return newSchema;
}

export const SwaggerUIRender = (
info: OpenAPIV3.InfoObject,
Expand All @@ -11,7 +59,21 @@ export const SwaggerUIRender = (
},
stringifiedSwaggerOptions: string,
autoDarkMode?: boolean
) => `<!DOCTYPE html>
): string => {
const swaggerOptions: OpenAPIV3.Document = JSON.parse(stringifiedSwaggerOptions);

if (swaggerOptions.components && swaggerOptions.components.schemas) {
swaggerOptions.components.schemas = Object.fromEntries(
Object.entries(swaggerOptions.components.schemas).map(([key, schema]) => [
key,
transformDateProperties(schema)
])
);
}

const transformedStringifiedSwaggerOptions = JSON.stringify(swaggerOptions);

return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
Expand Down Expand Up @@ -57,8 +119,9 @@ export const SwaggerUIRender = (
<script src="https://unpkg.com/swagger-ui-dist@${version}/swagger-ui-bundle.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle(${stringifiedSwaggerOptions});
window.ui = SwaggerUIBundle(${transformedStringifiedSwaggerOptions});
};
</script>
</body>
</html>`
</html>`;
};

0 comments on commit 18860ac

Please sign in to comment.