Skip to content

Commit

Permalink
guess the object or array if there is no type
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Dec 28, 2024
1 parent 99e7c51 commit 908d17e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 68 deletions.
53 changes: 25 additions & 28 deletions ndc-http-schema/openapi/internal/oas2_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,44 +60,41 @@ func (oc *oas2SchemaBuilder) getSchemaTypeFromParameter(param *v2.Parameter, fie
}

// get and convert an OpenAPI data type to a NDC type
func (oc *oas2SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths []string) (*SchemaInfoCache, error) {
if typeSchema == nil {
func (oc *oas2SchemaBuilder) getSchemaType(baseSchema *base.Schema, fieldPaths []string) (*SchemaInfoCache, error) {
if baseSchema == nil {
return nil, errParameterSchemaEmpty(fieldPaths)
}

if oc.builder.ConvertOptions.NoDeprecation && typeSchema.Deprecated != nil && *typeSchema.Deprecated {
if oc.builder.ConvertOptions.NoDeprecation && baseSchema.Deprecated != nil && *baseSchema.Deprecated {
return nil, nil
}

if len(typeSchema.AllOf) > 0 {
return oc.buildUnionSchemaType(typeSchema, typeSchema.AllOf, oasAllOf, fieldPaths)
if len(baseSchema.AllOf) > 0 {
return oc.buildUnionSchemaType(baseSchema, baseSchema.AllOf, oasAllOf, fieldPaths)
}

if len(typeSchema.AnyOf) > 0 {
return oc.buildUnionSchemaType(typeSchema, typeSchema.AnyOf, oasAnyOf, fieldPaths)
if len(baseSchema.AnyOf) > 0 {
return oc.buildUnionSchemaType(baseSchema, baseSchema.AnyOf, oasAnyOf, fieldPaths)
}

if len(typeSchema.OneOf) > 0 {
return oc.buildUnionSchemaType(typeSchema, typeSchema.OneOf, oasOneOf, fieldPaths)
if len(baseSchema.OneOf) > 0 {
return oc.buildUnionSchemaType(baseSchema, baseSchema.OneOf, oasOneOf, fieldPaths)
}

oasTypes, nullable := extractNullableFromOASTypes(typeSchema.Type)
nullable = nullable || (typeSchema.Nullable != nil && *typeSchema.Nullable)
if typeSchema.AdditionalProperties != nil && (typeSchema.AdditionalProperties.B || typeSchema.AdditionalProperties.A != nil) {
var result schema.TypeEncoder = oc.builder.buildScalarJSON()
if nullable {
result = schema.NewNullableType(result)
}
oasTypes, nullable := extractNullableFromOASTypes(baseSchema.Type)
nullable = nullable || (baseSchema.Nullable != nil && *baseSchema.Nullable)

return &SchemaInfoCache{
TypeRead: result,
TypeWrite: result,
TypeSchema: createSchemaFromOpenAPISchema(typeSchema),
}, nil
if len(oasTypes) == 0 {
// if the OAS schema has properties or items we can consider this type as an object or array
if baseSchema.Properties != nil && baseSchema.Properties.Len() > 0 {
oasTypes = []string{"object"}
} else if baseSchema.Items != nil && baseSchema.Items.A != nil {
oasTypes = []string{"array"}
}
}

if len(oasTypes) != 1 || isPrimitiveScalar(oasTypes) {
scalarName := getScalarFromType(oc.builder.schema, oasTypes, typeSchema.Format, typeSchema.Enum, fieldPaths)
scalarName := getScalarFromType(oc.builder.schema, oasTypes, baseSchema.Format, baseSchema.Enum, fieldPaths)
var resultType schema.TypeEncoder = schema.NewNamedType(scalarName)
if nullable {
resultType = schema.NewNullableType(resultType)
Expand All @@ -106,7 +103,7 @@ func (oc *oas2SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths [
return &SchemaInfoCache{
TypeRead: resultType,
TypeWrite: resultType,
TypeSchema: createSchemaFromOpenAPISchema(typeSchema),
TypeSchema: createSchemaFromOpenAPISchema(baseSchema),
}, nil
}

Expand All @@ -115,14 +112,14 @@ func (oc *oas2SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths [
typeName := oasTypes[0]
switch typeName {
case "object":
typeResult, err = oc.evalObjectType(typeSchema, fieldPaths)
typeResult, err = oc.evalObjectType(baseSchema, fieldPaths)
if err != nil {
return nil, err
}
case "array":
var itemSchema *SchemaInfoCache
if typeSchema.Items != nil || typeSchema.Items.A != nil {
itemSchema, err = oc.getSchemaTypeFromProxy(typeSchema.Items.A, false, fieldPaths)
if baseSchema.Items != nil || baseSchema.Items.A != nil {
itemSchema, err = oc.getSchemaTypeFromProxy(baseSchema.Items.A, false, fieldPaths)
if err != nil {
return nil, err
}
Expand All @@ -136,7 +133,7 @@ func (oc *oas2SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths [
}

typeResult = &SchemaInfoCache{
TypeSchema: createSchemaFromOpenAPISchema(typeSchema),
TypeSchema: createSchemaFromOpenAPISchema(baseSchema),
TypeRead: schema.NewArrayType(itemSchema.TypeRead),
TypeWrite: schema.NewArrayType(itemSchema.TypeWrite),
}
Expand All @@ -161,7 +158,7 @@ func (oc *oas2SchemaBuilder) evalObjectType(baseSchema *base.Schema, fieldPaths
typeResult := createSchemaFromOpenAPISchema(baseSchema)
refName := utils.StringSliceToPascalCase(fieldPaths)

if baseSchema.Properties == nil || baseSchema.Properties.IsZero() {
if baseSchema.Properties == nil || baseSchema.Properties.IsZero() || (baseSchema.AdditionalProperties != nil && (baseSchema.AdditionalProperties.B || baseSchema.AdditionalProperties.A != nil)) {
// treat no-property objects as a JSON scalar
var scalarType schema.TypeEncoder = oc.builder.buildScalarJSON()
if baseSchema.Nullable != nil && *baseSchema.Nullable {
Expand Down
52 changes: 24 additions & 28 deletions ndc-http-schema/openapi/internal/oas3_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,44 +86,40 @@ func (oc *oas3SchemaBuilder) getSchemaTypeFromProxy(schemaProxy *base.SchemaProx
}

// get and convert an OpenAPI data type to a NDC type
func (oc *oas3SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths []string) (*SchemaInfoCache, error) {
if typeSchema == nil {
func (oc *oas3SchemaBuilder) getSchemaType(baseSchema *base.Schema, fieldPaths []string) (*SchemaInfoCache, error) {
if baseSchema == nil {
return nil, errParameterSchemaEmpty(fieldPaths)
}

if oc.builder.ConvertOptions.NoDeprecation && typeSchema.Deprecated != nil && *typeSchema.Deprecated {
if oc.builder.ConvertOptions.NoDeprecation && baseSchema.Deprecated != nil && *baseSchema.Deprecated {
return nil, nil
}

if len(typeSchema.AllOf) > 0 {
return oc.buildUnionSchemaType(typeSchema, typeSchema.AllOf, oasAllOf, fieldPaths)
if len(baseSchema.AllOf) > 0 {
return oc.buildUnionSchemaType(baseSchema, baseSchema.AllOf, oasAllOf, fieldPaths)
}

if len(typeSchema.AnyOf) > 0 {
return oc.buildUnionSchemaType(typeSchema, typeSchema.AnyOf, oasAnyOf, fieldPaths)
if len(baseSchema.AnyOf) > 0 {
return oc.buildUnionSchemaType(baseSchema, baseSchema.AnyOf, oasAnyOf, fieldPaths)
}

if len(typeSchema.OneOf) > 0 {
return oc.buildUnionSchemaType(typeSchema, typeSchema.OneOf, oasOneOf, fieldPaths)
if len(baseSchema.OneOf) > 0 {
return oc.buildUnionSchemaType(baseSchema, baseSchema.OneOf, oasOneOf, fieldPaths)
}

oasTypes, nullable := extractNullableFromOASTypes(typeSchema.Type)
nullable = nullable || (typeSchema.Nullable != nil && *typeSchema.Nullable)
if typeSchema.AdditionalProperties != nil && (typeSchema.AdditionalProperties.B || typeSchema.AdditionalProperties.A != nil) {
var result schema.TypeEncoder = oc.builder.buildScalarJSON()
if nullable {
result = schema.NewNullableType(result)
oasTypes, nullable := extractNullableFromOASTypes(baseSchema.Type)
nullable = nullable || (baseSchema.Nullable != nil && *baseSchema.Nullable)
if len(oasTypes) == 0 {
// if the OAS schema has properties or items we can consider this type as an object or array
if baseSchema.Properties != nil && baseSchema.Properties.Len() > 0 {
oasTypes = []string{"object"}
} else if baseSchema.Items != nil && baseSchema.Items.A != nil {
oasTypes = []string{"array"}
}

return &SchemaInfoCache{
TypeRead: result,
TypeWrite: result,
TypeSchema: createSchemaFromOpenAPISchema(typeSchema),
}, nil
}

if len(oasTypes) != 1 || isPrimitiveScalar(oasTypes) {
scalarName := getScalarFromType(oc.builder.schema, oasTypes, typeSchema.Format, typeSchema.Enum, fieldPaths)
scalarName := getScalarFromType(oc.builder.schema, oasTypes, baseSchema.Format, baseSchema.Enum, fieldPaths)
var resultType schema.TypeEncoder = schema.NewNamedType(scalarName)
if nullable {
resultType = schema.NewNullableType(resultType)
Expand All @@ -132,7 +128,7 @@ func (oc *oas3SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths [
return &SchemaInfoCache{
TypeRead: resultType,
TypeWrite: resultType,
TypeSchema: createSchemaFromOpenAPISchema(typeSchema),
TypeSchema: createSchemaFromOpenAPISchema(baseSchema),
}, nil
}

Expand All @@ -141,14 +137,14 @@ func (oc *oas3SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths [
typeName := oasTypes[0]
switch typeName {
case "object":
typeResult, err = oc.evalObjectType(typeSchema, fieldPaths)
typeResult, err = oc.evalObjectType(baseSchema, fieldPaths)
if err != nil {
return nil, err
}
case "array":
var itemSchema *SchemaInfoCache
if typeSchema.Items != nil && typeSchema.Items.A != nil {
itemSchema, err = oc.getSchemaTypeFromProxy(typeSchema.Items.A, false, fieldPaths)
if baseSchema.Items != nil && baseSchema.Items.A != nil {
itemSchema, err = oc.getSchemaTypeFromProxy(baseSchema.Items.A, false, fieldPaths)
if err != nil {
return nil, err
}
Expand All @@ -162,7 +158,7 @@ func (oc *oas3SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths [
}

typeResult = &SchemaInfoCache{
TypeSchema: createSchemaFromOpenAPISchema(typeSchema),
TypeSchema: createSchemaFromOpenAPISchema(baseSchema),
TypeRead: schema.NewArrayType(itemSchema.TypeRead),
TypeWrite: schema.NewArrayType(itemSchema.TypeWrite),
}
Expand All @@ -185,7 +181,7 @@ func (oc *oas3SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths [
func (oc *oas3SchemaBuilder) evalObjectType(baseSchema *base.Schema, fieldPaths []string) (*SchemaInfoCache, error) {
typeResult := createSchemaFromOpenAPISchema(baseSchema)
refName := utils.StringSliceToPascalCase(fieldPaths)
if baseSchema.Properties == nil || baseSchema.Properties.IsZero() {
if baseSchema.Properties == nil || baseSchema.Properties.IsZero() || (baseSchema.AdditionalProperties != nil && (baseSchema.AdditionalProperties.B || baseSchema.AdditionalProperties.A != nil)) {
// treat no-property objects as a JSON scalar
var scalarType schema.TypeEncoder = oc.builder.buildScalarJSON()
if baseSchema.Nullable != nil && *baseSchema.Nullable {
Expand Down
7 changes: 5 additions & 2 deletions ndc-http-schema/openapi/testdata/openai/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,11 @@
"type": {
"type": "nullable",
"underlying_type": {
"name": "JSON",
"type": "named"
"type": "nullable",
"underlying_type": {
"name": "JSON",
"type": "named"
}
}
},
"http": {
Expand Down
7 changes: 5 additions & 2 deletions ndc-http-schema/openapi/testdata/openai/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,11 @@
"type": {
"type": "nullable",
"underlying_type": {
"name": "JSON",
"type": "named"
"type": "nullable",
"underlying_type": {
"name": "JSON",
"type": "named"
}
}
}
},
Expand Down
1 change: 1 addition & 0 deletions ndc-http-schema/openapi/testdata/petstore2/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
"description": "successful operation",
"schema": {
"type": "array",
"additionalProperties": true,
"items": { "$ref": "#/definitions/Pet" }
}
},
Expand Down
6 changes: 1 addition & 5 deletions ndc-http-schema/openapi/testdata/petstore3/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -3287,11 +3287,7 @@
"type": "named"
}
},
"http": {
"type": [
"object"
]
}
"http": {}
},
"discounts": {
"description": "The coupon or promotion code to apply to this Session. Currently, only up to one may be specified.",
Expand Down
5 changes: 2 additions & 3 deletions ndc-http-schema/openapi/testdata/petstore3/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -1557,8 +1557,7 @@
"x-stripeBypassValidation": true
}
},
"title": "customer_update_params",
"type": "object"
"title": "customer_update_params"
},
"discounts": {
"description": "The coupon or promotion code to apply to this Session. Currently, only up to one may be specified.",
Expand Down Expand Up @@ -3970,6 +3969,7 @@
},
"tags": {
"type": "array",
"additionalProperties": true,
"xml": {
"wrapped": true
},
Expand Down Expand Up @@ -4369,7 +4369,6 @@
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/User"
}
Expand Down

0 comments on commit 908d17e

Please sign in to comment.