Skip to content

Commit b12442a

Browse files
feat: extract schema-level descriptions from OpenAPI tags (#1)
This adds support for extracting resource and data source descriptions from the OpenAPI tags section. When an operation has tags, the description of the first matching tag from the document's tags array is used as the schema-level description. Changes: - Add Description field to explorer.Resource and explorer.DataSource - Add helper functions getTagDescription and getOperationTagDescription - Update resource_mapper.go to set resourceSchema.Description - Update datasource_mapper.go to set dataSourceSchema.Description - Regenerate golden test files
1 parent 3bb8151 commit b12442a

7 files changed

Lines changed: 75 additions & 13 deletions

File tree

internal/cmd/testdata/github/provider_code_spec.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,9 @@
11851185
"computed_optional_required": "computed"
11861186
}
11871187
}
1188-
]
1188+
],
1189+
"description": "View, modify your gists.",
1190+
"markdown_description": "View, modify your gists."
11891191
}
11901192
},
11911193
{
@@ -6981,7 +6983,9 @@
69816983
"computed_optional_required": "computed"
69826984
}
69836985
}
6984-
]
6986+
],
6987+
"description": "Interact with GitHub Repos.",
6988+
"markdown_description": "Interact with GitHub Repos."
69856989
}
69866990
}
69876991
],
@@ -13175,7 +13179,9 @@
1317513179
"description": "A repository on GitHub."
1317613180
}
1317713181
}
13178-
]
13182+
],
13183+
"description": "Interact with GitHub Repos.",
13184+
"markdown_description": "Interact with GitHub Repos."
1317913185
}
1318013186
}
1318113187
],

internal/cmd/testdata/petstore3/provider_code_spec.json

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
"description": "Order status, possible values - 'placed', 'approved', or 'delivered'"
4444
}
4545
}
46-
]
46+
],
47+
"description": "Access to Petstore orders",
48+
"markdown_description": "Access to Petstore orders"
4749
}
4850
},
4951
{
@@ -124,7 +126,9 @@
124126
}
125127
}
126128
}
127-
]
129+
],
130+
"description": "Everything about your Pets",
131+
"markdown_description": "Everything about your Pets"
128132
}
129133
},
130134
{
@@ -204,7 +208,9 @@
204208
}
205209
}
206210
}
207-
]
211+
],
212+
"description": "Everything about your Pets",
213+
"markdown_description": "Everything about your Pets"
208214
}
209215
}
210216
],
@@ -267,7 +273,9 @@
267273
]
268274
}
269275
}
270-
]
276+
],
277+
"description": "Access to Petstore orders",
278+
"markdown_description": "Access to Petstore orders"
271279
}
272280
},
273281
{
@@ -360,7 +368,9 @@
360368
}
361369
}
362370
}
363-
]
371+
],
372+
"description": "Everything about your Pets",
373+
"markdown_description": "Everything about your Pets"
364374
}
365375
},
366376
{
@@ -410,7 +420,9 @@
410420
"description": "User Status"
411421
}
412422
}
413-
]
423+
],
424+
"description": "Operations about user",
425+
"markdown_description": "Operations about user"
414426
}
415427
}
416428
],

