Skip to content
Closed
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
14 changes: 14 additions & 0 deletions internal/test/issues/issue-1841/a.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
openapi: 3.0.0
components:
schemas:
Merged:
allOf:
- $ref: '#/components/schemas/Example'
- $ref: 'b.yaml#/components/schemas/Merge'
Example:
type: object
properties:
a:
type: string
c:
$ref: 'b.yaml#/components/schemas/Element'
16 changes: 16 additions & 0 deletions internal/test/issues/issue-1841/b.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
openapi: 3.0.0
components:
schemas:
Merge:
type: object
properties:
b:
type: array
items:
$ref: "#/components/schemas/Element"
Element:
type: object
properties:
c:
type: string

14 changes: 14 additions & 0 deletions internal/test/issues/issue-1841/b/openapi.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/test/issues/issue-1841/config-a.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package: issues
generate:
models: true
import-mapping:
b.yaml: github.com/oapi-codegen/oapi-codegen/v2/internal/test/issues/issue-1841/b
output-options:
skip-prune: true
output: openapi.gen.go
6 changes: 6 additions & 0 deletions internal/test/issues/issue-1841/config-b.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package: b
generate:
models: true
output-options:
skip-prune: true
output: b/openapi.gen.go
4 changes: 4 additions & 0 deletions internal/test/issues/issue-1841/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package issues

//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config-a.yaml a.yaml
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config-b.yaml b.yaml
21 changes: 21 additions & 0 deletions internal/test/issues/issue-1841/openapi.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 46 additions & 7 deletions pkg/codegen/merge_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,56 @@ func valueWithPropagatedRef(ref *openapi3.SchemaRef) (openapi3.Schema, error) {
remoteComponent := pathParts[0]

// remote ref
schema := *ref.Value
for _, value := range schema.Properties {
if len(value.Ref) > 0 && value.Ref[0] == '#' {
// local reference, should propagate remote
value.Ref = remoteComponent + value.Ref
}
}
schema := *propagateToSchema(ref.Value, remoteComponent)

return schema, nil
}

func propagateToRef(ref *openapi3.SchemaRef, remoteComponent string) *openapi3.SchemaRef {
if ref == nil {
return nil
}
newRef := *ref
if len(ref.Ref) > 0 && ref.Ref[0] == '#' {
// local reference, should propagate remote
newRef.Ref = remoteComponent + ref.Ref
}
newRef.Value = propagateToSchema(ref.Value, remoteComponent)
return &newRef
}

func propagateToSchema(schema *openapi3.Schema, remoteComponent string) *openapi3.Schema {
newSchema := *schema
newSchema.AdditionalProperties.Schema = propagateToRef(schema.AdditionalProperties.Schema, remoteComponent)
if schema.AllOf != nil {
newSchema.AllOf = make(openapi3.SchemaRefs, len(schema.AllOf))
for i, ref := range schema.AllOf {
newSchema.AllOf[i] = propagateToRef(ref, remoteComponent)
}
}
if schema.AnyOf != nil {
newSchema.AnyOf = make(openapi3.SchemaRefs, len(schema.AnyOf))
for i, ref := range schema.AnyOf {
newSchema.AnyOf[i] = propagateToRef(ref, remoteComponent)
}
}
newSchema.Items = propagateToRef(schema.Items, remoteComponent)
newSchema.Not = propagateToRef(schema.Not, remoteComponent)
if schema.OneOf != nil {
newSchema.OneOf = make(openapi3.SchemaRefs, len(schema.OneOf))
for i, ref := range schema.OneOf {
newSchema.OneOf[i] = propagateToRef(ref, remoteComponent)
}
}
if schema.Properties != nil {
newSchema.Properties = make(openapi3.Schemas, len(schema.Properties))
for key, ref := range schema.Properties {
newSchema.Properties[key] = propagateToRef(ref, remoteComponent)
}
}
return &newSchema
}

func mergeAllOf(allOf []*openapi3.SchemaRef) (openapi3.Schema, error) {
var schema openapi3.Schema
for _, schemaRef := range allOf {
Expand Down