Ejemplo n.º 1
0
func (r RequestInfo) ToSwagger() swagger.Method {
	ret := swagger.Method{
		Description: r.Description,
		Responses:   map[string]swagger.Response{},
		Parameters:  nil,
		Tags:        []string{strings.Title(r.Group)},
	}

	if len(r.Params) > 0 {
		ret.Parameters = make([]swagger.Param, 0)
	}
	for _, p := range r.Params {
		if !p.Hidden {
			ret.Parameters = append(ret.Parameters, p.ToSwagger())
		}
	}

	if r.Returns != nil {

		s := jsonschema.Reflect(r.Returns)

		ret.Responses["default"] = swagger.Response{
			Description: reflect.TypeOf(r.Returns).String(),
			Schema:      swagger.Schema(s),
		}

	} else {
		ret.Responses["default"] = swagger.Response{"", jsonschema.Reflect("")}

	}

	return ret
}
Ejemplo n.º 2
0
func generateSpecSchema(item interface{}, defs spec.Definitions) *spec.Schema {
	refl := jsonschema.Reflect(item)
	schema := specSchemaFromJsonType(refl.Type)

	schema.Definitions = map[string]spec.Schema{}
	for name, def := range refl.Definitions {
		defs[name] = *specSchemaFromJsonType(def)
	}

	return schema
}
Ejemplo n.º 3
0
func (g *ramlGenerator) Generate(tests []IApiTest) ([]byte, error) {
	doc := g.seed // copy seed

	for _, test := range tests {
		// path MUST begin with '/'
		path := test.Path()
		if path[0] != '/' {
			path = "/" + path
		}

		resource, ok := doc.Resources[path]
		if !ok { // new resource created by default
			resource.UriParameters = map[string]raml.NamedParameter{}
		}

		m := raml.Method{
			Responses:       map[raml.HTTPCode]raml.Response{},
			Headers:         map[raml.HTTPHeader]raml.Header{},
			QueryParameters: map[string]raml.NamedParameter{},
			Description:     test.Description(),
			Name:            test.Method(),
		}

		processedHeaderParams := map[string]interface{}{}
		processedPathParams := map[string]interface{}{}
		processedQueryParams := map[string]interface{}{}

		for _, testCase := range test.TestCases() {
			m.Description = testCase.Description
			for key, param := range testCase.PathParams {
				if _, ok := processedPathParams[key]; ok {
					continue
				}

				uriParam := generateRamlNamedParameter(key, param)

				processedPathParams[key] = nil
				resource.UriParameters[key] = uriParam
			}

			for key, param := range testCase.Headers {
				if _, ok := processedHeaderParams[key]; ok {
					continue
				}

				h := generateRamlNamedParameter(key, param)

				m.Headers[raml.HTTPHeader(key)] = raml.Header(h)
				processedHeaderParams[key] = nil
			}

			for key, param := range testCase.QueryParams {
				if _, ok := processedQueryParams[key]; ok {
					continue
				}

				queryParam := generateRamlNamedParameter(key, param)

				processedQueryParams[key] = nil
				m.QueryParameters[key] = queryParam
			}

			response := raml.Response{}
			response.Description = testCase.Description
			response.HTTPCode = raml.HTTPCode(testCase.ExpectedHttpCode)
			if testCase.ExpectedData != nil {
				schema := jsonschema.Reflect(testCase.ExpectedData)

				// TODO: marshal data according to MIME type, coming soon with RAML 1.0
				schemaBytes, _ := json.MarshalIndent(schema, "", "  ")
				response.Bodies.DefaultSchema = string(schemaBytes)

				// TODO: marshal data according to MIME type, coming soon with RAML 1.0
				exampleBytes, _ := json.MarshalIndent(testCase.ExpectedData, "", "  ")
				response.Bodies.DefaultExample = string(exampleBytes)
			}

			m.Responses[raml.HTTPCode(testCase.ExpectedHttpCode)] = response

		}

		// TODO: check if path has already assigned an method to some other test
		// return error if so
		switch test.Method() {
		case "GET":
			resource.Get = &m
		case "POST":
			resource.Post = &m
		case "PATCH":
			resource.Patch = &m
		case "DELETE":
			resource.Delete = &m
		case "PUT":
			resource.Put = &m
		case "HEAD":
			resource.Head = &m
		}

		doc.Resources[path] = resource
	}

	generatedDoc, err := yaml.Marshal(doc)
	if err == nil {
		header := []byte(fmt.Sprintf("#%%RAML %s\n", doc.RAMLVersion))
		generatedDoc = append(header, generatedDoc...)

	}

	return generatedDoc, err
}