internal/cmd/testdata/scaleway/provider_code_spec.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,9 @@
987987
]
988988
}
989989
}
990-
]
990+
],
991+
"description": "Server types are denomination of the different instances we provide.\nScaleway offers **Virtual Cloud** and **dedicated GPU** instances.\n\n**Virtual Cloud Instances**\n\nVirtual cloud instances are offering the best performance/price ratio for most workloads.\nDifferent instance ranges are proposed:\n\n* The **Development** instances range provides stable and consistent performance for\n development and testing needs. Spin up a development or test environment within seconds.\n Refer to the [Development Instance offer details](https://www.scaleway.com/en/virtual-instances/play2/)\n for more information.\n\n* The **General Purpose** instances range is the solution for production workloads. Powerful\n AMD EPYC CPUs back those instances and offer up to 48 Cores, 256GB of RAM and storage\n options up to 600GB of replicated local NVMe SSD storage and/or up to 10TB of Block Storage.\n Refer to the [General Purpose offer details](https://www.scaleway.com/en/virtual-instances/pro2/) for more information.\n\n* The **Enterprise** instances range is the solution for most demanding workloads and\n mission-critical applications. Powerful AMD EPYC CPUs back those instances and\n offer up to 96 Cores, 384GB of RAM and up to 10TB of Block Storage. Refer to the\n [Enterprise offer details](https://www.scaleway.com/en/virtual-instances/enterprise/) for more information.\n\n**Dedicated GPU Instances**\n\nScaleway GPU Instances are virtual compute instances equipped with dedicated high-end\nNvidia graphical processing unit (GPUs). They are ideal for data processing, artificial\nintelligence, rendering and video encoding. The GPU is dedicated to each instance and\ndirectly exposed through PCI-e. For more information, refer to the\n[GPU Instances Developper Documentation](https://www.scaleway.com/en/docs/compute/gpu/quickstart/).\n",
992+
"markdown_description": "Server types are denomination of the different instances we provide.\nScaleway offers **Virtual Cloud** and **dedicated GPU** instances.\n\n**Virtual Cloud Instances**\n\nVirtual cloud instances are offering the best performance/price ratio for most workloads.\nDifferent instance ranges are proposed:\n\n* The **Development** instances range provides stable and consistent performance for\n development and testing needs. Spin up a development or test environment within seconds.\n Refer to the [Development Instance offer details](https://www.scaleway.com/en/virtual-instances/play2/)\n for more information.\n\n* The **General Purpose** instances range is the solution for production workloads. Powerful\n AMD EPYC CPUs back those instances and offer up to 48 Cores, 256GB of RAM and storage\n options up to 600GB of replicated local NVMe SSD storage and/or up to 10TB of Block Storage.\n Refer to the [General Purpose offer details](https://www.scaleway.com/en/virtual-instances/pro2/) for more information.\n\n* The **Enterprise** instances range is the solution for most demanding workloads and\n mission-critical applications. Powerful AMD EPYC CPUs back those instances and\n offer up to 96 Cores, 384GB of RAM and up to 10TB of Block Storage. Refer to the\n [Enterprise offer details](https://www.scaleway.com/en/virtual-instances/enterprise/) for more information.\n\n**Dedicated GPU Instances**\n\nScaleway GPU Instances are virtual compute instances equipped with dedicated high-end\nNvidia graphical processing unit (GPUs). They are ideal for data processing, artificial\nintelligence, rendering and video encoding. The GPU is dedicated to each instance and\ndirectly exposed through PCI-e. For more information, refer to the\n[GPU Instances Developper Documentation](https://www.scaleway.com/en/docs/compute/gpu/quickstart/).\n"
991993
}
992994
},
993995
{
@@ -2081,7 +2083,9 @@
20812083
"description": "List of servers."
20822084
}
20832085
}
2084-
]
2086+
],
2087+
"description": "Server types are denomination of the different instances we provide.\nScaleway offers **Virtual Cloud** and **dedicated GPU** instances.\n\n**Virtual Cloud Instances**\n\nVirtual cloud instances are offering the best performance/price ratio for most workloads.\nDifferent instance ranges are proposed:\n\n* The **Development** instances range provides stable and consistent performance for\n development and testing needs. Spin up a development or test environment within seconds.\n Refer to the [Development Instance offer details](https://www.scaleway.com/en/virtual-instances/play2/)\n for more information.\n\n* The **General Purpose** instances range is the solution for production workloads. Powerful\n AMD EPYC CPUs back those instances and offer up to 48 Cores, 256GB of RAM and storage\n options up to 600GB of replicated local NVMe SSD storage and/or up to 10TB of Block Storage.\n Refer to the [General Purpose offer details](https://www.scaleway.com/en/virtual-instances/pro2/) for more information.\n\n* The **Enterprise** instances range is the solution for most demanding workloads and\n mission-critical applications. Powerful AMD EPYC CPUs back those instances and\n offer up to 96 Cores, 384GB of RAM and up to 10TB of Block Storage. Refer to the\n [Enterprise offer details](https://www.scaleway.com/en/virtual-instances/enterprise/) for more information.\n\n**Dedicated GPU Instances**\n\nScaleway GPU Instances are virtual compute instances equipped with dedicated high-end\nNvidia graphical processing unit (GPUs). They are ideal for data processing, artificial\nintelligence, rendering and video encoding. The GPU is dedicated to each instance and\ndirectly exposed through PCI-e. For more information, refer to the\n[GPU Instances Developper Documentation](https://www.scaleway.com/en/docs/compute/gpu/quickstart/).\n",
2088+
"markdown_description": "Server types are denomination of the different instances we provide.\nScaleway offers **Virtual Cloud** and **dedicated GPU** instances.\n\n**Virtual Cloud Instances**\n\nVirtual cloud instances are offering the best performance/price ratio for most workloads.\nDifferent instance ranges are proposed:\n\n* The **Development** instances range provides stable and consistent performance for\n development and testing needs. Spin up a development or test environment within seconds.\n Refer to the [Development Instance offer details](https://www.scaleway.com/en/virtual-instances/play2/)\n for more information.\n\n* The **General Purpose** instances range is the solution for production workloads. Powerful\n AMD EPYC CPUs back those instances and offer up to 48 Cores, 256GB of RAM and storage\n options up to 600GB of replicated local NVMe SSD storage and/or up to 10TB of Block Storage.\n Refer to the [General Purpose offer details](https://www.scaleway.com/en/virtual-instances/pro2/) for more information.\n\n* The **Enterprise** instances range is the solution for most demanding workloads and\n mission-critical applications. Powerful AMD EPYC CPUs back those instances and\n offer up to 96 Cores, 384GB of RAM and up to 10TB of Block Storage. Refer to the\n [Enterprise offer details](https://www.scaleway.com/en/virtual-instances/enterprise/) for more information.\n\n**Dedicated GPU Instances**\n\nScaleway GPU Instances are virtual compute instances equipped with dedicated high-end\nNvidia graphical processing unit (GPUs). They are ideal for data processing, artificial\nintelligence, rendering and video encoding. The GPU is dedicated to each instance and\ndirectly exposed through PCI-e. For more information, refer to the\n[GPU Instances Developper Documentation](https://www.scaleway.com/en/docs/compute/gpu/quickstart/).\n"
20852089
}
20862090
}
20872091
],
@@ -2623,7 +2627,9 @@
26232627
"description": "UUID of the image you want to get."
26242628
}
26252629
}
2626-
]
2630+
],
2631+
"description": "Images are backups of your instances.\nYou can reuse that image to restore your data or create a series of instances with a predefined configuration.\n\nAn image is a complete backup of your server including all volumes.\n",
2632+
"markdown_description": "Images are backups of your instances.\nYou can reuse that image to restore your data or create a series of instances with a predefined configuration.\n\nAn image is a complete backup of your server including all volumes.\n"
26272633
}
26282634
},
26292635
{
@@ -2755,7 +2761,9 @@
27552761
]
27562762
}
27572763
}
2758-
]
2764+
],
2765+
"description": "A flexible IP address is an IP address which you hold independently of any server.\nYou can attach it to any of your servers and do live migration of the IP address between your servers.\n\nBe aware that attaching a flexible IP address to a server will remove the previous public IP address of the server and cut any ongoing public connection to the server.\n",
2766+
"markdown_description": "A flexible IP address is an IP address which you hold independently of any server.\nYou can attach it to any of your servers and do live migration of the IP address between your servers.\n\nBe aware that attaching a flexible IP address to a server will remove the previous public IP address of the server and cut any ongoing public connection to the server.\n"
27592767
}
27602768
}
27612769
],

