-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSwag.Doc.Path.Operation.Response.pas
148 lines (127 loc) · 5.04 KB
/
Swag.Doc.Path.Operation.Response.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
{******************************************************************************}
{ }
{ Delphi SwagDoc Library }
{ Copyright (c) 2018 Marcelo Jaloto }
{ https://github.com/marcelojaloto/SwagDoc }
{ }
{******************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{******************************************************************************}
unit Swag.Doc.Path.Operation.Response;
interface
uses
System.Generics.Collections,
System.JSON,
Swag.Common.Types,
Swag.Doc.Path.Operation.ResponseHeaders,
Swag.Doc.Definition;
type
TSwagResponse = class(TObject)
private
fStatusCode: TSwagStatusCode;
fSchema: TSwagDefinition;
fHeaders: TObjectList<TSwagHeaders>;
fDescription: string;
fExamples: TDictionary<TSwagJsonExampleDescription, TJSONObject>;
protected
function GenerateExamplesJsonObject: TJSONObject;
public
constructor Create; reintroduce;
destructor Destroy; override;
function GenerateJsonObject: TJSONObject;
procedure Load(pJson : TJSONObject);
property StatusCode: TSwagStatusCode read fStatusCode write fStatusCode;
property Description: string read fDescription write fDescription;
property Schema: TSwagDefinition read fSchema;
property Headers : TObjectList<TSwagHeaders> read fHeaders;
property Examples: TDictionary<TSwagJsonExampleDescription, TJSONObject> read fExamples;
end;
implementation
uses
System.SysUtils;
const
c_SwagResponseDescription = 'description';
c_SwagResponseSchema = 'schema';
c_SwagResponseExamples = 'examples';
c_SwagResponseHeaders = 'headers';
{ TSwagResponse }
constructor TSwagResponse.Create;
begin
inherited Create;
fExamples := TDictionary<TSwagJsonExampleDescription, TJSONObject>.Create;
fSchema := TSwagDefinition.Create;
fHeaders := TObjectList<TSwagHeaders>.Create;
end;
destructor TSwagResponse.Destroy;
begin
FreeAndNil(fExamples);
FreeAndNil(fSchema);
FreeAndNil(fHeaders);
inherited Destroy;
end;
function TSwagResponse.GenerateExamplesJsonObject: TJSONObject;
var
vKey: TSwagJsonExampleDescription;
begin
Result := TJsonObject.Create;
for vKey in fExamples.Keys do
Result.AddPair(vKey, fExamples.Items[vKey]);
end;
function TSwagResponse.GenerateJsonObject: TJSONObject;
var
vJsonObject: TJsonObject;
vIndex: Integer;
vJsonHeaders: TJSONObject;
begin
vJsonObject := TJsonObject.Create;
vJsonObject.AddPair(c_SwagResponseDescription, fDescription);
if (not fSchema.Name.IsEmpty) then
vJsonObject.AddPair(c_SwagResponseSchema, fSchema.GenerateJsonRefDefinition)
else if Assigned(fSchema.JsonSchema) then
vJsonObject.AddPair(c_SwagResponseSchema, fSchema.JsonSchema);
if (fExamples.Count > 0) then
vJsonObject.AddPair(c_SwagResponseExamples, GenerateExamplesJsonObject);
if fHeaders.Count > 0 then
begin
vJsonHeaders := TJSONObject.Create;
for vIndex := 0 to fHeaders.Count - 1 do
begin
vJsonHeaders.AddPair(fHeaders[vIndex].Name, fHeaders[vIndex].GenerateJsonObject);
end;
vJsonObject.AddPair(c_SwagResponseHeaders, vJsonHeaders);
end;
Result := vJsonObject;
end;
procedure TSwagResponse.Load(pJson: TJSONObject);
var
vJSONHeaders: TJSONObject;
vIndex: Integer;
vHeader: TSwagHeaders;
begin
if Assigned(pJson.Values[c_SwagResponseDescription]) then
fDescription := pJson.Values[c_SwagResponseDescription].Value;
if Assigned(pJson.Values[c_SwagResponseHeaders]) then
begin
vJSONHeaders := pJson.Values[c_SwagResponseHeaders] as TJSONObject;
for vIndex := 0 to vJSONHeaders.Count - 1 do
begin
vHeader := TSwagHeaders.Create;
vHeader.Load(vJSONHeaders.Pairs[vIndex].JsonValue as TJSONObject);
vHeader.Name := vJSONHeaders.Pairs[vIndex].JsonString.Value;
fHeaders.Add(vheader);
end;
end;
end;
end.