Skip to content
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
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ Each protocol has its own limitations, corner cases, and features; thus, each ha




1 change: 1 addition & 0 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,4 @@ Prefix that follows specification is not enough though. Remember that the title




6 changes: 3 additions & 3 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ $ npm install -g @the-codegen-project/cli
$ codegen COMMAND
running command...
$ codegen (--version)
@the-codegen-project/cli/0.33.1 linux-x64 node-v18.20.7
@the-codegen-project/cli/0.34.0 linux-x64 node-v18.20.8
$ codegen --help [COMMAND]
USAGE
$ codegen COMMAND
Expand Down Expand Up @@ -81,7 +81,7 @@ DESCRIPTION
Generate code based on your configuration, use `init` to get started.
```

_See code: [src/commands/generate.ts](https://github.com/the-codegen-project/cli/blob/v0.33.1/src/commands/generate.ts)_
_See code: [src/commands/generate.ts](https://github.com/the-codegen-project/cli/blob/v0.34.0/src/commands/generate.ts)_

## `codegen help [COMMAND]`

Expand Down Expand Up @@ -139,7 +139,7 @@ DESCRIPTION
Initialize The Codegen Project in your project
```

_See code: [src/commands/init.ts](https://github.com/the-codegen-project/cli/blob/v0.33.1/src/commands/init.ts)_
_See code: [src/commands/init.ts](https://github.com/the-codegen-project/cli/blob/v0.34.0/src/commands/init.ts)_

## `codegen version`

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@the-codegen-project/cli",
"description": "CLI to work with code generation in any environment",
"version": "0.33.1",
"version": "0.34.0",
"bin": {
"codegen": "./bin/run.mjs"
},
Expand Down
5 changes: 5 additions & 0 deletions schemas/configuration-schema-0.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
"default": true,
"description": "By default we assume that the models might be transpiled to JS, therefore JS restrictions will be applied by default."
},
"includeValidation": {
"type": "boolean",
"default": true,
"description": "By default we assume that the models will be used to also validate incoming data."
},
"rawPropertyNames": {
"type": "boolean",
"default": false,
Expand Down
5 changes: 4 additions & 1 deletion src/codegen/generators/typescript/channels/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ export async function generateTypeScriptChannelsForAsyncAPI(
payloads: TypeScriptPayloadRenderType,
protocolsToUse: SupportedProtocols[],
protocolCodeFunctions: Record<string, string[]>,
externalProtocolFunctionInformation: Record<string, TypeScriptChannelRenderedFunctionType[]>,
externalProtocolFunctionInformation: Record<
string,
TypeScriptChannelRenderedFunctionType[]
>,
dependencies: string[]
): Promise<void> {
const {asyncapiDocument} = validateAsyncapiContext(context);
Expand Down
5 changes: 4 additions & 1 deletion src/codegen/generators/typescript/channels/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ async function finalizeGeneration(
context: TypeScriptChannelsContext,
dependencies: string[],
protocolCodeFunctions: Record<string, string[]>,
externalProtocolFunctionInformation: Record<string, TypeScriptChannelRenderedFunctionType[]>,
externalProtocolFunctionInformation: Record<
string,
TypeScriptChannelRenderedFunctionType[]
>,
parameters: TypeScriptParameterRenderType,
payloads: TypeScriptPayloadRenderType
): Promise<TypeScriptChannelRenderType> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export async function generateAmqpChannels(
context: TypeScriptChannelsGeneratorContext,
channel: ChannelInterface,
protocolCodeFunctions: Record<string, string[]>,
externalProtocolFunctionInformation: Record<string, TypeScriptChannelRenderedFunctionType[]>,
externalProtocolFunctionInformation: Record<
string,
TypeScriptChannelRenderedFunctionType[]
>,
dependencies: string[]
) {
const {parameter, topic} = context;
Expand Down Expand Up @@ -56,7 +59,10 @@ export async function generateAmqpChannels(
function addRendersToExternal(
renders: SingleFunctionRenderType[],
protocolCodeFunctions: Record<string, string[]>,
externalProtocolFunctionInformation: Record<string, TypeScriptChannelRenderedFunctionType[]>,
externalProtocolFunctionInformation: Record<
string,
TypeScriptChannelRenderedFunctionType[]
>,
dependencies: string[],
parameter?: ConstrainedObjectModel
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export async function generateEventSourceChannels(
context: TypeScriptChannelsGeneratorContext,
channel: ChannelInterface,
protocolCodeFunctions: Record<string, string[]>,
externalProtocolFunctionInformation: Record<string, TypeScriptChannelRenderedFunctionType[]>,
externalProtocolFunctionInformation: Record<
string,
TypeScriptChannelRenderedFunctionType[]
>,
dependencies: string[]
) {
const {parameter, topic} = context;
Expand Down Expand Up @@ -55,7 +58,10 @@ export async function generateEventSourceChannels(
function addRendersToExternal(
renders: SingleFunctionRenderType[],
protocolCodeFunctions: Record<string, string[]>,
externalProtocolFunctionInformation: Record<string, TypeScriptChannelRenderedFunctionType[]>,
externalProtocolFunctionInformation: Record<
string,
TypeScriptChannelRenderedFunctionType[]
>,
dependencies: string[],
parameter?: ConstrainedObjectModel
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export async function generateKafkaChannels(
context: TypeScriptChannelsGeneratorContext,
channel: ChannelInterface,
protocolCodeFunctions: Record<string, string[]>,
externalProtocolFunctionInformation: Record<string, TypeScriptChannelRenderedFunctionType[]>,
externalProtocolFunctionInformation: Record<
string,
TypeScriptChannelRenderedFunctionType[]
>,
dependencies: string[]
) {
const {parameter, topic} = context;
Expand Down Expand Up @@ -57,7 +60,10 @@ export async function generateKafkaChannels(
function addRendersToExternal(
renders: SingleFunctionRenderType[],
protocolCodeFunctions: Record<string, string[]>,
externalProtocolFunctionInformation: Record<string, TypeScriptChannelRenderedFunctionType[]>,
externalProtocolFunctionInformation: Record<
string,
TypeScriptChannelRenderedFunctionType[]
>,
dependencies: string[],
parameter?: ConstrainedObjectModel
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export async function generateMqttChannels(
context: TypeScriptChannelsGeneratorContext,
channel: ChannelInterface,
protocolCodeFunctions: Record<string, string[]>,
externalProtocolFunctionInformation: Record<string, TypeScriptChannelRenderedFunctionType[]>,
externalProtocolFunctionInformation: Record<
string,
TypeScriptChannelRenderedFunctionType[]
>,
dependencies: string[]
) {
const {generator, parameter, topic} = context;
Expand Down Expand Up @@ -53,7 +56,10 @@ export async function generateMqttChannels(
function addRendersToExternal(
renders: SingleFunctionRenderType[],
protocolCodeFunctions: Record<string, string[]>,
externalProtocolFunctionInformation: Record<string, TypeScriptChannelRenderedFunctionType[]>,
externalProtocolFunctionInformation: Record<
string,
TypeScriptChannelRenderedFunctionType[]
>,
dependencies: string[],
parameter?: ConstrainedObjectModel
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
/* eslint-disable sonarjs/cognitive-complexity */
/* eslint-disable security/detect-object-injection */
import { RenderRegularParameters, ChannelFunctionTypes, TypeScriptChannelsGeneratorContext, TypeScriptChannelRenderedFunctionType} from '../../types';
import {findNameFromOperation, findOperationId, findReplyId} from '../../../../../utils';
import {
RenderRegularParameters,
ChannelFunctionTypes,
TypeScriptChannelsGeneratorContext,
TypeScriptChannelRenderedFunctionType
} from '../../types';
import {
findNameFromOperation,
findOperationId,
findReplyId
} from '../../../../../utils';
import {getMessageTypeAndModule} from '../../utils';
import {
getFunctionTypeMappingFromAsyncAPI,
Expand Down Expand Up @@ -32,7 +41,10 @@ export async function generateNatsChannels(
context: TypeScriptChannelsGeneratorContext,
channel: ChannelInterface,
protocolCodeFunctions: Record<string, string[]>,
externalProtocolFunctionInformation: Record<string, TypeScriptChannelRenderedFunctionType[]>,
externalProtocolFunctionInformation: Record<
string,
TypeScriptChannelRenderedFunctionType[]
>,
dependencies: string[]
) {
const {parameter, topic} = context;
Expand Down Expand Up @@ -65,7 +77,10 @@ export async function generateNatsChannels(
function addRendersToExternal(
renders: SingleFunctionRenderType[],
protocolCodeFunctions: Record<string, string[]>,
externalProtocolFunctionInformation: Record<string, TypeScriptChannelRenderedFunctionType[]>,
externalProtocolFunctionInformation: Record<
string,
TypeScriptChannelRenderedFunctionType[]
>,
dependencies: string[],
parameter?: ConstrainedObjectModel
) {
Expand Down
42 changes: 28 additions & 14 deletions src/codegen/generators/typescript/payloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,15 @@ return payload.marshal();
return JSON.stringify(payload);
}`;
}
function renderUnionUnmarshal(model: ConstrainedUnionModel, renderer: TypeScriptRenderer) {
function renderUnionUnmarshal(
model: ConstrainedUnionModel,
renderer: TypeScriptRenderer
) {
const discriminatorChecks = model.union.map((model) => {
return findDiscriminatorChecks(model, renderer);
});
const hasObjValues =
discriminatorChecks.filter((value) => value?.objCheck).length >=
1;
discriminatorChecks.filter((value) => value?.objCheck).length >= 1;
return `export function unmarshal(json: any): ${model.name} {
${
hasObjValues
Expand All @@ -210,32 +212,38 @@ function renderUnionUnmarshal(model: ConstrainedUnionModel, renderer: TypeScript
/**
* Safe stringify that removes x- properties and circular references by assuming true
*/
export function safeStringify (value: any): string {
export function safeStringify(value: any): string {
const stack: any[] = [];
let r = 0;
let r = 0;
const replacer = (key: string, value: any) => {
// remove extension properties
if (key.startsWith('x-')) { return; }
if (key.startsWith('x-')) {
return;
}

switch (typeof value) {
case "function":
case 'function':
return 'true';
// is this a primitive value ?
case "boolean":
case "number":
case "string":
case 'boolean':
case 'number':
case 'string':
// primitives cannot have properties
// so these are safe to parse
return value;
default: {
// only null does not need to be stored
// for all objects check recursion first
// hopefully 255 calls are enough ...
if (!value || 255 < ++r) {return 'true';}
if (!value || 255 < ++r) {
return 'true';
}

const i = stack.indexOf(value);
// all objects not already parsed
if (i < 0) {return stack.push(value) && value;}
if (i < 0) {
return stack.push(value) && value;
}
// all others are duplicated or cyclic
// let them through
return 'true';
Expand Down Expand Up @@ -270,8 +278,14 @@ export async function generateTypescriptPayload(
if (!generator.includeValidation) {
return content;
}
renderer.dependencyManager.addTypeScriptDependency('{Ajv, Options as AjvOptions, ValidateFunction}', 'ajv');
renderer.dependencyManager.addTypeScriptDependency('addFormats', 'ajv-formats');
renderer.dependencyManager.addTypeScriptDependency(
'{Ajv, Options as AjvOptions, ValidateFunction}',
'ajv'
);
renderer.dependencyManager.addTypeScriptDependency(
'addFormats',
'ajv-formats'
);
return `${content}
public theCodeGenSchema = ${safeStringify(model.originalInput)};
public validate(context : {data: any, ajvInstance?: Ajv, ajvOptions?: AjvOptions}): { valid: boolean; validateFunction: ValidateFunction; } {
Expand Down