diff --git a/openapi/internal/oas2.go b/openapi/internal/oas2.go index bcf020b..0cb3e92 100644 --- a/openapi/internal/oas2.go +++ b/openapi/internal/oas2.go @@ -16,15 +16,20 @@ import ( "github.com/pb33f/libopenapi/orderedmap" ) +// OAS2Builder the NDC schema builder from OpenAPI 2.0 specification type OAS2Builder struct { - schema *rest.NDCRestSchema *ConvertOptions + + schema *rest.NDCRestSchema + typeUsageCounter TypeUsageCounter } +// NewOAS2Builder creates an OAS3Builder instance func NewOAS2Builder(schema *rest.NDCRestSchema, options ConvertOptions) *OAS2Builder { builder := &OAS2Builder{ - schema: schema, - ConvertOptions: applyConvertOptions(options), + schema: schema, + typeUsageCounter: TypeUsageCounter{}, + ConvertOptions: applyConvertOptions(options), } setDefaultSettings(builder.schema.Settings, builder.ConvertOptions) @@ -82,6 +87,7 @@ func (oc *OAS2Builder) BuildDocumentModel(docModel *libopenapi.DocumentModel[v2. } oc.schema.Settings.Security = convertSecurities(docModel.Model.Security) + cleanUnusedSchemaTypes(oc.schema, &oc.typeUsageCounter) return nil } @@ -251,6 +257,7 @@ func (oc *OAS2Builder) getSchemaTypeFromParameter(param *v2.Parameter, apiPath s if isPrimitiveScalar(param.Type) { scalarName := getScalarFromType(oc.schema, []string{param.Type}, param.Format, param.Enum, oc.trimPathPrefix(apiPath), fieldPaths) result = schema.NewNamedType(scalarName) + oc.typeUsageCounter.Increase(scalarName) } else { switch param.Type { case "object": @@ -262,6 +269,7 @@ func (oc *OAS2Builder) getSchemaTypeFromParameter(param *v2.Parameter, apiPath s itemName := getScalarFromType(oc.schema, []string{param.Items.Type}, param.Format, param.Enum, oc.trimPathPrefix(apiPath), fieldPaths) result = schema.NewArrayType(schema.NewNamedType(itemName)) + oc.typeUsageCounter.Increase(itemName) default: return nil, fmt.Errorf("unsupported schema type %s", param.Type) @@ -287,6 +295,7 @@ func (oc *OAS2Builder) getSchemaType(typeSchema *base.Schema, apiPath string, fi if _, ok := oc.schema.ScalarTypes[scalarName]; !ok { oc.schema.ScalarTypes[scalarName] = *defaultScalarTypes[rest.ScalarJSON] } + oc.typeUsageCounter.Increase(scalarName) typeResult = createSchemaFromOpenAPISchema(typeSchema, scalarName) return schema.NewNamedType(scalarName), typeResult, nil } @@ -300,6 +309,7 @@ func (oc *OAS2Builder) getSchemaType(typeSchema *base.Schema, apiPath string, fi if isPrimitiveScalar(typeName) { scalarName := getScalarFromType(oc.schema, typeSchema.Type, typeSchema.Format, typeSchema.Enum, oc.trimPathPrefix(apiPath), fieldPaths) result = schema.NewNamedType(scalarName) + oc.typeUsageCounter.Increase(scalarName) typeResult = createSchemaFromOpenAPISchema(typeSchema, scalarName) } else { @@ -337,6 +347,8 @@ func (oc *OAS2Builder) getSchemaType(typeSchema *base.Schema, apiPath string, fi propApiSchema.Nullable = nullable typeResult.Properties[propName] = *propApiSchema object.Fields[propName] = objField + + oc.typeUsageCounter.Increase(getNamedType(propType, true, "")) } oc.schema.ObjectTypes[refName] = object @@ -350,6 +362,7 @@ func (oc *OAS2Builder) getSchemaType(typeSchema *base.Schema, apiPath string, fi itemName := getSchemaRefTypeNameV2(typeSchema.Items.A.GetReference()) if itemName != "" { itemName := utils.ToPascalCase(itemName) + oc.typeUsageCounter.Increase(itemName) result = schema.NewArrayType(schema.NewNamedType(itemName)) } else { itemSchemaA := typeSchema.Items.A.Schema() @@ -361,6 +374,7 @@ func (oc *OAS2Builder) getSchemaType(typeSchema *base.Schema, apiPath string, fi typeResult.Items = propType result = schema.NewArrayType(itemSchema) + oc.typeUsageCounter.Increase(getNamedType(itemSchema, true, "")) } } diff --git a/openapi/internal/oas2_operation.go b/openapi/internal/oas2_operation.go index b551f76..1f58aa2 100644 --- a/openapi/internal/oas2_operation.go +++ b/openapi/internal/oas2_operation.go @@ -202,6 +202,7 @@ func (oc *oas2OperationBuilder) convertParameters(params []*v2.Parameter, apiPat return nil, err } + oc.builder.typeUsageCounter.Increase(getNamedType(typeEncoder, true, "")) argument := schema.ArgumentInfo{ Type: typeEncoder.Encode(), } @@ -262,12 +263,15 @@ func (oc *oas2OperationBuilder) convertResponse(responses *v2.Responses, apiPath // return nullable boolean type if the response content is null if resp == nil || resp.Schema == nil { - return schema.NewNullableNamedType("Boolean"), nil + scalarName := string(rest.ScalarBoolean) + oc.builder.typeUsageCounter.Increase(scalarName) + return schema.NewNullableNamedType(scalarName), nil } schemaType, _, err := oc.builder.getSchemaTypeFromProxy(resp.Schema, false, apiPath, fieldPaths) if err != nil { return nil, err } + oc.builder.typeUsageCounter.Increase(getNamedType(schemaType, true, "")) return schemaType, nil } diff --git a/openapi/internal/oas3.go b/openapi/internal/oas3.go index d607e05..ccdfe5d 100644 --- a/openapi/internal/oas3.go +++ b/openapi/internal/oas3.go @@ -14,17 +14,22 @@ import ( "github.com/pb33f/libopenapi/orderedmap" ) +// OAS3Builder the NDC schema builder from OpenAPI 3.0 specification type OAS3Builder struct { - schema *rest.NDCRestSchema - evaluatingTypes map[string]string *ConvertOptions + + schema *rest.NDCRestSchema + evaluatingTypes map[string]string + typeUsageCounter TypeUsageCounter } +// NewOAS3Builder creates an OAS3Builder instance func NewOAS3Builder(schema *rest.NDCRestSchema, options ConvertOptions) *OAS3Builder { builder := &OAS3Builder{ - schema: schema, - evaluatingTypes: make(map[string]string), - ConvertOptions: applyConvertOptions(options), + schema: schema, + evaluatingTypes: make(map[string]string), + typeUsageCounter: TypeUsageCounter{}, + ConvertOptions: applyConvertOptions(options), } setDefaultSettings(builder.schema.Settings, builder.ConvertOptions) @@ -71,6 +76,7 @@ func (oc *OAS3Builder) BuildDocumentModel(docModel *libopenapi.DocumentModel[v3. // reevaluate write argument types oc.evaluatingTypes = make(map[string]string) oc.transformWriteSchema() + cleanUnusedSchemaTypes(oc.schema, &oc.typeUsageCounter) return nil } @@ -242,6 +248,7 @@ func (oc *OAS3Builder) convertComponentSchemas(schemaItem orderedmap.Pair[string scalar := schema.NewScalarType() scalar.Representation = schema.NewTypeRepresentationJSON().Encode() oc.schema.ScalarTypes[refName] = *scalar + oc.evaluatingTypes[fmt.Sprintf("#/components/schemas/%s", typeKey)] = refName } return err @@ -260,6 +267,7 @@ func (oc *OAS3Builder) buildScalarJSON() *schema.NamedType { if _, ok := oc.schema.ScalarTypes[scalarName]; !ok { oc.schema.ScalarTypes[scalarName] = *defaultScalarTypes[rest.ScalarJSON] } + oc.typeUsageCounter.Increase(scalarName) return schema.NewNamedType(scalarName) } @@ -311,6 +319,8 @@ func (oc *OAS3Builder) populateWriteSchemaType(schemaType schema.Type) (schema.T writeName := formatWriteObjectName(ty.Name) if _, ok := oc.schema.ObjectTypes[writeName]; ok { + oc.typeUsageCounter.Increase(writeName) + oc.typeUsageCounter.Decrease(ty.Name) return schema.NewNamedType(writeName).Encode(), writeName, true } if evaluated { @@ -339,6 +349,8 @@ func (oc *OAS3Builder) populateWriteSchemaType(schemaType schema.Type) (schema.T } } if hasWriteField { + oc.typeUsageCounter.Increase(writeName) + oc.typeUsageCounter.Decrease(ty.Name) oc.schema.ObjectTypes[writeName] = writeObject return schema.NewNamedType(writeName).Encode(), writeName, true } diff --git a/openapi/internal/oas3_operation.go b/openapi/internal/oas3_operation.go index 9369e17..e2edcf7 100644 --- a/openapi/internal/oas3_operation.go +++ b/openapi/internal/oas3_operation.go @@ -303,6 +303,7 @@ func (oc *oas3OperationBuilder) convertRequestBody(reqBody *v3.RequestBody, apiP return nil, nil, nil } + oc.builder.typeUsageCounter.Increase(getNamedType(schemaType, true, "")) bodyResult := &rest.RequestBody{ ContentType: contentType, Schema: typeSchema, @@ -366,6 +367,7 @@ func (oc *oas3OperationBuilder) convertRequestBody(reqBody *v3.RequestBody, apiP EncodingObject: headerEncoding, } + oc.builder.typeUsageCounter.Increase(getNamedType(ndcType, true, "")) argument := schema.ArgumentInfo{ Type: ndcType.Encode(), } @@ -400,7 +402,9 @@ func (oc *oas3OperationBuilder) convertResponse(responses *v3.Responses, apiPath // return nullable boolean type if the response content is null if resp == nil || resp.Content == nil { - return schema.NewNullableNamedType("Boolean"), nil + scalarName := string(rest.ScalarBoolean) + oc.builder.typeUsageCounter.Increase(scalarName) + return schema.NewNullableNamedType(scalarName), nil } jsonContent, ok := resp.Content.Get("application/json") if !ok { @@ -412,6 +416,7 @@ func (oc *oas3OperationBuilder) convertResponse(responses *v3.Responses, apiPath if err != nil { return nil, err } + oc.builder.typeUsageCounter.Increase(getNamedType(schemaType, true, "")) return schemaType, nil } diff --git a/openapi/internal/oas3_schema.go b/openapi/internal/oas3_schema.go index ce64e76..ecf6277 100644 --- a/openapi/internal/oas3_schema.go +++ b/openapi/internal/oas3_schema.go @@ -45,7 +45,6 @@ func (oc *oas3SchemaBuilder) getSchemaTypeFromProxy(schemaProxy *base.SchemaProx var err error rawRefName := schemaProxy.GetReference() - if rawRefName == "" { ndcType, typeSchema, isRef, err = oc.getSchemaType(innerSchema, fieldPaths) if err != nil { @@ -58,6 +57,7 @@ func (oc *oas3SchemaBuilder) getSchemaTypeFromProxy(schemaProxy *base.SchemaProx Type: objectName, Description: innerSchema.Description, } + oc.builder.typeUsageCounter.Increase(objectName) } else { // return early object from ref refName := getSchemaRefTypeNameV3(rawRefName) @@ -72,12 +72,14 @@ func (oc *oas3SchemaBuilder) getSchemaTypeFromProxy(schemaProxy *base.SchemaProx return nil, nil, false, err } typeSchema.Description = innerSchema.Description + oc.builder.typeUsageCounter.Increase(getNamedType(ndcType, true, "")) } else { ndcType = schema.NewNamedType(objectName) typeSchema = &rest.TypeSchema{ Type: objectName, Description: innerSchema.Description, } + oc.builder.typeUsageCounter.Increase(objectName) } } @@ -152,6 +154,7 @@ func (oc *oas3SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths [ scalarName := getScalarFromType(oc.builder.schema, typeSchema.Type, typeSchema.Format, typeSchema.Enum, oc.builder.trimPathPrefix(oc.apiPath), fieldPaths) result = schema.NewNamedType(scalarName) typeResult = createSchemaFromOpenAPISchema(typeSchema, scalarName) + oc.builder.typeUsageCounter.Increase(scalarName) } else { typeName := typeSchema.Type[0] typeResult = createSchemaFromOpenAPISchema(typeSchema, typeName) @@ -219,6 +222,7 @@ func (oc *oas3SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths [ if len(readObject.Fields) == 0 && len(writeObject.Fields) == 0 { oc.builder.schema.ObjectTypes[refName] = object result = schema.NewNamedType(refName) + oc.builder.typeUsageCounter.Increase(refName) } else { for key, field := range object.Fields { readObject.Fields[key] = field @@ -229,8 +233,10 @@ func (oc *oas3SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths [ oc.builder.schema.ObjectTypes[writeRefName] = writeObject if oc.writeMode { result = schema.NewNamedType(writeRefName) + oc.builder.typeUsageCounter.Increase(writeRefName) } else { result = schema.NewNamedType(refName) + oc.builder.typeUsageCounter.Increase(refName) } } case "array": @@ -239,7 +245,9 @@ func (oc *oas3SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths [ } itemName := getSchemaRefTypeNameV3(typeSchema.Items.A.GetReference()) + var refName string if itemName != "" { + refName = itemName result = schema.NewArrayType(schema.NewNamedType(utils.ToPascalCase(itemName))) } else { itemSchemaA := typeSchema.Items.A.Schema() @@ -249,6 +257,7 @@ func (oc *oas3SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths [ return nil, nil, isRef, err } if itemSchema != nil { + refName = getNamedType(itemSchema, true, "") result = schema.NewArrayType(itemSchema) } else { result = schema.NewArrayType(oc.builder.buildScalarJSON()) @@ -262,6 +271,7 @@ func (oc *oas3SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths [ if result == nil { return nil, nil, false, fmt.Errorf("cannot parse type reference name: %s", typeSchema.Items.A.GetReference()) } + oc.builder.typeUsageCounter.Increase(refName) default: return nil, nil, false, fmt.Errorf("unsupported schema type %s", typeName) } @@ -362,6 +372,8 @@ func (oc *oas3SchemaBuilder) buildAllOfAnyOfSchemaType(schemaProxies []*base.Sch if oc.writeMode && len(writeObject.Fields) > 0 { refName = writeRefName } + + oc.builder.typeUsageCounter.Increase(refName) if len(typeSchema.Properties) == 0 { typeSchema = &rest.TypeSchema{ Type: refName, diff --git a/openapi/internal/types.go b/openapi/internal/types.go new file mode 100644 index 0000000..f5a62e8 --- /dev/null +++ b/openapi/internal/types.go @@ -0,0 +1,147 @@ +package internal + +import ( + "log/slog" + "regexp" + + rest "github.com/hasura/ndc-rest-schema/schema" + "github.com/hasura/ndc-sdk-go/schema" +) + +var ( + bracketRegexp = regexp.MustCompile(`[\{\}]`) + schemaRefNameV2Regexp = regexp.MustCompile(`^#/definitions/([a-zA-Z0-9\.\-_]+)$`) + schemaRefNameV3Regexp = regexp.MustCompile(`^#/components/schemas/([a-zA-Z0-9\.\-_]+)$`) +) + +var defaultScalarTypes = map[rest.ScalarName]*schema.ScalarType{ + rest.ScalarBoolean: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationBoolean().Encode(), + }, + rest.ScalarString: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationString().Encode(), + }, + rest.ScalarInt32: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationInt32().Encode(), + }, + rest.ScalarInt64: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationInt64().Encode(), + }, + rest.ScalarFloat32: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationFloat32().Encode(), + }, + rest.ScalarFloat64: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationFloat64().Encode(), + }, + rest.ScalarJSON: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationJSON().Encode(), + }, + // string format variants https://swagger.io/docs/specification/data-models/data-types/#string + // string with date format + rest.ScalarDate: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationDate().Encode(), + }, + // string with date-time format + rest.ScalarTimestampTZ: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationTimestampTZ().Encode(), + }, + // string with byte format + rest.ScalarBytes: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationBytes().Encode(), + }, + // string with byte format + rest.ScalarBinary: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationBytes().Encode(), + }, + rest.ScalarEmail: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationString().Encode(), + }, + rest.ScalarURI: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationString().Encode(), + }, + rest.ScalarUUID: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationUUID().Encode(), + }, + rest.ScalarIPV4: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationString().Encode(), + }, + rest.ScalarIPV6: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationString().Encode(), + }, + // unix-time the timestamp integer which is measured in seconds since the Unix epoch + rest.ScalarUnixTime: { + AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, + ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, + Representation: schema.NewTypeRepresentationInt32().Encode(), + }, +} + +// ConvertOptions represent the common convert options for both OpenAPI v2 and v3 +type ConvertOptions struct { + MethodAlias map[string]string + TrimPrefix string + EnvPrefix string + Logger *slog.Logger +} + +// TypeUsageCounter tracks the list of reference types and number of usage of them in other models +type TypeUsageCounter map[string]int + +// Increase increases the usage of an element +func (tuc *TypeUsageCounter) Increase(name string) { + if counter, ok := (*tuc)[name]; ok { + (*tuc)[name] = counter + 1 + } else { + (*tuc)[name] = 1 + } +} + +// Decrease decreases the usage of an element +func (tuc *TypeUsageCounter) Decrease(name string) { + if counter, ok := (*tuc)[name]; ok && counter > 1 { + (*tuc)[name] = counter - 1 + } else { + (*tuc)[name] = 0 + } +} + +// Get returns the usage count of the input name +func (tuc *TypeUsageCounter) Get(name string) int { + counter, ok := (*tuc)[name] + if !ok { + return 0 + } + return counter +} diff --git a/openapi/internal/utils.go b/openapi/internal/utils.go index b734cf8..ee9b6e6 100644 --- a/openapi/internal/utils.go +++ b/openapi/internal/utils.go @@ -3,7 +3,6 @@ package internal import ( "fmt" "log/slog" - "regexp" "slices" "strings" @@ -14,114 +13,6 @@ import ( "gopkg.in/yaml.v3" ) -var ( - bracketRegexp = regexp.MustCompile(`[\{\}]`) - schemaRefNameV2Regexp = regexp.MustCompile(`^#/definitions/([a-zA-Z0-9\.\-_]+)$`) - schemaRefNameV3Regexp = regexp.MustCompile(`^#/components/schemas/([a-zA-Z0-9\.\-_]+)$`) -) - -var defaultScalarTypes = map[rest.ScalarName]*schema.ScalarType{ - rest.ScalarBoolean: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationBoolean().Encode(), - }, - rest.ScalarString: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationString().Encode(), - }, - rest.ScalarInt32: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationInt32().Encode(), - }, - rest.ScalarInt64: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationInt64().Encode(), - }, - rest.ScalarFloat32: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationFloat32().Encode(), - }, - rest.ScalarFloat64: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationFloat64().Encode(), - }, - rest.ScalarJSON: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationJSON().Encode(), - }, - // string format variants https://swagger.io/docs/specification/data-models/data-types/#string - // string with date format - rest.ScalarDate: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationDate().Encode(), - }, - // string with date-time format - rest.ScalarTimestampTZ: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationTimestampTZ().Encode(), - }, - // string with byte format - rest.ScalarBytes: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationBytes().Encode(), - }, - // string with byte format - rest.ScalarBinary: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationBytes().Encode(), - }, - rest.ScalarEmail: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationString().Encode(), - }, - rest.ScalarURI: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationString().Encode(), - }, - rest.ScalarUUID: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationUUID().Encode(), - }, - rest.ScalarIPV4: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationString().Encode(), - }, - rest.ScalarIPV6: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationString().Encode(), - }, - // unix-time the timestamp integer which is measured in seconds since the Unix epoch - rest.ScalarUnixTime: { - AggregateFunctions: schema.ScalarTypeAggregateFunctions{}, - ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{}, - Representation: schema.NewTypeRepresentationInt32().Encode(), - }, -} - -// ConvertOptions represent the common convert options for both OpenAPI v2 and v3 -type ConvertOptions struct { - MethodAlias map[string]string - TrimPrefix string - EnvPrefix string - Logger *slog.Logger -} - func applyConvertOptions(opts ConvertOptions) *ConvertOptions { if opts.Logger == nil { opts.Logger = slog.Default() @@ -459,6 +350,39 @@ func evalSchemaProxiesSlice(schemaProxies []*base.SchemaProxy, location rest.Par return results, nullable } +func cleanUnusedSchemaTypes(schema *rest.NDCRestSchema, usageCounter *TypeUsageCounter) { + for key := range schema.ObjectTypes { + cleanUnusedObjectType(schema, usageCounter, key) + } + for key := range schema.ScalarTypes { + if usageCounter.Get(key) == 0 { + delete(schema.ScalarTypes, key) + } + } +} + +// recursively clean unused objects as well as their inner properties +func cleanUnusedObjectType(schema *rest.NDCRestSchema, usageCounter *TypeUsageCounter, key string) { + object, ok := schema.ObjectTypes[key] + if !ok { + return + } + if usageCounter.Get(key) > 0 { + return + } + delete(schema.ObjectTypes, key) + for _, elem := range object.Fields { + elemName := getNamedType(elem.Type.Interface(), true, "") + if elemName == "" { + continue + } + usageCounter.Decrease(elemName) + if usageCounter.Get(elemName) <= 0 { + cleanUnusedObjectType(schema, usageCounter, elemName) + } + } +} + func formatWriteObjectName(name string) string { return fmt.Sprintf("%sInput", name) } diff --git a/openapi/testdata/jsonplaceholder/expected.json b/openapi/testdata/jsonplaceholder/expected.json index 6bc6978..d90dd9d 100644 --- a/openapi/testdata/jsonplaceholder/expected.json +++ b/openapi/testdata/jsonplaceholder/expected.json @@ -1162,13 +1162,6 @@ "type": "int64" } }, - "NotFoundError": { - "aggregate_functions": {}, - "comparison_operators": {}, - "representation": { - "type": "json" - } - }, "String": { "aggregate_functions": {}, "comparison_operators": {}, diff --git a/openapi/testdata/onesignal/expected-patch.json b/openapi/testdata/onesignal/expected-patch.json index 46110a7..c18c7eb 100644 --- a/openapi/testdata/onesignal/expected-patch.json +++ b/openapi/testdata/onesignal/expected-patch.json @@ -138,11 +138,6 @@ "comparison_operators": {}, "representation": { "type": "float64" } }, - "IdentityObject": { - "aggregate_functions": {}, - "comparison_operators": {}, - "representation": { "type": "json" } - }, "Int32": { "aggregate_functions": {}, "comparison_operators": {}, diff --git a/openapi/testdata/onesignal/expected.json b/openapi/testdata/onesignal/expected.json index c060310..8dda2b3 100644 --- a/openapi/testdata/onesignal/expected.json +++ b/openapi/testdata/onesignal/expected.json @@ -329,7 +329,7 @@ "type": { "type": "nullable", "underlying_type": { - "name": "JSON", + "name": "Notification200Errors", "type": "named" } } @@ -1479,13 +1479,6 @@ "type": "float64" } }, - "IdentityObject": { - "aggregate_functions": {}, - "comparison_operators": {}, - "representation": { - "type": "json" - } - }, "Int32": { "aggregate_functions": {}, "comparison_operators": {}, @@ -1518,7 +1511,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["sent", "clicked"], + "one_of": [ + "sent", + "clicked" + ], "type": "enum" } }, @@ -1526,7 +1522,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["OR", "AND"], + "one_of": [ + "OR", + "AND" + ], "type": "enum" } }, @@ -1534,7 +1533,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["sum", "count"], + "one_of": [ + "sum", + "count" + ], "type": "enum" } }, @@ -1542,7 +1544,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["push", "email", "sms"], + "one_of": [ + "push", + "email", + "sms" + ], "type": "enum" } }, diff --git a/openapi/testdata/petstore2/swagger.json b/openapi/testdata/petstore2/swagger.json index 9fdfce6..483984d 100644 --- a/openapi/testdata/petstore2/swagger.json +++ b/openapi/testdata/petstore2/swagger.json @@ -704,6 +704,14 @@ } }, "definitions": { + "Foo": { + "type": "object", + "properties": { + "code": { "type": "integer", "format": "int32" }, + "type": { "type": "string" }, + "message": { "type": "string" } + } + }, "ApiResponse": { "type": "object", "properties": { @@ -821,6 +829,34 @@ "type": "string" } } + }, + "consentRequestSession": { + "type": "object", + "properties": { + "access_token": { + "type": "object", + "additionalProperties": true + }, + "id_token": { + "type": "object", + "additionalProperties": true + } + } + }, + "PreviousConsentSession": { + "type": "object", + "properties": { + "remember": { + "type": "boolean" + }, + "remember_for": { + "type": "integer", + "format": "int64" + }, + "session": { + "$ref": "#/definitions/consentRequestSession" + } + } } }, "externalDocs": { diff --git a/openapi/testdata/petstore3/expected.json b/openapi/testdata/petstore3/expected.json index 2bb140d..8532423 100644 --- a/openapi/testdata/petstore3/expected.json +++ b/openapi/testdata/petstore3/expected.json @@ -5,8 +5,7 @@ "url": "{{PET_STORE_SERVER_URL:-https://petstore3.swagger.io/api/v3}}" }, { - "url": "{{PET_STORE_SERVER_URL_2:-https://petstore3.swagger.io/api/v3.1}}", - "group": "test" + "url": "{{PET_STORE_SERVER_URL_2:-https://petstore3.swagger.io/api/v3.1}}" } ], "timeout": "{{PET_STORE_TIMEOUT}}", @@ -44,7 +43,10 @@ "security": [ {}, { - "petstore_auth": ["write:pets", "read:pets"] + "petstore_auth": [ + "write:pets", + "read:pets" + ] } ], "version": "1.0.19" @@ -68,7 +70,10 @@ ], "security": [ { - "petstore_auth": ["write:pets", "read:pets"] + "petstore_auth": [ + "write:pets", + "read:pets" + ] } ] }, @@ -114,7 +119,10 @@ ], "security": [ { - "petstore_auth": ["write:pets", "read:pets"] + "petstore_auth": [ + "write:pets", + "read:pets" + ] } ] }, @@ -161,7 +169,10 @@ "api_key": [] }, { - "petstore_auth": ["write:pets", "read:pets"] + "petstore_auth": [ + "write:pets", + "read:pets" + ] } ] }, @@ -1850,6 +1861,15 @@ }, "PostCheckoutSessionsBodyPaymentMethodOptionsAcssDebit": { "fields": { + "affirm": { + "type": { + "type": "nullable", + "underlying_type": { + "name": "PaymentMethodAffirm", + "type": "named" + } + } + }, "currency": { "type": { "type": "nullable", @@ -3425,7 +3445,10 @@ "method": "post", "security": [ { - "petstore_auth": ["write:pets", "read:pets"] + "petstore_auth": [ + "write:pets", + "read:pets" + ] } ], "requestBody": { @@ -3457,7 +3480,10 @@ "method": "put", "security": [ { - "petstore_auth": ["write:pets", "read:pets"] + "petstore_auth": [ + "write:pets", + "read:pets" + ] } ], "requestBody": { @@ -3514,7 +3540,10 @@ ], "security": [ { - "petstore_auth": ["write:pets", "read:pets"] + "petstore_auth": [ + "write:pets", + "read:pets" + ] } ] }, @@ -3580,7 +3609,10 @@ ], "security": [ { - "petstore_auth": ["write:pets", "read:pets"] + "petstore_auth": [ + "write:pets", + "read:pets" + ] } ] }, @@ -3635,7 +3667,10 @@ ], "security": [ { - "petstore_auth": ["write:pets", "read:pets"] + "petstore_auth": [ + "write:pets", + "read:pets" + ] } ], "requestBody": { @@ -3719,7 +3754,10 @@ }, "encoding": { "profileImage": { - "contentType": ["image/png", "image/jpeg"], + "contentType": [ + "image/png", + "image/jpeg" + ], "headers": { "X-Rate-Limit-Limit": { "explode": false, @@ -4776,6 +4814,10 @@ "type": "object", "nullable": true, "properties": { + "affirm": { + "type": "PaymentMethodAffirm", + "nullable": true + }, "currency": { "type": "CheckoutCurrency", "nullable": true @@ -5985,7 +6027,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["auto", "never"], + "one_of": [ + "auto", + "never" + ], "type": "enum" } }, @@ -5993,7 +6038,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["AC", "AD"], + "one_of": [ + "AC", + "AD" + ], "type": "enum" } }, @@ -6001,7 +6049,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["", "exclude_tax", "include_inclusive_tax"], + "one_of": [ + "", + "exclude_tax", + "include_inclusive_tax" + ], "type": "enum" } }, @@ -6009,7 +6061,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["auto", "required"], + "one_of": [ + "auto", + "required" + ], "type": "enum" } }, @@ -6017,7 +6072,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["automatic", "automatic_async", "manual"], + "one_of": [ + "automatic", + "automatic_async", + "manual" + ], "type": "enum" } }, @@ -6025,7 +6084,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["android", "ios", "web"], + "one_of": [ + "android", + "ios", + "web" + ], "type": "enum" } }, @@ -6033,7 +6096,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["cad", "usd"], + "one_of": [ + "cad", + "usd" + ], "type": "enum" } }, @@ -6041,7 +6107,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["always", "if_required"], + "one_of": [ + "always", + "if_required" + ], "type": "enum" } }, @@ -6049,7 +6118,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["invoice", "subscription"], + "one_of": [ + "invoice", + "subscription" + ], "type": "enum" } }, @@ -6057,7 +6129,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["bank_transfer"], + "one_of": [ + "bank_transfer" + ], "type": "enum" } }, @@ -6065,7 +6139,12 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["day", "month", "week", "year"], + "one_of": [ + "day", + "month", + "week", + "year" + ], "type": "enum" } }, @@ -6073,7 +6152,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["auto", "bg", "cs"], + "one_of": [ + "auto", + "bg", + "cs" + ], "type": "enum" } }, @@ -6081,7 +6164,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["cancel", "create_invoice", "pause"], + "one_of": [ + "cancel", + "create_invoice", + "pause" + ], "type": "enum" } }, @@ -6089,7 +6176,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["payment", "setup", "subscription"], + "one_of": [ + "payment", + "setup", + "subscription" + ], "type": "enum" } }, @@ -6097,7 +6188,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["auto", "never"], + "one_of": [ + "auto", + "never" + ], "type": "enum" } }, @@ -6105,7 +6199,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["always", "if_required"], + "one_of": [ + "always", + "if_required" + ], "type": "enum" } }, @@ -6113,7 +6210,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["acss_debit", "affirm"], + "one_of": [ + "acss_debit", + "affirm" + ], "type": "enum" } }, @@ -6121,7 +6221,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["combined", "interval", "sporadic"], + "one_of": [ + "combined", + "interval", + "sporadic" + ], "type": "enum" } }, @@ -6129,7 +6233,12 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["balances", "ownership", "payment_method", "transactions"], + "one_of": [ + "balances", + "ownership", + "payment_method", + "transactions" + ], "type": "enum" } }, @@ -6137,7 +6246,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["auto", "hidden"], + "one_of": [ + "auto", + "hidden" + ], "type": "enum" } }, @@ -6145,7 +6257,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["cs-CZ", "da-DK"], + "one_of": [ + "cs-CZ", + "da-DK" + ], "type": "enum" } }, @@ -6153,7 +6268,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["balances", "transactions"], + "one_of": [ + "balances", + "transactions" + ], "type": "enum" } }, @@ -6161,7 +6279,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["auto", "none"], + "one_of": [ + "auto", + "none" + ], "type": "enum" } }, @@ -6169,7 +6290,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["create_prorations", "none"], + "one_of": [ + "create_prorations", + "none" + ], "type": "enum" } }, @@ -6177,7 +6301,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["always", "if_required", "never"], + "one_of": [ + "always", + "if_required", + "never" + ], "type": "enum" } }, @@ -6185,7 +6313,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["any", "automatic", "challenge"], + "one_of": [ + "any", + "automatic", + "challenge" + ], "type": "enum" } }, @@ -6193,7 +6325,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["aba", "iban"], + "one_of": [ + "aba", + "iban" + ], "type": "enum" } }, @@ -6201,7 +6336,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["off_session", "on_session"], + "one_of": [ + "off_session", + "on_session" + ], "type": "enum" } }, @@ -6209,7 +6347,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["auto", "never"], + "one_of": [ + "auto", + "never" + ], "type": "enum" } }, @@ -6217,7 +6358,12 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["auto", "book", "donate", "pay"], + "one_of": [ + "auto", + "book", + "donate", + "pay" + ], "type": "enum" } }, @@ -6225,7 +6371,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["exclusive", "inclusive", "unspecified"], + "one_of": [ + "exclusive", + "inclusive", + "unspecified" + ], "type": "enum" } }, @@ -6233,7 +6383,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none", "required"], + "one_of": [ + "none", + "required" + ], "type": "enum" } }, @@ -6241,7 +6394,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["business", "personal"], + "one_of": [ + "business", + "personal" + ], "type": "enum" } }, @@ -6249,7 +6405,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["account", "self"], + "one_of": [ + "account", + "self" + ], "type": "enum" } }, @@ -6257,7 +6416,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["embedded", "hosted"], + "one_of": [ + "embedded", + "hosted" + ], "type": "enum" } }, @@ -6265,7 +6427,13 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["business_day", "day", "hour", "month", "week"], + "one_of": [ + "business_day", + "day", + "hour", + "month", + "week" + ], "type": "enum" } }, @@ -6273,17 +6441,14 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["automatic", "instant", "microdeposits"], + "one_of": [ + "automatic", + "instant", + "microdeposits" + ], "type": "enum" } }, - "ConnectEmbeddedAccountFeatures": { - "aggregate_functions": {}, - "comparison_operators": {}, - "representation": { - "type": "json" - } - }, "Float64": { "aggregate_functions": {}, "comparison_operators": {}, @@ -6316,7 +6481,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["placed", "approved", "delivered"], + "one_of": [ + "placed", + "approved", + "delivered" + ], "type": "enum" } }, @@ -6331,7 +6500,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["available", "pending", "sold"], + "one_of": [ + "available", + "pending", + "sold" + ], "type": "enum" } }, @@ -6339,7 +6512,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["custom"], + "one_of": [ + "custom" + ], "type": "enum" } }, @@ -6347,7 +6522,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["dropdown", "numeric", "text"], + "one_of": [ + "dropdown", + "numeric", + "text" + ], "type": "enum" } }, @@ -6355,7 +6534,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none", "off_session", "on_session"], + "one_of": [ + "none", + "off_session", + "on_session" + ], "type": "enum" } }, @@ -6363,7 +6546,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6371,7 +6556,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6379,7 +6566,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6387,7 +6576,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6395,7 +6586,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none", "off_session", "on_session"], + "one_of": [ + "none", + "off_session", + "on_session" + ], "type": "enum" } }, @@ -6403,7 +6598,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6411,7 +6608,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none", "off_session", "on_session"], + "one_of": [ + "none", + "off_session", + "on_session" + ], "type": "enum" } }, @@ -6419,7 +6620,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none", "off_session", "on_session"], + "one_of": [ + "none", + "off_session", + "on_session" + ], "type": "enum" } }, @@ -6427,7 +6632,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["eu_bank_transfer", "gb_bank_transfer"], + "one_of": [ + "eu_bank_transfer", + "gb_bank_transfer" + ], "type": "enum" } }, @@ -6435,7 +6643,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6443,7 +6653,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6451,7 +6663,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6459,7 +6673,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6467,7 +6683,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6475,7 +6693,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6483,7 +6703,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6491,7 +6713,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6499,7 +6723,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none", "off_session"], + "one_of": [ + "none", + "off_session" + ], "type": "enum" } }, @@ -6507,7 +6734,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6515,7 +6744,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6523,7 +6754,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6531,7 +6764,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["", "manual"], + "one_of": [ + "", + "manual" + ], "type": "enum" } }, @@ -6539,7 +6775,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["", "none", "off_session"], + "one_of": [ + "", + "none", + "off_session" + ], "type": "enum" } }, @@ -6547,7 +6787,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none", "off_session"], + "one_of": [ + "none", + "off_session" + ], "type": "enum" } }, @@ -6555,7 +6798,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none", "off_session", "on_session"], + "one_of": [ + "none", + "off_session", + "on_session" + ], "type": "enum" } }, @@ -6563,7 +6810,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6571,7 +6820,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none", "off_session", "on_session"], + "one_of": [ + "none", + "off_session", + "on_session" + ], "type": "enum" } }, @@ -6579,7 +6832,10 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["automatic", "instant"], + "one_of": [ + "automatic", + "instant" + ], "type": "enum" } }, @@ -6587,7 +6843,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["none"], + "one_of": [ + "none" + ], "type": "enum" } }, @@ -6595,7 +6853,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["fixed_amount"], + "one_of": [ + "fixed_amount" + ], "type": "enum" } }, @@ -6603,7 +6863,11 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["placed", "approved", "delivered"], + "one_of": [ + "placed", + "approved", + "delivered" + ], "type": "enum" } }, @@ -6647,7 +6911,9 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["treasury.inbound_transfer"], + "one_of": [ + "treasury.inbound_transfer" + ], "type": "enum" } }, @@ -6655,7 +6921,12 @@ "aggregate_functions": {}, "comparison_operators": {}, "representation": { - "one_of": ["canceled", "failed", "processing", "succeeded"], + "one_of": [ + "canceled", + "failed", + "processing", + "succeeded" + ], "type": "enum" } }, diff --git a/openapi/testdata/petstore3/source.json b/openapi/testdata/petstore3/source.json index 8bc3e8a..ef22cf1 100644 --- a/openapi/testdata/petstore3/source.json +++ b/openapi/testdata/petstore3/source.json @@ -22,8 +22,7 @@ "url": "https://petstore3.swagger.io/api/v3" }, { - "url": "https://petstore3.swagger.io/api/v3.1", - "x-server-group": "test" + "url": "https://petstore3.swagger.io/api/v3.1" } ], "tags": [ @@ -1909,6 +1908,9 @@ "properties": { "acss_debit": { "properties": { + "affirm": { + "$ref": "#/components/schemas/payment_method_affirm" + }, "currency": { "enum": ["cad", "usd"], "type": "string" diff --git a/schema/enum.go b/schema/enum.go index b9839bc..752b644 100644 --- a/schema/enum.go +++ b/schema/enum.go @@ -189,6 +189,32 @@ const ( ScalarIPV6 ScalarName = "IPv6" ) +var scalarName_enums = []ScalarName{ + ScalarBoolean, + ScalarString, + ScalarInt32, + ScalarInt64, + ScalarFloat32, + ScalarFloat64, + ScalarBigDecimal, + ScalarUUID, + ScalarDate, + ScalarTimestampTZ, + ScalarBytes, + ScalarBinary, + ScalarJSON, + ScalarUnixTime, + ScalarEmail, + ScalarURI, + ScalarIPV4, + ScalarIPV6, +} + +// IsDefaultScalar checks if the scalar name is +func IsDefaultScalar(name string) bool { + return slices.Contains(scalarName_enums, ScalarName(name)) +} + const ( ContentTypeHeader = "Content-Type" ContentTypeJSON = "application/json"