From b9c8f6fc1f482fca9ff0cad2b49ce8a3e22a56fe Mon Sep 17 00:00:00 2001 From: Lorenzo Scalese Date: Sat, 14 Oct 2023 13:16:08 +0200 Subject: [PATCH] #17 --- src/Grongier/OpenApi/DefinitionV3.cls | 66 ++++++++++++++++---- src/dc/openapi/client/Spec.cls | 2 - src/dc/openapi/server/ServerAppGenerator.cls | 1 - src/dc/openapi/suite/Generate.cls | 23 ++++++- 4 files changed, 74 insertions(+), 18 deletions(-) diff --git a/src/Grongier/OpenApi/DefinitionV3.cls b/src/Grongier/OpenApi/DefinitionV3.cls index 8a0a428..9bcc073 100644 --- a/src/Grongier/OpenApi/DefinitionV3.cls +++ b/src/Grongier/OpenApi/DefinitionV3.cls @@ -20,7 +20,12 @@ Property allowPropertyOverride As %Boolean [ InitialExpression = {$$$YES} ]; Property generateInLineModel As %Boolean [ InitialExpression = {$$$NO} ]; -Method %OnNew(specification As %DynamicObject, package As %String, super As %String, compile As %Boolean, flags As %String) As %Status +Method %OnNew( + specification As %DynamicObject, + package As %String, + super As %String, + compile As %Boolean, + flags As %String) As %Status { Set ..spec = specification, ..package = package @@ -82,7 +87,9 @@ Method GenerateClasses() As %Status Quit sc } -Method GenerateClassModel(schema As %DynamicObject, className As %String) As %Status +Method GenerateClassModel( + schema As %DynamicObject, + className As %String) As %Status { Set sc = $$$OK, requiredArray = [] @@ -104,7 +111,9 @@ Method GenerateClassModel(schema As %DynamicObject, className As %String) As %St Quit sc } -Method GenerateProperties(classDef As %Dictionary.ClassDefinition, properties As %DynamicObject) As %Status +Method GenerateProperties( + classDef As %Dictionary.ClassDefinition, + properties As %DynamicObject) As %Status { Set sc = $$$OK @@ -214,7 +223,9 @@ XMLParameters Quit } -Method ManageAllOf(classDef As %Dictionary.ClassDefinition, schema As %DynamicObject) As %Status +Method ManageAllOf( + classDef As %Dictionary.ClassDefinition, + schema As %DynamicObject) As %Status { Set sc = $$$OK, superClasses = "" @@ -243,7 +254,9 @@ Method ManageAllOf(classDef As %Dictionary.ClassDefinition, schema As %DynamicOb Quit sc } -Method ManageRequiredProperties(classDef As %Dictionary.ClassDefinition, requiredArray As %DynamicArray) As %Status +Method ManageRequiredProperties( + classDef As %Dictionary.ClassDefinition, + requiredArray As %DynamicArray) As %Status { Set sc = $$$OK @@ -256,7 +269,14 @@ Method ManageRequiredProperties(classDef As %Dictionary.ClassDefinition, require Set property = ..FindPropertyName(classDef, propertyName) Continue:property="" ; property not found ... Do ..log($Char(9) _ " - " _ propertyName) - Set property.Required = $$$YES + + If ##class(dc.openapi.common.Utils).GetTempData("model.property.norequired", $$$NO) { + ; https://github.com/lscalese/openapi-suite/issues/17 + Set property.Required = $$$NO + Set property.Description = property.Description _ $Char(10) _ "/// * This property is required." + } Else { + Set property.Required = $$$YES + } } } @@ -359,7 +379,10 @@ parametersObject Quit } -ClassMethod log(msg As %String, nl As %Boolean = {$$$YES}, dtOnNL As %Boolean = {$$$YES}) +ClassMethod log( + msg As %String, + nl As %Boolean = {$$$YES}, + dtOnNL As %Boolean = {$$$YES}) { Quit:$Get(%zverbose)=0 @@ -370,7 +393,9 @@ ClassMethod log(msg As %String, nl As %Boolean = {$$$YES}, dtOnNL As %Boolean = Quit } -ClassMethod GetObjectScriptType(oaType As %DynamicObject, format As %String = "") As %String +ClassMethod GetObjectScriptType( + oaType As %DynamicObject, + format As %String = "") As %String { Quit:oaType="" "%VarString" @@ -398,7 +423,10 @@ ClassMethod GetObjectScriptType(oaType As %DynamicObject, format As %String = "" Quit $Get(table(oaType, format), $Get(table(oaType), "%VarString" )) } -Method GetObjectByRef(reference As %String, Output name As %String, Output package As %String) As %DynamicObject +Method GetObjectByRef( + reference As %String, + Output name As %String, + Output package As %String) As %DynamicObject { ; required : Do ##class(dc.openapi.common.Utils).SetTempData("model.package", ..package) Quit ##class(dc.openapi.common.Utils).GetObjectByRef(..spec, reference, .name, .package) @@ -410,7 +438,9 @@ ClassMethod GetSpecForPackage(package As %String) As %DynamicObject [ CodeMode = ##class(dc.openapi.common.Utils).GetSpecForPackage(package) } -ClassMethod CreateClassDefInstance(className As %String, Output classDef As %Dictionary.ClassDefinition = "") As %Dictionary.ClassDefinition +ClassMethod CreateClassDefInstance( + className As %String, + Output classDef As %Dictionary.ClassDefinition = "") As %Dictionary.ClassDefinition { If $$$defClassDefined(className) { Set sc = $$Delete^%apiOBJ(className,"-d") @@ -429,7 +459,11 @@ ClassMethod CreateClassDefInstance(className As %String, Output classDef As %Dic Quit $$$OK } -Method CommonSchemaProcess(classDef As %Dictionary.ClassDefinition, propDef As %Dictionary.PropertyDefinition, schema As %DynamicObject, name As %String) As %Status +Method CommonSchemaProcess( + classDef As %Dictionary.ClassDefinition, + propDef As %Dictionary.PropertyDefinition, + schema As %DynamicObject, + name As %String) As %Status { Set sc = $$$OK @@ -482,7 +516,9 @@ Method CommonSchemaProcess(classDef As %Dictionary.ClassDefinition, propDef As % Quit sc } -ClassMethod FindPropertyName(classDefinition As %Dictionary.ClassDefinition, propertyName As %String) As %Dictionary.PropertyDefinition +ClassMethod FindPropertyName( + classDefinition As %Dictionary.ClassDefinition, + propertyName As %String) As %Dictionary.PropertyDefinition { #dim property As %Dictionary.PropertyDefinition Set key = "" @@ -498,7 +534,11 @@ ClassMethod FindPropertyName(classDefinition As %Dictionary.ClassDefinition, pro Return "" } -ClassMethod GetClsNameForAnonymObject(path As %String = "", method As %String = "", name As %String = "", contentType As %String = "") As %String +ClassMethod GetClsNameForAnonymObject( + path As %String = "", + method As %String = "", + name As %String = "", + contentType As %String = "") As %String { Quit "Z"_name _ $ZCRC(path_method_name_contentType,0) } diff --git a/src/dc/openapi/client/Spec.cls b/src/dc/openapi/client/Spec.cls index d02c2e8..3e5d1ad 100644 --- a/src/dc/openapi/client/Spec.cls +++ b/src/dc/openapi/client/Spec.cls @@ -23,8 +23,6 @@ ClassMethod generateApp( { Set sc = $$$OK - Do ##class(dc.openapi.common.Utils).KillTempData() ; Kill tempory data of previous code generation - Set spec = ##class(dc.openapi.common.SpecificationLoader).AbstractSpecLoader(spec, .sc, '$Get(features("noExtRef"),0)) Quit:$$$ISERR(sc) sc diff --git a/src/dc/openapi/server/ServerAppGenerator.cls b/src/dc/openapi/server/ServerAppGenerator.cls index 5ed2d99..837d6a6 100644 --- a/src/dc/openapi/server/ServerAppGenerator.cls +++ b/src/dc/openapi/server/ServerAppGenerator.cls @@ -14,7 +14,6 @@ ClassMethod Generate( { $$$zlog("Generate server-side application " _ application) - Do ##class(dc.openapi.common.Utils).KillTempData() ; Kill tempory data of previous code generation Do ##class(community.utils.SSLClient).CreateSSLConfiguration("DefaultSSL") Set spec = ##class(dc.openapi.common.SpecificationLoader).AbstractSpecLoader(specification, .sc, '$Get(features("noExtRef"),0)) diff --git a/src/dc/openapi/suite/Generate.cls b/src/dc/openapi/suite/Generate.cls index e9b8719..2a2d896 100644 --- a/src/dc/openapi/suite/Generate.cls +++ b/src/dc/openapi/suite/Generate.cls @@ -11,7 +11,10 @@ ClassMethod Client( { Set %zverbose = 1 Set Features("simpleHttpClientOnly") = 1 - + + Do ##class(dc.openapi.common.Utils).KillTempData() ; Kill tempory data of previous code generation + Do ..HandleCommonFeatures(.Features) + ; If we have an external ref mapping, set noExtRef to 1 ; It avoid validator.swagger.io to resolve external references. If $IsObject(ExternalRefMap), '$Data(Features("noExtRef")) Set Features("noExtRef") = 1 @@ -28,6 +31,9 @@ ClassMethod ProductionClient( Set %zverbose = 1 Set Features("simpleHttpClientOnly") = 0 + Do ##class(dc.openapi.common.Utils).KillTempData() ; Kill tempory data of previous code generation + Do ..HandleCommonFeatures(.Features) + ; If we have an external ref mapping, set noExtRef to 1 ; It avoid validator.swagger.io to resolve external references. If $IsObject(ExternalRefMap), '$Data(Features("noExtRef")) Set Features("noExtRef") = 1 @@ -42,7 +48,10 @@ ClassMethod Server( ExternalRefMap As %DynamicObject = "") As %Status { Set %zverbose = 1 - + + Do ##class(dc.openapi.common.Utils).KillTempData() ; Kill tempory data of previous code generation + Do ..HandleCommonFeatures(.Features) + ; If we have an external ref mapping, set noExtRef to 1 ; It avoid validator.swagger.io to resolve external references. If $IsObject(ExternalRefMap), '$Data(Features("noExtRef")) Set Features("noExtRef") = 1 @@ -58,6 +67,8 @@ ClassMethod Models( { Set %zverbose = 1 + Do ##class(dc.openapi.common.Utils).KillTempData() ; Kill tempory data of previous code generation + Set Features("simpleHttpClientOnly") = 1 set Features("modelOnly") = 1 @@ -68,4 +79,12 @@ ClassMethod Models( Quit ##class(dc.openapi.client.Spec).generateApp(PackageName, Specfication, .Features, ExternalRefMap) } +ClassMethod HandleCommonFeatures(ByRef Features As %Binary) As %Status [ Internal, Private ] +{ + Quit:'$Data(Features) $$$OK + Set key = "model.property.norequired" + Do:$Data(Features(key),value) ##class(dc.openapi.common.Utils).SetTempData(key, value) + Quit $$$OK +} + }