Skip to content

core.openapi.gen #

OpenAPI Code Generation Module

Way structure definitions are written and arranged

Object schemas are defined in an OpenAPI Specification, which define the structure of data passed as parameters to a API Call, and data returned by the calls.

These schemas therefore require data structures that need to be defined as V structs in code.

Object schemas defined in the components field of the OpenAPI Specification are assumed to be 'common' to API calls defined in the specification. The structs representing these common object schemas are therefore defined in a model.v file.

After that, the schemas defined in the path operations are generated alongside the Client API Methods they belong to.

openapi.json

{
    'components': {
        'schemas': {
            'Person': {}
        }
    },
    'paths': {
        '/new_person': {
            'post': {
                'parameters': [
                    {
                        'name': 'person_args',
                        'schema': {
                            'type': 'object'
                        }
                    }
                ]
            }
        }
    }
}

model.v

struct Person{}

methods.v

struct NewPersonArgs {}

fn new_person(person_args NewPersonArgs) Person {}

fn generate_client_module #

fn generate_client_module(config ClientConfig) !Module

enum ParameterEncoding #

enum ParameterEncoding {
	path
}

struct ClientConfig #

struct ClientConfig {
	api_name string
	paths    []Path
	structs  []Struct
}

configures client to be generated

struct ClientGenerator #

struct ClientGenerator {
	api_name      string // name of the api the client is being generated for
	client_struct Struct // the structure representing the API Client, receiver of API Call methods.
pub mut:
	generated_structs []Struct
	generated_methods []string
}

fn (ClientGenerator) generate_client_method #

fn (mut gen ClientGenerator) generate_client_method(config ClientMethodConfig) !codemodel.Function

generate_client_call generates a client method and accompanying necessary

struct ClientMethodConfig #

@[params]
struct ClientMethodConfig {
	receiver   codemodel.Param
	name       string // name of method
	parameters []Parameter
	responses  map[string]Parameter // Params mapped by http response code
	method     http.Method
}

struct Operation #

struct Operation {
	name       string
	method     http.Method
	parameters []Parameter
	responses  map[string]Parameter // Params mapped by http response code
}

HTTP Operations that can be done with a given path

struct Parameter #

struct Parameter {
	codemodel.Param
	encoding ParameterEncoding
}

API Call or Response parameter.

struct Path #

struct Path {
	operations []Operation
}

HTTP API Paths