json from open api yaml #229

Closed
opened 2025-12-29 08:24:27 +01:00 by adam · 9 comments
Owner

Originally created by @hsnsalhi on GitHub (Dec 3, 2019).

Hello,
Is there a way to create json mapping from existing yaml OpenApi ?
Thank you

Originally created by @hsnsalhi on GitHub (Dec 3, 2019). Hello, Is there a way to create json mapping from existing yaml OpenApi ? Thank you
adam added the question label 2025-12-29 08:24:27 +01:00
adam closed this issue 2025-12-29 08:24:27 +01:00
Author
Owner

@StefH commented on GitHub (Dec 3, 2019):

No sorry, this is not yet possible. I had this idea before, but not yet implemented.

Maybe you can also think about this?

What needs to be done is just parsing the YML and generating an JSON with mappings.

@StefH commented on GitHub (Dec 3, 2019): No sorry, this is not yet possible. I had this idea before, but not yet implemented. Maybe you can also think about this? What needs to be done is just parsing the YML and generating an JSON with mappings.
Author
Owner

@StefH commented on GitHub (Dec 3, 2019):

A possible option could be to use https://github.com/microsoft/OpenAPI.NET to deserialize the yml to an object (OpenApiDocument) and convert that to a MappingModel object

@StefH commented on GitHub (Dec 3, 2019): A possible option could be to use https://github.com/microsoft/OpenAPI.NET to deserialize the yml to an object (OpenApiDocument) and convert that to a MappingModel object
Author
Owner

@hsnsalhi commented on GitHub (Dec 4, 2019):

How can I convert a OpenApiDocument to a MappingModel object ? is this possible with a WireMock helper or should I do it myself ?

@hsnsalhi commented on GitHub (Dec 4, 2019): How can I convert a OpenApiDocument to a MappingModel object ? is this possible with a WireMock helper or should I do it myself ?
Author
Owner

@StefH commented on GitHub (Dec 4, 2019):

I'm working on some proto type code, I keep you informed.

@StefH commented on GitHub (Dec 4, 2019): I'm working on some proto type code, I keep you informed.
Author
Owner

@hsnsalhi commented on GitHub (Dec 4, 2019):

Ok thank you

@hsnsalhi commented on GitHub (Dec 4, 2019): Ok thank you
Author
Owner

@StefH commented on GitHub (Dec 4, 2019):

Should this code be added to WireMock?
Or a separate commandline application, or just a NuGet with a library?

@StefH commented on GitHub (Dec 4, 2019): Should this code be added to WireMock? Or a separate commandline application, or just a NuGet with a library?
Author
Owner

@hsnsalhi commented on GitHub (Dec 5, 2019):

I think the best way is to add it as a feature in WireMock
I've started a try but this needs to be improved

public List<MappingModel> ConvertOpenApiDoc(OpenApiDocument document)
{
	var mappinModels = 
		document.Paths.SelectMany(path => // for each path 
		   path.Value.Operations.SelectMany(operation => // for each operation like (post, get, put...)
		   {
			   var example = operation.Value.RequestBody.Content.SelectMany(content => content.Value.Examples.Select(example => example.Value)).FirstOrDefault();
			   return operation.Value.Responses.Select(response => // for each response
				   new MappingModel
				   {
					   Guid = Guid.NewGuid(),
					   Title = path.Key,
					   Request = new RequestModel { Methods = new[] { operation.Key.ToString() }, Path = path.Key, Body = new BodyModel { Matcher = new MatcherModel { Pattern = example.Value } } },
					   Response = new ResponseModel { StatusCode = int.Parse(response.Key), Body = null }
				   });
		   }));

	return mappinModels.ToList();
}
@hsnsalhi commented on GitHub (Dec 5, 2019): I think the best way is to add it as a feature in WireMock I've started a try but this needs to be improved ```` public List<MappingModel> ConvertOpenApiDoc(OpenApiDocument document) { var mappinModels = document.Paths.SelectMany(path => // for each path path.Value.Operations.SelectMany(operation => // for each operation like (post, get, put...) { var example = operation.Value.RequestBody.Content.SelectMany(content => content.Value.Examples.Select(example => example.Value)).FirstOrDefault(); return operation.Value.Responses.Select(response => // for each response new MappingModel { Guid = Guid.NewGuid(), Title = path.Key, Request = new RequestModel { Methods = new[] { operation.Key.ToString() }, Path = path.Key, Body = new BodyModel { Matcher = new MatcherModel { Pattern = example.Value } } }, Response = new ResponseModel { StatusCode = int.Parse(response.Key), Body = null } }); })); return mappinModels.ToList(); }
Author
Owner

@StefH commented on GitHub (Dec 5, 2019):

See this project:
https://github.com/WireMock-Net/WireMock.Net.OpenApiParser/tree/master/example/YamlDotNetConsoleApp/YamlDotNetConsoleApp

It currently supports reading a schema and creating dummy data. Or it also supports examples.

Some improvements need to be made like better dummy data for objects and more support for other types.

But it's a start.

@StefH commented on GitHub (Dec 5, 2019): See this project: https://github.com/WireMock-Net/WireMock.Net.OpenApiParser/tree/master/example/YamlDotNetConsoleApp/YamlDotNetConsoleApp It currently supports reading a schema and creating dummy data. Or it also supports examples. Some improvements need to be made like better dummy data for objects and more support for other types. But it's a start.
Author
Owner

@StefH commented on GitHub (Jul 7, 2020):

@hsnsalhi

I started a new project : https://github.com/WireMock-Net/WireMock.Net/tree/master/src/WireMock.Net.OpenApiParser

And you can try the console-app: https://github.com/WireMock-Net/WireMock.Net/tree/master/examples/WireMock.Net.OpenApiParser.ConsoleApp

In case you need more functionality, just create an issue.

@StefH commented on GitHub (Jul 7, 2020): @hsnsalhi I started a new project : https://github.com/WireMock-Net/WireMock.Net/tree/master/src/WireMock.Net.OpenApiParser And you can try the console-app: https://github.com/WireMock-Net/WireMock.Net/tree/master/examples/WireMock.Net.OpenApiParser.ConsoleApp In case you need more functionality, just create an issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#229