Skip to content

Commit cbd8338

Browse files
Merge remote-tracking branch 'origin/main' into azure-open-ai-chat-completion-integration
2 parents 65b591d + e4dab78 commit cbd8338

File tree

615 files changed

+3518
-1778
lines changed

Some content is hidden

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

615 files changed

+3518
-1778
lines changed

.github/workflows/code-format.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ jobs:
2121
2222
- name: Code style check
2323
run: |
24+
npm run compile:specification --prefix compiler
2425
npm run format:check --prefix compiler
2526

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/metamodel.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ export class Endpoint {
469469
index?: string[]
470470
cluster?: string[]
471471
}
472+
473+
codegenExclude?: boolean
472474
}
473475

474476
export class UrlTemplate {

compiler/src/model/utils.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ export function hoistRequestAnnotations (
634634
request: model.Request, jsDocs: JSDoc[], mappings: Record<string, model.Endpoint>, response: model.TypeName | null
635635
): void {
636636
const knownRequestAnnotations = [
637-
'rest_spec_name', 'behavior', 'class_serializer', 'index_privileges', 'cluster_privileges', 'doc_id', 'availability', 'doc_tag', 'ext_doc_id'
637+
'rest_spec_name', 'behavior', 'class_serializer', 'index_privileges', 'cluster_privileges', 'doc_id', 'availability', 'doc_tag', 'ext_doc_id', 'codegen_exclude'
638638
]
639639
// in most of the cases the jsDocs comes in a single block,
640640
// but it can happen that the user defines multiple single line jsDoc.
@@ -720,6 +720,9 @@ export function hoistRequestAnnotations (
720720
} else if (tag === 'doc_tag') {
721721
assert(jsDocs, value.trim() !== '', `Request ${request.name.name}'s @doc_tag cannot be empty`)
722722
endpoint.docTag = value.trim()
723+
} else if (tag === 'codegen_exclude') {
724+
// Mark this endpoint to be excluded from client code generation
725+
endpoint.codegenExclude = true
723726
} else {
724727
assert(jsDocs, false, `Unhandled tag: '${tag}' with value: '${value}' on request ${request.name.name}`)
725728
}
@@ -1506,3 +1509,24 @@ export function sortTypeDefinitions (types: model.TypeDefinition[]): void {
15061509
return 0
15071510
})
15081511
}
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.

compiler/src/steps/add-description.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,6 @@ export default async function addDescription (model: model.Model, jsonSpec: Map<
3232
const spec = jsonSpec.get(endpoint.name)
3333
assert(spec, `Can't find the json spec for ${endpoint.name}`)
3434

35-
for (const property of requestDefinition.path) {
36-
const definition = spec.url.paths.find(path => {
37-
if (path.parts == null) return false
38-
return path.parts[property.name] != null
39-
})
40-
if (definition?.parts != null) {
41-
const { description } = definition.parts[property.name]
42-
if (typeof description === 'string') {
43-
property.description = property.description ?? description
44-
}
45-
}
46-
}
47-
48-
if (spec.params != null) {
49-
for (const property of requestDefinition.query) {
50-
const param = spec.params[property.name]
51-
if (param != null && typeof param.description === 'string') {
52-
property.description = property.description ?? param.description
53-
}
54-
}
55-
}
56-
5735
if (spec.documentation.description != null) {
5836
requestDefinition.description = requestDefinition.description ?? spec.documentation.description
5937
}

0 commit comments

Comments
 (0)