Exemple #1
0
func (g *Generator) generateOperation(b *descriptor.Binding) *spec.Operation {
	response := new(spec.Response)
	responses := new(spec.Responses)

	op := new(spec.Operation)
	op.Tags = make([]string, 0)
	op.Parameters = make([]spec.Parameter, 0)

	op.ID = strings.ToLower(b.Method.GetName())

	// op.Consumes = []string{"application/x-www-form-urlencoded", "application/json", "application/x-protobuf"}
	// op.Produces = []string{"application/json", "application/x-protobuf"}
	if cmt := g.file.GetCommentText(b.Method.CommentPath); cmt != nil {
		// TODO(ceram1): Cut by newline to extract summary, like golang.
		op.Summary = *cmt
		_ = strings.Index(*cmt, "\n\n") // idx to cut comment.

		op.Description = *cmt
	}

	for _, param := range b.PathParams {
		p := g.generatePathParameter(&param)
		op.Parameters = append(op.Parameters, *p)
	}

	response.Ref = messageRef(b.Method.ResponseType)

	responses.Default = response
	op.Responses = responses
	return op
}
Exemple #2
0
func (ss *setOpResponses) Parse(lines []string) error {
	if len(lines) == 0 || (len(lines) == 1 && len(lines[0]) == 0) {
		return nil
	}

	var def *spec.Response
	var scr map[int]spec.Response

	for _, line := range lines {
		kv := strings.SplitN(line, ":", 2)
		var key, value string

		if len(kv) > 1 {
			key = strings.TrimSpace(kv[0])
			if key == "" {
				// this must be some weird empty line
				continue
			}
			value = strings.TrimSpace(kv[1])
			if value == "" {
				var resp spec.Response
				if strings.EqualFold("default", key) {
					if def == nil {
						def = &resp
					}
				} else {
					if sc, err := strconv.Atoi(key); err == nil {
						if scr == nil {
							scr = make(map[int]spec.Response)
						}
						scr[sc] = resp
					}
				}
				continue
			}

			var arrays int
			for strings.HasPrefix(value, "[]") {
				arrays++
				value = value[2:]
			}

			var isDefinitionRef bool
			var ref spec.Ref
			var err error
			if arrays == 0 {
				ref, err = spec.NewRef("#/responses/" + value)
			} else {
				isDefinitionRef = true
				ref, err = spec.NewRef("#/definitions/" + value)
			}
			if _, ok := ss.responses[value]; !ok {
				if _, ok := ss.definitions[value]; ok {
					isDefinitionRef = true
					ref, err = spec.NewRef("#/definitions/" + value)
				}
			} else {
			}
			if err != nil {
				return err
			}

			var resp spec.Response

			if !isDefinitionRef {
				resp.Ref = ref
			} else {
				resp.Schema = new(spec.Schema)
				if arrays == 0 {
					resp.Schema.Ref = ref
				} else {
					cs := resp.Schema
					for i := 0; i < arrays; i++ {
						cs.Typed("array", "")
						cs.Items = new(spec.SchemaOrArray)
						cs.Items.Schema = new(spec.Schema)
						cs = cs.Items.Schema
					}
					cs.Ref = ref
				}
			}

			if strings.EqualFold("default", key) {
				if def == nil {
					def = &resp
				}
			} else {
				if sc, err := strconv.Atoi(key); err == nil {
					if scr == nil {
						scr = make(map[int]spec.Response)
					}
					scr[sc] = resp
				}
			}
		}
	}
	ss.set(def, scr)
	return nil
}