具有不同类型的Swagger-Editor模型数组

时间:2017-01-27 22:35:53

标签: swagger-2.0 swagger-editor

我有一个返回类型,它是一个包含两个元素的数组。第一个元素是一个整数,第二个元素是一个字典数组,其键为public class Statistics extends Application { final CategoryAxis xAxis = new CategoryAxis(); final NumberAxis yAxis = new NumberAxis(); // here I use the modified version of chart final ModifiedBarChart<String, Number> chart = new ModifiedBarChart<>(xAxis, yAxis); final XYChart.Series<String, Number> incoming = new XYChart.Series<>(); final XYChart.Series<String, Number> outgoing = new XYChart.Series<>(); @Override public void start(Stage stage) { xAxis.setLabel("Month"); xAxis.setCategories(FXCollections.observableArrayList( Arrays.asList("Jan", "Feb", "Mar"))); yAxis.setLabel("Value"); incoming.setName("Incoming"); incoming.getData().add(new XYChart.Data("Jan", 25601.34)); incoming.getData().add(new XYChart.Data("Feb", 20148.82)); incoming.getData().add(new XYChart.Data("Mar", 10000)); outgoing.setName("Outgoing"); // To set the min value of yAxis you can either set lowerBound to 0 or don't use negative numbers here outgoing.getData().add(new XYChart.Data("Jan", -7401.85)); outgoing.getData().add(new XYChart.Data("Feb", -1941.19)); outgoing.getData().add(new XYChart.Data("Mar", -5263.37)); chart.getData().addAll(incoming, outgoing); Scene scene = new Scene(chart, 800, 600); stage.setScene(scene); stage.show(); // and here I iterate over all plotChildren elements // note, that firstly all positiveBars come, then all negative ones int dataSize = incoming.getData().size(); for (int i = 0; i < dataSize; i++) { Node positiveBar = chart.getPlotChildren().get(i); // subtract 1 to make two bars y-axis aligned chart.getPlotChildren().get(i + dataSize).setLayoutY(positiveBar.getLayoutY() - 1); } } // note, that I extend BarChart here private static class ModifiedBarChart<X, Y> extends BarChart<X, Y> { public ModifiedBarChart(@NamedArg("xAxis") Axis<X> xAxis, @NamedArg("yAxis") Axis<Y> yAxis) { super(xAxis, yAxis); } public ModifiedBarChart(@NamedArg("xAxis") Axis<X> xAxis, @NamedArg("yAxis") Axis<Y> yAxis, @NamedArg("data") ObservableList<Series<X, Y>> data) { super(xAxis, yAxis, data); } public ModifiedBarChart(@NamedArg("xAxis") Axis<X> xAxis, @NamedArg("yAxis") Axis<Y> yAxis, @NamedArg("data") ObservableList<Series<X, Y>> data, @NamedArg("categoryGap") double categoryGap) { super(xAxis, yAxis, data, categoryGap); } @Override public ObservableList<Node> getPlotChildren() { return super.getPlotChildren(); } } sql_identname值是一个整数,sql_ident值是一个字符串。

我无法弄清楚如何在我的响应对象中对此进行建模。所以它就像:

name

1 个答案:

答案 0 :(得分:0)

在OpenAPI / Swagger 2.0中,数组项必须属于同一类型,因此无法精确建模响应。您可以做的最多是使用typeless schema items,这意味着项目可以是任何内容 - 数字,对象,字符串等 - 但您无法指定项目的确切类型。

definitions:
  MyResponse:
    type: array
    items: {}

在OpenAPI 3.0中,可以使用oneOfanyOf来描述多类型数组:

components:
  schemas:
    MyResponse:
      type: array
      items:
        oneOf:
          - type: integer
          - type: array
            items:
              $ref: "#/components/schemas/MyDictionary"

    MyDictionary:
      type: object
      properties:
        sql_ident:
          type: string
        name:
          type: string