From 720c9ad6d8181aa8494ff6c6a4db49f9ade7b06a Mon Sep 17 00:00:00 2001 From: Rucciva Date: Wed, 19 Jun 2024 14:51:02 +0700 Subject: [PATCH] inline ref when its not component --- internal/bundle/testdata/profile/profile.yml | 16 +++- internal/bundle/testdata/profile/tenants.yml | 77 ++++++++++++++++++++ internal/bundle/testoutput/profile.yml | 52 ++++++++++++- internal/util/component.go | 19 +++-- 4 files changed, 153 insertions(+), 11 deletions(-) create mode 100644 internal/bundle/testdata/profile/tenants.yml diff --git a/internal/bundle/testdata/profile/profile.yml b/internal/bundle/testdata/profile/profile.yml index 2453502..2bbb65c 100644 --- a/internal/bundle/testdata/profile/profile.yml +++ b/internal/bundle/testdata/profile/profile.yml @@ -15,6 +15,16 @@ paths: $ref: paths/profiles.yml /tenants/{tenant-id}/profiles/{profile-id}: $ref: paths/profile.yml - -components: - x-test: {} + /tenants/{tenant-id}: + get: + operationId: "GetTenant" + summary: "Get Tenant" + parameters: + - $ref: tenants.yml#/paths/~1tenants~1{tenant-id}/parameters/0 + responses: + 200: + $ref: tenants.yml#/paths/~1tenants~1{tenant-id}/get/responses/200 + 404: + $ref: tenants.yml#/paths/~1tenants~1{tenant-id}/get/responses/404 + 500: + $ref: tenants.yml#/paths/~1tenants~1{tenant-id}/get/responses/500 diff --git a/internal/bundle/testdata/profile/tenants.yml b/internal/bundle/testdata/profile/tenants.yml new file mode 100644 index 0000000..e505dd7 --- /dev/null +++ b/internal/bundle/testdata/profile/tenants.yml @@ -0,0 +1,77 @@ +openapi: "3.0.0" +info: + title: "Profile API" + version: "1.0.0" + license: + name: "Internal" + url: "http://localhost" +servers: + - url: "https://profile:8443" + - url: "https://localhost:8443" +security: + - {} +paths: + /tenants/{tenant-id}: + parameters: + - name: tenant-id + required: true + in: path + schema: + $ref: "#/components/schemas/TheUUID" + get: + security: + - {} + summary: "get tenant" + operationId: "GetTenant" + responses: + "200": + $ref: "#/components/responses/Tenant" + "404": + $ref: "#/components/responses/TenantNotFound" + "500": + description: "Error" + content: + application/json: + schema: + properties: + message: + $ref: "#/components/schemas/TheZeroableString" +components: + schemas: + TheUUID: + type: string + format: uuid + x-go-type-skip-optional-pointer: true + Tenant: + properties: + id: + $ref: "#/components/schemas/TheUUID" + name: + $ref: "#/components/schemas/TheZeroableString" + TheZeroableString: + type: string + x-go-type-skip-optional-pointer: true + responses: + Error: + description: "error" + content: + "application/json": + schema: + properties: + id: + $ref: "#/components/schemas/TheUUID" + Tenant: + description: "success" + headers: + TraceID: + $ref: "#/components/headers/TraceID" + content: + "application/json": + schema: + $ref: "#/components/schemas/Tenant" + TenantNotFound: + $ref: "#/components/responses/Error" + headers: + TraceID: + schema: + $ref: "#/components/schemas/TheZeroableString" diff --git a/internal/bundle/testoutput/profile.yml b/internal/bundle/testoutput/profile.yml index ea3dce4..97abe64 100644 --- a/internal/bundle/testoutput/profile.yml +++ b/internal/bundle/testoutput/profile.yml @@ -94,6 +94,29 @@ paths: description: bad request "500": description: server error + /tenants/{tenant-id}: + get: + operationId: "GetTenant" + summary: "Get Tenant" + parameters: + - name: tenant-id + required: true + in: path + schema: + $ref: '#/components/schemas/TheUUID' + responses: + "200": + $ref: '#/components/responses/Tenant' + "404": + $ref: '#/components/responses/TenantNotFound' + "500": + description: "Error" + content: + application/json: + schema: + properties: + message: + $ref: '#/components/schemas/TheZeroableString' components: schemas: UUID: @@ -135,6 +158,19 @@ components: $ref: '#/components/schemas/ZeroableString' dob: $ref: '#/components/schemas/ZeroableTime' + TheUUID: + type: string + format: uuid + x-go-type-skip-optional-pointer: true + TheZeroableString: + type: string + x-go-type-skip-optional-pointer: true + Tenant: + properties: + id: + $ref: '#/components/schemas/TheUUID' + name: + $ref: '#/components/schemas/TheZeroableString' responses: Error: description: "error" @@ -143,7 +179,7 @@ components: schema: properties: id: - $ref: '#/components/schemas/UUID' + $ref: '#/components/schemas/TheUUID' Profile: description: "success" headers: @@ -155,6 +191,17 @@ components: $ref: '#/components/schemas/Profile' ProfileNotFound: $ref: '#/components/responses/Error' + Tenant: + description: "success" + headers: + TraceID: + $ref: '#/components/headers/TraceID' + content: + "application/json": + schema: + $ref: '#/components/schemas/Tenant' + TenantNotFound: + $ref: '#/components/responses/Error' parameters: ProfileID: name: profile-id @@ -172,5 +219,4 @@ components: headers: TraceID: schema: - $ref: '#/components/schemas/ZeroableString' - x-test: {} + $ref: '#/components/schemas/TheZeroableString' diff --git a/internal/util/component.go b/internal/util/component.go index 3914a5f..ade561b 100644 --- a/internal/util/component.go +++ b/internal/util/component.go @@ -65,7 +65,7 @@ func (c Components) copyComponents(docv3 *libopenapi.DocumentModel[v3.Document], continue } - err := c.copyComponentNode(ref, prefix) + exist, err := c.copyComponentNode(ref, prefix) if err != nil { return fmt.Errorf("fail to locate component: %w", err) } @@ -74,7 +74,12 @@ func (c Components) copyComponents(docv3 *libopenapi.DocumentModel[v3.Document], continue } - LocalizeReference(ref, prefix) + if !exist { + ref.Node.Content = idx.GetMappedReferences()[ref.FullDefinition].Node.Content + } else { + LocalizeReference(ref, prefix) + } + } } @@ -91,12 +96,13 @@ func (c Components) copyComponents(docv3 *libopenapi.DocumentModel[v3.Document], return c.replaceRootNodes(docv3) } -func (c Components) copyComponentNode(src *index.Reference, prefix string) (err error) { +func (c Components) copyComponentNode(src *index.Reference, prefix string) (exist bool, err error) { node, err := locateNode(src) if err != nil { - return fmt.Errorf("fail to locate component: %w", err) + return false, fmt.Errorf("fail to locate component: %w", err) } + exist = true name := prefix + src.Name switch { case strings.HasPrefix(src.Definition, "#/components/schemas/"): @@ -125,9 +131,12 @@ func (c Components) copyComponentNode(src *index.Reference, prefix string) (err case strings.HasPrefix(src.Definition, "#/components/callbacks/"): c.Callbacks.Set(name, node) + + default: + exist = false } - return nil + return } func locateNode(ref *index.Reference) (node *yaml.Node, err error) {