cancel
Showing results for 
Search instead for 
Did you mean: 

Generate spec file from OpenAPI.class java instance

SOLVED
Occasional Visitor

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
	}
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Moderator

Re: Generate spec file from OpenAPI.class java instance

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.


Helen Kosova
SmartBear Technical Writer
________________________
Vote up helpful replies.
Accept this reply if you think it's the best solution to your question.

Join the September Hub-bub to show off, learn and win
1 REPLY 1
Highlighted
Moderator

Re: Generate spec file from OpenAPI.class java instance

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.


Helen Kosova
SmartBear Technical Writer
________________________
Vote up helpful replies.
Accept this reply if you think it's the best solution to your question.

Join the September Hub-bub to show off, learn and win
New Here?
Join us and watch the welcome video:
SeptemberNews
Join our online event
SeptemberHubBub
Top Kudoed Authors
Join the September Hub-bub to show off, learn and win