1

I'm using Swagger Parser in my OpenAPI 3 specification and I'm trying to retrieve multiple objects in the schema section, the problem with my code is that it retrieves only one object defined as follows :

Schema model = definitions.get("NewPet");

This is the code :

public static void main(String[] args) throws Exception {
    OpenAPI swagger = new OpenAPIV3Parser().read("C:\\Users\\openapi.yaml");
            Map < String, Schema > definitions = swagger.getComponents().getSchemas();
            Schema model = definitions.get("NewPet");
            Example example = ExampleBuilder.fromSchema(model, definitions);
            SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
            Json.mapper().registerModule(simpleModule);
            String jsonExample = Json.pretty(example);
            System.out.println(jsonExample);
}

And this is the snippet of the OpenAPI I'm using

components:
schemas:
Pet:
  allOf:
    - $ref: '#/components/schemas/NewPet'
    - required:
      - id
      properties:
        id:
          type: integer
          format: int64

NewPet:
  required:
    - name  
  properties:
    name:
      type: string
    tag:
      type: string    

This is my output { "name" : "string", "tag" : "string" }

I'm searching for a solution that could let me read all the objects of the schema section without having to specify which one of them.

Linda Naoui
  • 147
  • 13
  • 1
    Hint: `definitions` is a map (`Map`). You need to [iterate through the map](https://stackoverflow.com/q/46898/113116). – Helen Jul 23 '19 at 16:55

0 Answers0