Forum Discussion

fabianobonin's avatar
fabianobonin
Occasional Visitor
6 years ago

Generate spec file from OpenAPI.class java instance

Hi, I need to generate a spec file from a OpenAPI instance that was created programmatically in Java.

Is it possible?

 

	public static void main(String[] args) {
		OpenAPI spec = new OpenAPI();
		spec.setOpenapi("3.0.1");
		spec.setPaths(...);
		spec.setComponents(...);
		
		// generate OpenAPI file in C:/spec.yml
	}
  • Hi fabianobonin,

     

    You can convert an OpenAPI instance to YAML text as follows:

    import io.swagger.v3.core.util.Yaml;
    
    String specAsYaml = Yaml.pretty().writeValueAsString(spec);

    and then save the resulting string to a file.

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi fabianobonin,

     

    You can convert an OpenAPI instance to YAML text as follows:

    import io.swagger.v3.core.util.Yaml;
    
    String specAsYaml = Yaml.pretty().writeValueAsString(spec);

    and then save the resulting string to a file.