Skip to content

Commit c83c55b

Browse files
committed
feat: added support for IP masks in ligato proto model annotations and implemented proper json schema export according to new annotation possibilities
Signed-off-by: Filip Gschwandtner <[email protected]>
1 parent d04592e commit c83c55b

21 files changed

+379
-271
lines changed

plugins/restapi/jsonschema/converter/types.go

+65
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import (
1616
"github.com/xeipuuv/gojsonschema"
1717
)
1818

19+
const (
20+
PatternIpv6WithMask = "^(::|(([a-fA-F0-9]{1,4}):){7}(([a-fA-F0-9]{1,4}))|(:(:([a-fA-F0-9]{1,4})){1,6})|((([a-fA-F0-9]{1,4}):){1,6}:)|((([a-fA-F0-9]{1,4}):)(:([a-fA-F0-9]{1,4})){1,6})|((([a-fA-F0-9]{1,4}):){2}(:([a-fA-F0-9]{1,4})){1,5})|((([a-fA-F0-9]{1,4}):){3}(:([a-fA-F0-9]{1,4})){1,4})|((([a-fA-F0-9]{1,4}):){4}(:([a-fA-F0-9]{1,4})){1,3})|((([a-fA-F0-9]{1,4}):){5}(:([a-fA-F0-9]{1,4})){1,2}))(\\\\/(12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))$"
21+
PatternIpv4WithMask = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(/(3[0-2]|[1-2][0-9]|[0-9]))$"
22+
)
23+
1924
var (
2025
globalPkg = newProtoPackage(nil, "")
2126

@@ -101,6 +106,7 @@ func (c *Converter) applyAllowNullValuesOption(schema *jsonschema.Type, schemaCh
101106
}, {
102107
Type: schemaChanges.Type,
103108
Format: schemaChanges.Format,
109+
Pattern: schemaChanges.Pattern,
104110
Minimum: schemaChanges.Minimum,
105111
ExclusiveMinimum: schemaChanges.ExclusiveMinimum,
106112
Maximum: schemaChanges.Maximum,
@@ -115,6 +121,7 @@ func (c *Converter) applyAllowNullValuesOption(schema *jsonschema.Type, schemaCh
115121
} else { // direct mapping (schema could be already partially built -> need to fill new values into it)
116122
schema.Type = schemaChanges.Type
117123
schema.Format = schemaChanges.Format
124+
schema.Pattern = schemaChanges.Pattern
118125
schema.Minimum = schemaChanges.Minimum
119126
schema.ExclusiveMinimum = schemaChanges.ExclusiveMinimum
120127
schema.Maximum = schemaChanges.Maximum
@@ -224,6 +231,64 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes
224231
Format: "ipv6",
225232
},
226233
}
234+
case ligato.LigatoOptions_IPV4_WITH_MASK:
235+
schema.Type = gojsonschema.TYPE_STRING
236+
schema.Pattern = PatternIpv4WithMask
237+
case ligato.LigatoOptions_IPV6_WITH_MASK:
238+
schema.Type = gojsonschema.TYPE_STRING
239+
schema.Pattern = PatternIpv6WithMask
240+
case ligato.LigatoOptions_IP_WITH_MASK:
241+
schema.OneOf = []*jsonschema.Type{
242+
{
243+
Type: gojsonschema.TYPE_STRING,
244+
Pattern: PatternIpv4WithMask,
245+
},
246+
{
247+
Type: gojsonschema.TYPE_STRING,
248+
Pattern: PatternIpv6WithMask,
249+
},
250+
}
251+
case ligato.LigatoOptions_IPV4_OPTIONAL_MASK:
252+
schema.OneOf = []*jsonschema.Type{
253+
{
254+
Type: gojsonschema.TYPE_STRING,
255+
Format: "ipv4",
256+
},
257+
{
258+
Type: gojsonschema.TYPE_STRING,
259+
Pattern: PatternIpv4WithMask,
260+
},
261+
}
262+
case ligato.LigatoOptions_IPV6_OPTIONAL_MASK:
263+
schema.OneOf = []*jsonschema.Type{
264+
{
265+
Type: gojsonschema.TYPE_STRING,
266+
Format: "ipv6",
267+
},
268+
{
269+
Type: gojsonschema.TYPE_STRING,
270+
Pattern: PatternIpv6WithMask,
271+
},
272+
}
273+
case ligato.LigatoOptions_IP_OPTIONAL_MASK:
274+
schema.OneOf = []*jsonschema.Type{
275+
{
276+
Type: gojsonschema.TYPE_STRING,
277+
Format: "ipv4",
278+
},
279+
{
280+
Type: gojsonschema.TYPE_STRING,
281+
Pattern: PatternIpv4WithMask,
282+
},
283+
{
284+
Type: gojsonschema.TYPE_STRING,
285+
Format: "ipv6",
286+
},
287+
{
288+
Type: gojsonschema.TYPE_STRING,
289+
Pattern: PatternIpv6WithMask,
290+
},
291+
}
227292
default: // no annotations or annotation used are not applicable here
228293
schema.Type = gojsonschema.TYPE_STRING
229294
}

proto/ligato/annotations.pb.go

+48-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/ligato/annotations.proto

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ message LigatoOptions {
1818
IP = 1;
1919
IPV4 = 2;
2020
IPV6 = 3;
21+
IP_WITH_MASK = 4;
22+
IPV4_WITH_MASK = 5;
23+
IPV6_WITH_MASK = 6;
24+
IP_OPTIONAL_MASK = 7;
25+
IPV4_OPTIONAL_MASK = 8;
26+
IPV6_OPTIONAL_MASK = 9;
2127
}
2228
Type type = 1;
2329

0 commit comments

Comments
 (0)