MCPcopy Index your code
hub / github.com/oapi-codegen/oapi-codegen / Generate

Function Generate

pkg/codegen/codegen.go:149–552  ·  view source on GitHub ↗

Generate uses the Go templating engine to generate all of our server wrappers from the descriptions we've built up above from the schema objects. opts defines

(spec *openapi3.T, opts Configuration)

Source from the content-addressed store, hash-verified

147// the descriptions we've built up above from the schema objects.
148// opts defines
149func Generate(spec *openapi3.T, opts Configuration) (string, error) {
150 // This is global state
151 globalState.options = opts
152 globalState.spec = spec
153 globalState.importMapping = constructImportMapping(opts.ImportMapping)
154 if opts.OutputOptions.TypeMapping != nil {
155 globalState.typeMapping = DefaultTypeMapping.Merge(*opts.OutputOptions.TypeMapping)
156 } else {
157 globalState.typeMapping = DefaultTypeMapping
158 }
159
160 filterOperationsByTag(spec, opts)
161 filterOperationsByOperationID(spec, opts)
162 if !opts.OutputOptions.SkipPrune {
163 pruneUnusedComponents(spec)
164 }
165
166 // if we are provided an override for the response type suffix update it
167 if opts.OutputOptions.ResponseTypeSuffix != "" {
168 responseTypeSuffix = opts.OutputOptions.ResponseTypeSuffix
169 }
170
171 if globalState.options.OutputOptions.ClientTypeName == "" {
172 globalState.options.OutputOptions.ClientTypeName = defaultClientTypeName
173 }
174
175 nameNormalizerFunction := NameNormalizerFunction(opts.OutputOptions.NameNormalizer)
176 nameNormalizer = NameNormalizers[nameNormalizerFunction]
177 if nameNormalizer == nil {
178 return "", fmt.Errorf(`the name-normalizer option %v could not be found among options %q`,
179 opts.OutputOptions.NameNormalizer, NameNormalizers.Options())
180 }
181
182 if nameNormalizerFunction != NameNormalizerFunctionToCamelCaseWithInitialisms && len(opts.OutputOptions.AdditionalInitialisms) > 0 {
183 return "", fmt.Errorf("you have specified `additional-initialisms`, but the `name-normalizer` is not set to `ToCamelCaseWithInitialisms`. Please specify `name-normalizer: ToCamelCaseWithInitialisms` or remove the `additional-initialisms` configuration")
184 }
185
186 globalState.initialismsMap = makeInitialismsMap(opts.OutputOptions.AdditionalInitialisms)
187
188 // Compile streaming-content-type patterns (defaults merged with user-supplied).
189 // Validate() already caught syntax errors, but surface any regression here too.
190 streamingRegexes, err := compileStreamingContentTypes(opts.OutputOptions.StreamingContentTypes)
191 if err != nil {
192 return "", err
193 }
194 globalState.streamingContentTypeRegexes = streamingRegexes
195
196 // Multi-pass name resolution: gather all schemas, then resolve names globally.
197 // Only enabled when resolve-type-name-collisions is set.
198 if opts.OutputOptions.ResolveTypeNameCollisions {
199 gathered := GatherSchemas(spec, opts)
200 globalState.resolvedNames = ResolveNames(gathered)
201 // Build a separate operationID -> wrapper name lookup for genResponseTypeName.
202 // Keys must use the normalized operationID (via nameNormalizer) because
203 // OperationDefinition.OperationId is normalized before templates run.
204 globalState.resolvedClientWrapperNames = make(map[string]string)
205 for _, gs := range gathered {
206 if gs.Context == ContextClientResponseWrapper && gs.OperationID != "" {

Calls 15

constructImportMappingFunction · 0.85
filterOperationsByTagFunction · 0.85
pruneUnusedComponentsFunction · 0.85
NameNormalizerFunctionTypeAlias · 0.85
makeInitialismsMapFunction · 0.85
GatherSchemasFunction · 0.85
ResolveNamesFunction · 0.85
LoadTemplatesFunction · 0.85
GetUserTemplateTextFunction · 0.85
OperationDefinitionsFunction · 0.85