Skip to content

Commit 8260d40

Browse files
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>
1 parent 5e7f6ad commit 8260d40

File tree

592 files changed

+2213
-1128
lines changed

Some content is hidden

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

592 files changed

+2213
-1128
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()
@@ -156,11 +157,11 @@ export function compileSpecification (endpointMappings: Record<string, model.End
156157

157158
// Visit all class, interface, enum and type alias definitions
158159
for (const declaration of declarations.classes) {
159-
model.types.push(compileClassOrInterfaceDeclaration(declaration, endpointMappings, declarations.classes))
160+
model.types.push(compileClassOrInterfaceDeclaration(declaration, endpointMappings, declarations.classes, declarations.enums))
160161
}
161162

162163
for (const declaration of declarations.interfaces) {
163-
model.types.push(compileClassOrInterfaceDeclaration(declaration, endpointMappings, declarations.classes))
164+
model.types.push(compileClassOrInterfaceDeclaration(declaration, endpointMappings, declarations.classes, declarations.enums))
164165
}
165166

166167
for (const declaration of declarations.enums) {
@@ -177,7 +178,7 @@ export function compileSpecification (endpointMappings: Record<string, model.End
177178
return model
178179
}
179180

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

@@ -247,6 +248,14 @@ function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | Int
247248
assert(member, property.properties.length > 0, 'There is no need to declare an empty object path_parts, just remove the path_parts declaration.')
248249
pathMember = member
249250
type.path = property.properties
251+
} else if (name === 'request_media_type' || name === 'response_media_type') {
252+
// add those property to requestMediaType and responseMediaType of the endpoint
253+
const mediaType = (member as PropertySignature).getStructure().type as string
254+
if (name === 'request_media_type') {
255+
mapping.requestMediaType = mediaTypeToStringArray(mediaType, allEnums)
256+
} else if (name === 'response_media_type') {
257+
mapping.responseMediaType = mediaTypeToStringArray(mediaType, allEnums)
258+
}
250259
} else if (name === 'query_parameters') {
251260
const property = visitRequestOrResponseProperty(member)
252261
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)