From f438fc529f186b27f2070e3866b1e46660f7448e Mon Sep 17 00:00:00 2001 From: Kenneth Aasan Date: Tue, 15 Aug 2023 15:28:40 +0200 Subject: [PATCH] feat: adds format in options instead of using original input --- src/generators/java/JavaConstrainer.ts | 21 +++++++-------------- src/helpers/CommonModelToMetaModel.ts | 4 ++++ src/interpreter/Interpreter.ts | 3 +++ src/models/CommonModel.ts | 1 + src/models/ConstrainedMetaModel.ts | 1 + src/models/MetaModel.ts | 1 + 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/generators/java/JavaConstrainer.ts b/src/generators/java/JavaConstrainer.ts index 1148ecf899..2f543c3e3c 100644 --- a/src/generators/java/JavaConstrainer.ts +++ b/src/generators/java/JavaConstrainer.ts @@ -138,26 +138,19 @@ export const JavaDefaultTypeMapping: JavaTypeMapping = { return type; }, String({ constrainedModel }): string { - let type = 'String'; - const format = - constrainedModel.originalInput && - constrainedModel.originalInput['format']; - switch (format) { + switch (constrainedModel.options.format) { case 'date': - type = 'java.time.LocalDate'; - break; + return 'java.time.LocalDate'; case 'time': - type = 'java.time.OffsetTime'; - break; + return 'java.time.OffsetTime'; case 'dateTime': case 'date-time': - type = 'java.time.OffsetDateTime'; - break; + return 'java.time.OffsetDateTime'; case 'binary': - type = 'byte[]'; - break; + return 'byte[]'; + default: + return 'String'; } - return type; }, Boolean({ constrainedModel }): string { return constrainedModel.options.isNullable ? 'Boolean' : 'boolean'; diff --git a/src/helpers/CommonModelToMetaModel.ts b/src/helpers/CommonModelToMetaModel.ts index dbbf52e13e..9c09d9fa4f 100644 --- a/src/helpers/CommonModelToMetaModel.ts +++ b/src/helpers/CommonModelToMetaModel.ts @@ -40,6 +40,10 @@ function getMetaModelOptions(commonModel: CommonModel): MetaModelOptions { }; } + if (commonModel.format) { + options.format = commonModel.format; + } + return options; } diff --git a/src/interpreter/Interpreter.ts b/src/interpreter/Interpreter.ts index f3e3075056..157a6102f8 100644 --- a/src/interpreter/Interpreter.ts +++ b/src/interpreter/Interpreter.ts @@ -135,6 +135,9 @@ export class Interpreter { if (schema.required !== undefined) { model.required = schema.required; } + if (schema.format) { + model.format = schema.format; + } interpretPatternProperties(schema, model, this, interpreterOptions); interpretAdditionalItems(schema, model, this, interpreterOptions); diff --git a/src/models/CommonModel.ts b/src/models/CommonModel.ts index f0b96898bf..3414394e8c 100644 --- a/src/models/CommonModel.ts +++ b/src/models/CommonModel.ts @@ -22,6 +22,7 @@ export class CommonModel { enum?: any[]; const?: unknown; discriminator?: string; + format?: string; items?: CommonModel | CommonModel[]; properties?: { [key: string]: CommonModel }; additionalProperties?: CommonModel; diff --git a/src/models/ConstrainedMetaModel.ts b/src/models/ConstrainedMetaModel.ts index 6e81e91160..06218d9273 100644 --- a/src/models/ConstrainedMetaModel.ts +++ b/src/models/ConstrainedMetaModel.ts @@ -20,6 +20,7 @@ export class ConstrainedMetaModelOptions extends MetaModelOptions { const?: ConstrainedMetaModelOptionsConst; discriminator?: ConstrainedMetaModelOptionsDiscriminator; isNullable?: boolean = false; + format?: string; } export abstract class ConstrainedMetaModel extends MetaModel { diff --git a/src/models/MetaModel.ts b/src/models/MetaModel.ts index 29e9704a61..86e0854c3e 100644 --- a/src/models/MetaModel.ts +++ b/src/models/MetaModel.ts @@ -10,6 +10,7 @@ export class MetaModelOptions { const?: MetaModelOptionsConst; discriminator?: MetaModelOptionsDiscriminator; isNullable?: boolean = false; + format?: string; } export class MetaModel {