Skip to content

Commit cf3a813

Browse files
committed
Add media types (#5779)
* 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/rank_eval/RankEvalRequest.ts # specification/_global/terms_enum/TermsEnumRequest.ts # specification/cat/circuit_breaker/CatCircuitBreakerRequest.ts # specification/cluster/reroute/ClusterRerouteRequest.ts # specification/cluster/state/ClusterStateRequest.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_sample/GetRandomSampleRequest.ts # specification/indices/get_sample_configuration/IndicesGetSampleConfigurationRequest.ts # specification/indices/get_sample_stats/GetRandomSampleStatsRequest.ts # specification/indices/put_sample_configuration/IndicesPutSampleConfigurationRequest.ts # specification/indices/remove_block/IndicesRemoveBlockRequest.ts # specification/indices/stats/IndicesStatsRequest.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
1 parent e92f312 commit cf3a813

File tree

584 files changed

+2176
-1103
lines changed

Some content is hidden

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

584 files changed

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

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

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

0 commit comments

Comments
 (0)