internal/explorer/config_explorer.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func (e configExplorer) FindResources() (map[string]Resource, error) {
9494
DeleteOp: deleteOp,
9595
CommonParameters: commonParameters,
9696
SchemaOptions: extractSchemaOptions(resourceConfig.SchemaOptions),
97+
Description: getOperationTagDescription(e.spec, createOp),
9798
}
9899
}
99100

@@ -121,6 +122,7 @@ func (e configExplorer) FindDataSources() (map[string]DataSource, error) {
121122
ReadOp: readOp,
122123
CommonParameters: commonParameters,
123124
SchemaOptions: extractSchemaOptions(dataSourceConfig.SchemaOptions),
125+
Description: getOperationTagDescription(e.spec, readOp),
124126
}
125127
}
126128
return dataSources, errResult
@@ -216,3 +218,21 @@ func extractOverrides(cfgOverrides map[string]config.Override) map[string]Overri
216218

217219
return overrides
218220
}
221+
222+
// getTagDescription looks up the description for a tag name from the document's tags
223+
func getTagDescription(tags []*highbase.Tag, tagName string) string {
224+
for _, tag := range tags {
225+
if tag.Name == tagName {
226+
return tag.Description
227+
}
228+
}
229+
return ""
230+
}
231+
232+
// getOperationTagDescription gets the description from the first tag of an operation
233+
func getOperationTagDescription(spec high.Document, op *high.Operation) string {
234+
if op == nil || len(op.Tags) == 0 {
235+
return ""
236+
}
237+
return getTagDescription(spec.Tags, op.Tags[0])
238+
}

