Skip to content

Commit 45a2b8e

Browse files
authored
Add media types (#5779) (#5829)
* 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 # specification/_global/clear_scroll/ClearScrollRequest.ts # 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/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/circuit_breaker/CatCircuitBreakerRequest.ts # specification/cat/count/CatCountRequest.ts # specification/cluster/allocation_explain/ClusterAllocationExplainRequest.ts # specification/cluster/reroute/ClusterRerouteRequest.ts # specification/cluster/state/ClusterStateRequest.ts # specification/eql/search/EqlSearchRequest.ts # specification/esql/query/QueryRequest.ts # specification/indices/add_block/IndicesAddBlockRequest.ts # specification/indices/data_streams_stats/IndicesDataStreamsStatsRequest.ts # specification/indices/delete_sample_configuration/IndicesDeleteSampleConfigurationRequest.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/put_data_stream_mappings/IndicesPutDataStreamMappingsRequest.ts # specification/indices/put_sample_configuration/IndicesPutSampleConfigurationRequest.ts # specification/indices/remove_block/IndicesRemoveBlockRequest.ts # specification/indices/resolve_index/ResolveIndexRequest.ts # specification/indices/stats/IndicesStatsRequest.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/security/get_stats/SecurityStatsRequest.ts # specification/snapshot/delete/SnapshotDeleteRequest.ts # specification/sql/query/QuerySqlRequest.ts # specification/streams/status/StreamsStatusRequest.ts
1 parent cd38187 commit 45a2b8e

File tree

577 files changed

+2132
-1093
lines changed

Some content is hidden

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

577 files changed

+2132
-1093
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
@@ -1509,3 +1509,24 @@ export function sortTypeDefinitions (types: model.TypeDefinition[]): void {
15091509
return 0
15101510
})
15111511
}
1512+
1513+
export function mediaTypeToStringArray (mediaType: string, allEnums: EnumDeclaration[]): string[] {
1514+
const mediaTypeEnumName = 'MediaType'
1515+
const mediaTypeEnum = allEnums.find(e => e.getName() === mediaTypeEnumName)
1516+
1517+
// Handle strings separated by a pipe and return multiple media types
1518+
let enumTypeList: string[]
1519+
if (mediaType.includes('|')) {
1520+
enumTypeList = mediaType.split('|').map(mt => mt.trim())
1521+
} else {
1522+
enumTypeList = [mediaType.trim()]
1523+
}
1524+
1525+
const mediaTypeList: string[] = []
1526+
for (const enumType of enumTypeList) {
1527+
const memberName = enumType.split('.').pop()
1528+
const value = mediaTypeEnum?.getMembers().find(m => m.getName() === memberName)?.getValue() as string
1529+
mediaTypeList.push(value)
1530+
}
1531+
return mediaTypeList
1532+
}

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

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

0 commit comments

Comments
 (0)