Browse Source

* Corrections when writing file: write in 3.1 format

Michaël Van Canneyt 8 months ago
parent
commit
86da0baf02
1 changed files with 50 additions and 17 deletions
  1. 50 17
      packages/fcl-jsonschema/src/fpjson.schema.writer.pp

+ 50 - 17
packages/fcl-jsonschema/src/fpjson.schema.writer.pp

@@ -39,7 +39,8 @@ Type
     Procedure WriteProperty(const aName : TJSONStringType; aValue : TJSONSchemaVocabularyList);
     Procedure WriteProperty(const aName : TJSONStringType; aValue : TJSONSchemaVocabularyList);
     Procedure WriteProperty(const aName : TJSONStringType; aValue : TSchemaSimpleTypes);
     Procedure WriteProperty(const aName : TJSONStringType; aValue : TSchemaSimpleTypes);
     procedure WriteProperty(const aName: TJSONStringType; aValue: TJSONSchema);
     procedure WriteProperty(const aName: TJSONStringType; aValue: TJSONSchema);
-    procedure WriteProperty(const aName: TJSONStringType; aValue: TJSONSchemaList);
+    procedure WriteListAsObject(const aName: TJSONStringType; aValue: TJSONSchemaList);
+    procedure WriteProperty(const aName: TJSONStringType; aValue: TJSONSchemaList; AllowAsObject: Boolean = False);
     Procedure DoWriteSchema(aSchema : TJSONSchema);
     Procedure DoWriteSchema(aSchema : TJSONSchema);
   Public
   Public
     Procedure Writeschema(aSchema : TJSONSchema; aWriter : TAbstractJSONWriter);
     Procedure Writeschema(aSchema : TJSONSchema; aWriter : TAbstractJSONWriter);
@@ -79,20 +80,39 @@ begin
  end;
  end;
 end;
 end;
 
 
-procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: TJSONSchemaList);
+procedure TJSONSchemaWriter.WriteListAsObject(const aName: TJSONStringType; aValue: TJSONSchemaList);
+var
+  I : Integer;
+begin
+ FWriter.StartProperty(aName);
+ FWriter.StartObject;
+ for I:=0 to aValue.Count-1 do
+   begin
+   FWriter.NextElement;
+   FWriter.StartProperty(aValue.Schemas[i].Name);
+   DoWriteSchema(aValue.Schemas[i]);
+   end;
+ FWriter.EndObject;
+ FWriter.EndProperty;
+end;
+
+procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: TJSONSchemaList; AllowAsObject: Boolean);
 
 
 var
 var
   I : Integer;
   I : Integer;
 
 
 begin
 begin
   FWriter.StartProperty(aName);
   FWriter.StartProperty(aName);
-  FWriter.StartArray;
-  for I:=0 to aValue.Count-1 do
-    begin
-    FWriter.NextElement;
-    DoWriteSchema(aValue.Schemas[i]);
-    end;
-  FWriter.EndArray;
+  if AllowAsObject and (aValue.Count=1) then
+    DoWriteSchema(aValue.Schemas[0])
+  else
+    for I:=0 to aValue.Count-1 do
+      begin
+      FWriter.StartArray;
+      FWriter.NextElement;
+      DoWriteSchema(aValue.Schemas[i]);
+      FWriter.EndArray;
+      end;
   FWriter.EndProperty;
   FWriter.EndProperty;
 end;
 end;
 
 
@@ -148,19 +168,32 @@ end;
 procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: TSchemaSimpleTypes);
 procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: TSchemaSimpleTypes);
 
 
 var
 var
-  St : TSchemaSimpleType;
+  lCount : Integer;
+  St,stVal : TSchemaSimpleType;
 
 
 begin
 begin
+  lCount:=0;
+  for St in TSchemaSimpleType do
+    if st in aValue then
+      begin
+      inc(lCount);
+      stVal:=St;
+      end;
   With FWriter do
   With FWriter do
     begin
     begin
     StartProperty(aName);
     StartProperty(aName);
-    StartArray;
-    For ST in aValue do
+    if lCount=1 then
+      WriteValue(stVal.AsString)
+    else
       begin
       begin
-      NextElement;
-      WriteValue(ST.AsString);
+      StartArray;
+      For ST in aValue do
+        begin
+        NextElement;
+        WriteValue(ST.AsString);
+        end;
+      EndArray;
       end;
       end;
-    EndArray;
     EndProperty;
     EndProperty;
     end;
     end;
 end;
 end;
@@ -201,7 +234,7 @@ begin
         jskMinLength : W.WriteProperty(PropName,aSchema.Validations.MinLength);
         jskMinLength : W.WriteProperty(PropName,aSchema.Validations.MinLength);
         jskPattern : W.WriteProperty(PropName,aSchema.Validations.Pattern);
         jskPattern : W.WriteProperty(PropName,aSchema.Validations.Pattern);
         // jskAdditionalItems : WriteProperty(PropName,aSchema.Validations.AdditionalItems);
         // jskAdditionalItems : WriteProperty(PropName,aSchema.Validations.AdditionalItems);
-        jskItems : WriteProperty(PropName,aSchema.Items);
+        jskItems : WriteProperty(PropName,aSchema.Items,true);
         jskPrefixItems : WriteProperty(PropName,aSchema.PrefixItems);
         jskPrefixItems : WriteProperty(PropName,aSchema.PrefixItems);
         jskMaxItems : W.WriteProperty(PropName,aSchema.Validations.MaxItems);
         jskMaxItems : W.WriteProperty(PropName,aSchema.Validations.MaxItems);
         jskMinItems : W.WriteProperty(PropName,aSchema.Validations.MinItems);
         jskMinItems : W.WriteProperty(PropName,aSchema.Validations.MinItems);
@@ -212,7 +245,7 @@ begin
         jskMinContains : W.WriteProperty(PropName,aSchema.Validations.MinContains);
         jskMinContains : W.WriteProperty(PropName,aSchema.Validations.MinContains);
         jskRequired : W.WriteProperty(PropName,aSchema.Validations.Required);
         jskRequired : W.WriteProperty(PropName,aSchema.Validations.Required);
         jskAdditionalProperties : WriteProperty(PropName,aSchema.AdditionalProperties);
         jskAdditionalProperties : WriteProperty(PropName,aSchema.AdditionalProperties);
-        jskProperties : WriteProperty(PropName,aSchema.Properties);
+        jskProperties : WriteListAsObject(PropName,aSchema.Properties);
         jskPatternProperties: WriteProperty(PropName,aSchema.PatternProperties);
         jskPatternProperties: WriteProperty(PropName,aSchema.PatternProperties);
         jskPropertyNames : WriteProperty(PropName,aSchema.PropertyNames);
         jskPropertyNames : WriteProperty(PropName,aSchema.PropertyNames);
         jskDependentSchemas : WriteProperty(PropName,aSchema.DependentSchemas);
         jskDependentSchemas : WriteProperty(PropName,aSchema.DependentSchemas);