internal/explorer/explorer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ type Resource struct {
2323
DeleteOp *high.Operation
2424
CommonParameters []*high.Parameter
2525
SchemaOptions SchemaOptions
26+
Description string // Resource-level description from OpenAPI tags
2627
}
2728

2829
// DataSource contains a Read operation and schema options for configuration.
2930
type DataSource struct {
3031
ReadOp *high.Operation
3132
CommonParameters []*high.Parameter
3233
SchemaOptions SchemaOptions
34+
Description string // DataSource-level description from OpenAPI tags
3335
}
3436

3537
// Provider contains a name and a schema.

internal/mapper/datasource_mapper.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,12 @@ func generateDataSourceSchema(logger *slog.Logger, name string, dataSource explo
156156
dataSourceAttributes, _ = dataSourceAttributes.ApplyOverrides(dataSource.SchemaOptions.AttributeOptions.Overrides)
157157

158158
dataSourceSchema.Attributes = dataSourceAttributes.ToSpec()
159+
160+
// Set the data source-level description from OpenAPI tags if available
161+
if dataSource.Description != "" {
162+
dataSourceSchema.Description = &dataSource.Description
163+
dataSourceSchema.MarkdownDescription = &dataSource.Description
164+
}
165+
159166
return dataSourceSchema, nil
160167
}

internal/mapper/resource_mapper.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,12 @@ func generateResourceSchema(logger *slog.Logger, explorerResource explorer.Resou
186186
resourceAttributes, _ = resourceAttributes.ApplyOverrides(explorerResource.SchemaOptions.AttributeOptions.Overrides)
187187

188188
resourceSchema.Attributes = resourceAttributes.ToSpec()
189+
190+
// Set the resource-level description from OpenAPI tags if available
191+
if explorerResource.Description != "" {
192+
resourceSchema.Description = &explorerResource.Description
193+
resourceSchema.MarkdownDescription = &explorerResource.Description
194+
}
195+
189196
return resourceSchema, nil
190197
}

0 commit comments

Comments
 (0)