Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 67 additions & 53 deletions openapi2conv/openapi2_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,25 +261,25 @@ func ToV3Parameter(components *openapi3.Components, parameter *openapi2.Paramete
required = []string{parameter.Name}
}
schemaRef := &openapi3.SchemaRef{Value: &openapi3.Schema{
Description: parameter.Description,
Type: typ,
Extensions: stripNonExtensions(parameter.Extensions),
Format: format,
Enum: parameter.Enum,
Min: parameter.Minimum,
Max: parameter.Maximum,
ExclusiveMin: parameter.ExclusiveMin,
ExclusiveMax: parameter.ExclusiveMax,
MinLength: parameter.MinLength,
MaxLength: parameter.MaxLength,
Default: parameter.Default,
MinItems: parameter.MinItems,
MaxItems: parameter.MaxItems,
Pattern: parameter.Pattern,
AllowEmptyValue: parameter.AllowEmptyValue,
UniqueItems: parameter.UniqueItems,
MultipleOf: parameter.MultipleOf,
Required: required,
Description: parameter.Description,
Type: typ,
Extensions: stripNonExtensions(parameter.Extensions),
Format: format,
Enum: parameter.Enum,
Min: parameter.Minimum,
Max: parameter.Maximum,
ExclusiveMinBool: &parameter.ExclusiveMin,
ExclusiveMaxBool: &parameter.ExclusiveMax,
MinLength: parameter.MinLength,
MaxLength: parameter.MaxLength,
Default: parameter.Default,
MinItems: parameter.MinItems,
MaxItems: parameter.MaxItems,
Pattern: parameter.Pattern,
AllowEmptyValue: parameter.AllowEmptyValue,
UniqueItems: parameter.UniqueItems,
MultipleOf: parameter.MultipleOf,
Required: required,
}}
if parameter.Items != nil {
schemaRef.Value.Items = ToV3SchemaRef(parameter.Items)
Expand Down Expand Up @@ -494,8 +494,8 @@ func ToV3SchemaRef(schema *openapi2.SchemaRef) *openapi3.SchemaRef {
Example: schema.Value.Example,
ExternalDocs: schema.Value.ExternalDocs,
UniqueItems: schema.Value.UniqueItems,
ExclusiveMin: schema.Value.ExclusiveMin,
ExclusiveMax: schema.Value.ExclusiveMax,
ExclusiveMinBool: &schema.Value.ExclusiveMin,
ExclusiveMaxBool: &schema.Value.ExclusiveMax,
ReadOnly: schema.Value.ReadOnly,
WriteOnly: schema.Value.WriteOnly,
AllowEmptyValue: schema.Value.AllowEmptyValue,
Expand Down Expand Up @@ -867,19 +867,17 @@ func FromV3SchemaRef(schema *openapi3.SchemaRef, components *openapi3.Components
break
}
}
return nil, &openapi2.Parameter{
In: "formData",
Name: originalName,
Description: schema.Value.Description,
Type: paramType,
Enum: schema.Value.Enum,
Minimum: schema.Value.Min,
Maximum: schema.Value.Max,
ExclusiveMin: schema.Value.ExclusiveMin,
ExclusiveMax: schema.Value.ExclusiveMax,
MinLength: schema.Value.MinLength,
MaxLength: schema.Value.MaxLength,
Default: schema.Value.Default,
param := &openapi2.Parameter{
In: "formData",
Name: originalName,
Description: schema.Value.Description,
Type: paramType,
Enum: schema.Value.Enum,
Minimum: schema.Value.Min,
Maximum: schema.Value.Max,
MinLength: schema.Value.MinLength,
MaxLength: schema.Value.MaxLength,
Default: schema.Value.Default,
// Items: schema.Value.Items,
MinItems: schema.Value.MinItems,
MaxItems: schema.Value.MaxItems,
Expand All @@ -889,6 +887,13 @@ func FromV3SchemaRef(schema *openapi3.SchemaRef, components *openapi3.Components
Extensions: stripNonExtensions(schema.Value.Extensions),
Required: required,
}
if schema.Value.ExclusiveMinBool != nil {
param.ExclusiveMin = *schema.Value.ExclusiveMinBool
}
if schema.Value.ExclusiveMaxBool != nil {
param.ExclusiveMax = *schema.Value.ExclusiveMaxBool
}
Comment on lines +890 to +895

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this condition logic we'll convert values only from v3.0 version and omit values from v3.1. Is it expect? Otherwise we could adjust this conditions to include the next logic:

	if schema.Value.ExclusiveMin != nil {
		v2Schema.ExclusiveMin = true
		v2Schema.Min = schema.Value.ExclusiveMin
	} else if schema.Value.ExclusiveMinBool != nil {
		v2Schema.ExclusiveMin = *schema.Value.ExclusiveMinBool
	}

	if schema.Value.ExclusiveMax != nil {
		v2Schema.ExclusiveMax = true
		v2Schema.Max = schema.Value.ExclusiveMax
	} else if schema.Value.ExclusiveMaxBool != nil {
		v2Schema.ExclusiveMax = *schema.Value.ExclusiveMaxBool
	}

I've tested this on next test:

func TestConvV3SchemaRefToV2(t *testing.T) {
	v3Schema := &openapi3.Schema{
		Type:         &openapi3.Types{"number"},
		ExclusiveMin: openapi3.Ptr(5.0),
		ExclusiveMax: openapi3.Ptr(100.0),
	}

	v2Schema, _ := FromV3SchemaRef(&openapi3.SchemaRef{Value: v3Schema}, nil)

	require.NotNil(t, v2Schema.Value.Min)
	require.Equal(t, 5.0, *v2Schema.Value.Min)
	require.True(t, v2Schema.Value.ExclusiveMin)

	require.NotNil(t, v2Schema.Value.Max)
	require.Equal(t, 100.0, *v2Schema.Value.Max)
	require.True(t, v2Schema.Value.ExclusiveMax)
}

return nil, param
}
}

Expand All @@ -903,8 +908,6 @@ func FromV3SchemaRef(schema *openapi3.SchemaRef, components *openapi3.Components
Example: schema.Value.Example,
ExternalDocs: schema.Value.ExternalDocs,
UniqueItems: schema.Value.UniqueItems,
ExclusiveMin: schema.Value.ExclusiveMin,
ExclusiveMax: schema.Value.ExclusiveMax,
ReadOnly: schema.Value.ReadOnly,
WriteOnly: schema.Value.WriteOnly,
AllowEmptyValue: schema.Value.AllowEmptyValue,
Expand All @@ -926,6 +929,13 @@ func FromV3SchemaRef(schema *openapi3.SchemaRef, components *openapi3.Components
AdditionalProperties: schema.Value.AdditionalProperties,
}

if schema.Value.ExclusiveMinBool != nil {
v2Schema.ExclusiveMin = *schema.Value.ExclusiveMinBool
}
if schema.Value.ExclusiveMaxBool != nil {
v2Schema.ExclusiveMax = *schema.Value.ExclusiveMaxBool
}

if v := schema.Value.Items; v != nil {
v2Schema.Items, _ = FromV3SchemaRef(v, components)
}
Expand Down Expand Up @@ -1031,30 +1041,34 @@ func FromV3RequestBodyFormData(mediaType *openapi3.MediaType) openapi2.Parameter
v2Items, _ = FromV3SchemaRef(val.Items, nil)
}
parameter := &openapi2.Parameter{
Name: propName,
Description: val.Description,
Type: typ,
In: "formData",
Extensions: stripNonExtensions(val.Extensions),
Enum: val.Enum,
ExclusiveMin: val.ExclusiveMin,
ExclusiveMax: val.ExclusiveMax,
MinLength: val.MinLength,
MaxLength: val.MaxLength,
Default: val.Default,
Items: v2Items,
MinItems: val.MinItems,
MaxItems: val.MaxItems,
Maximum: val.Max,
Minimum: val.Min,
Pattern: val.Pattern,
Name: propName,
Description: val.Description,
Type: typ,
In: "formData",
Extensions: stripNonExtensions(val.Extensions),
Enum: val.Enum,
MinLength: val.MinLength,
MaxLength: val.MaxLength,
Default: val.Default,
Items: v2Items,
MinItems: val.MinItems,
MaxItems: val.MaxItems,
Maximum: val.Max,
Minimum: val.Min,
Pattern: val.Pattern,
// CollectionFormat: val.CollectionFormat,
// Format: val.Format,
AllowEmptyValue: val.AllowEmptyValue,
Required: required,
UniqueItems: val.UniqueItems,
MultipleOf: val.MultipleOf,
}
if val.ExclusiveMinBool != nil {
parameter.ExclusiveMin = *val.ExclusiveMinBool
}
if val.ExclusiveMaxBool != nil {
parameter.ExclusiveMax = *val.ExclusiveMaxBool
}
parameters = append(parameters, parameter)
}
return parameters
Expand Down
Loading