Skip to content

Commit f362ab2

Browse files
authored
Add media types (#5779) (#5830)
* Add media types to specification They come from the rest-api-spec. * Support mediaType * Fix lint * Fix compiler lint * Use string values for enums * Take advantage of the AST and use enums text values * Fix linter * Run make spec-format-fix * Add recursive enum parsing to handle meta members * Add const to highlight enum target name * Remove support for composite enums * Add more media types --------- Co-authored-by: Laurent Saint-Félix <laurent.saintfelix@elastic.co> (cherry picked from commit 8260d40) # Conflicts: # output/schema/schema.json # output/schema/validation-errors.json # specification/_global/count/CountRequest.ts # specification/_global/field_caps/FieldCapabilitiesRequest.ts # specification/_global/msearch/MultiSearchRequest.ts # specification/_global/msearch_template/MultiSearchTemplateRequest.ts # specification/_global/open_point_in_time/OpenPointInTimeRequest.ts # specification/_global/rank_eval/RankEvalRequest.ts # specification/_global/render_search_template/RenderSearchTemplateRequest.ts # specification/_global/search/SearchRequest.ts # specification/_global/search_mvt/SearchMvtRequest.ts # specification/_global/search_template/SearchTemplateRequest.ts # specification/_global/terms_enum/TermsEnumRequest.ts # specification/_types/common.ts # specification/async_search/submit/AsyncSearchSubmitRequest.ts # specification/cat/aliases/CatAliasesRequest.ts # specification/cat/circuit_breaker/CatCircuitBreakerRequest.ts # specification/cat/count/CatCountRequest.ts # specification/cat/health/CatHealthRequest.ts # specification/cluster/allocation_explain/ClusterAllocationExplainRequest.ts # specification/cluster/reroute/ClusterRerouteRequest.ts # specification/cluster/state/ClusterStateRequest.ts # specification/connector/get/ConnectorGetRequest.ts # specification/eql/search/EqlSearchRequest.ts # specification/esql/async_query/AsyncQueryRequest.ts # specification/esql/get_query/GetQueryRequest.ts # specification/esql/list_queries/ListQueriesRequest.ts # specification/esql/query/QueryRequest.ts # specification/ilm/migrate_to_data_tiers/Request.ts # specification/indices/add_block/IndicesAddBlockRequest.ts # specification/indices/data_streams_stats/IndicesDataStreamsStatsRequest.ts # specification/indices/delete_sample_configuration/IndicesDeleteSampleConfigurationRequest.ts # specification/indices/exists_alias/IndicesExistsAliasRequest.ts # specification/indices/get_alias/IndicesGetAliasRequest.ts # specification/indices/get_all_sample_configuration/IndicesGetAllSampleConfigurationRequest.ts # specification/indices/get_data_stream_mappings/IndicesGetDataStreamMappingsRequest.ts # specification/indices/get_sample/GetRandomSampleRequest.ts # specification/indices/get_sample_configuration/IndicesGetSampleConfigurationRequest.ts # specification/indices/get_sample_stats/GetRandomSampleStatsRequest.ts # specification/indices/migrate_reindex/MigrateReindexRequest.ts # specification/indices/put_data_stream_mappings/IndicesPutDataStreamMappingsRequest.ts # specification/indices/put_sample_configuration/IndicesPutSampleConfigurationRequest.ts # specification/indices/resolve_index/ResolveIndexRequest.ts # specification/indices/stats/IndicesStatsRequest.ts # specification/indices/unfreeze/IndicesUnfreezeRequest.ts # specification/indices/unfreeze/IndicesUnfreezeResponse.ts # specification/inference/put_ai21/PutAi21Request.ts # specification/inference/put_contextualai/PutContextualAiRequest.ts # specification/inference/put_llama/PutLlamaRequest.ts # specification/inference/put_openshift_ai/PutOpenShiftAiRequest.ts # specification/inference/rerank/RerankRequest.ts # specification/ingest/put_pipeline/PutPipelineRequest.ts # specification/nodes/info/NodesInfoRequest.ts # specification/nodes/stats/NodesStatsRequest.ts # specification/nodes/usage/NodesUsageRequest.ts # specification/project/tags/TagsRequest.ts # specification/security/clear_cached_privileges/SecurityClearCachedPrivilegesRequest.ts # specification/snapshot/delete/SnapshotDeleteRequest.ts # specification/sql/query/QuerySqlRequest.ts # specification/synonyms/delete_synonym_rule/SynonymRuleDeleteRequest.ts # validator/rules/no-inline-unions.js # validator/test/no-inline-unions.test.js
1 parent 03e81e0 commit f362ab2

File tree

573 files changed

+2111
-1088
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

573 files changed

+2111
-1088
lines changed

compiler/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import validateRestSpec from './steps/validate-rest-spec'
2525
import addInfo from './steps/add-info'
2626
import addDescription from './steps/add-description'
2727
import validateModel from './steps/validate-model'
28-
import addContentType from './steps/add-content-type'
2928
import readDefinitionValidation from './steps/read-definition-validation'
3029
import addDeprecation from './steps/add-deprecation'
3130
import ExamplesProcessor from './steps/add-examples'
@@ -73,7 +72,6 @@ compiler
7372
.generateModel()
7473
.step(addInfo)
7574
.step(addDeprecation)
76-
.step(addContentType)
7775
.step(readDefinitionValidation)
7876
.step(validateRestSpec)
7977
.step(addDescription)

compiler/src/model/build-model.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ import {
5353
verifyUniqueness,
5454
parseJsDocTags,
5555
deepEqual,
56-
sourceLocation, sortTypeDefinitions, parseDeprecation
56+
sourceLocation, sortTypeDefinitions, parseDeprecation,
57+
mediaTypeToStringArray
5758
} from './utils'
5859

5960
const jsonSpec = buildJsonSpec()
@@ -143,11 +144,11 @@ export function compileSpecification (endpointMappings: Record<string, model.End
143144

144145
// Visit all class, interface, enum and type alias definitions
145146
for (const declaration of declarations.classes) {
146-
model.types.push(compileClassOrInterfaceDeclaration(declaration, endpointMappings, declarations.classes))
147+
model.types.push(compileClassOrInterfaceDeclaration(declaration, endpointMappings, declarations.classes, declarations.enums))
147148
}
148149

149150
for (const declaration of declarations.interfaces) {
150-
model.types.push(compileClassOrInterfaceDeclaration(declaration, endpointMappings, declarations.classes))
151+
model.types.push(compileClassOrInterfaceDeclaration(declaration, endpointMappings, declarations.classes, declarations.enums))
151152
}
152153

153154
for (const declaration of declarations.enums) {
@@ -164,7 +165,7 @@ export function compileSpecification (endpointMappings: Record<string, model.End
164165
return model
165166
}
166167

167-
function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | InterfaceDeclaration, mappings: Record<string, model.Endpoint>, allClasses: ClassDeclaration[]): model.Request | model.Response | model.Interface {
168+
function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | InterfaceDeclaration, mappings: Record<string, model.Endpoint>, allClasses: ClassDeclaration[], allEnums: EnumDeclaration[]): model.Request | model.Response | model.Interface | model.Enum {
168169
const name = declaration.getName()
169170
assert(declaration, name != null, 'Anonymous definitions should not exists')
170171

@@ -234,6 +235,14 @@ function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | Int
234235
assert(member, property.properties.length > 0, 'There is no need to declare an empty object path_parts, just remove the path_parts declaration.')
235236
pathMember = member
236237
type.path = property.properties
238+
} else if (name === 'request_media_type' || name === 'response_media_type') {
239+
// add those property to requestMediaType and responseMediaType of the endpoint
240+
const mediaType = (member as PropertySignature).getStructure().type as string
241+
if (name === 'request_media_type') {
242+
mapping.requestMediaType = mediaTypeToStringArray(mediaType, allEnums)
243+
} else if (name === 'response_media_type') {
244+
mapping.responseMediaType = mediaTypeToStringArray(mediaType, allEnums)
245+
}
237246
} else if (name === 'query_parameters') {
238247
const property = visitRequestOrResponseProperty(member)
239248
assert(member, property.properties.length > 0, 'There is no need to declare an empty object query_parameters, just remove the query_parameters declaration.')

compiler/src/model/utils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,3 +1504,24 @@ export function sortTypeDefinitions (types: model.TypeDefinition[]): void {
15041504
return 0
15051505
})
15061506
}
1507+
1508+
export function mediaTypeToStringArray (mediaType: string, allEnums: EnumDeclaration[]): string[] {
1509+
const mediaTypeEnumName = 'MediaType'
1510+
const mediaTypeEnum = allEnums.find(e => e.getName() === mediaTypeEnumName)
1511+
1512+
// Handle strings separated by a pipe and return multiple media types
1513+
let enumTypeList: string[]
1514+
if (mediaType.includes('|')) {
1515+
enumTypeList = mediaType.split('|').map(mt => mt.trim())
1516+
} else {
1517+
enumTypeList = [mediaType.trim()]
1518+
}
1519+
1520+
const mediaTypeList: string[] = []
1521+
for (const enumType of enumTypeList) {
1522+
const memberName = enumType.split('.').pop()
1523+
const value = mediaTypeEnum?.getMembers().find(m => m.getName() === memberName)?.getValue() as string
1524+
mediaTypeList.push(value)
1525+
}
1526+
return mediaTypeList
1527+
}

compiler/src/steps/add-content-type.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)